依赖注入之unity(winform方式)
依赖注入之unity(winform方式)
要讲unity就必须先了解DI和IOC及DIP,如下链接提供DI和IOC的基础:https://www.cnblogs.com/zlp520/p/12015973.html
一.什么是unity?
unity是实现依赖注入的IOC容器,通过unity可以降低代码的耦合度。
二.下载并添加引用:
Microsoft.Practices.Unity.dll
Microsoft.Practices.Unity.Configuration.dll
二.实现途径?
1.代码实现:(核心代码)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;
using Microsoft.Practices.Unity.Configuration;
using Microsoft.Practices.Unity;
using ZLP.IBLL;
using ZLP.BLL; namespace ZLP.Win
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
var container = new UnityContainer();//创建容器对象
container.RegisterType<IUserBLL, UserBLL>();//通过代码注入
var instance = container.Resolve<IUserBLL>();//从容器中获取对象
this.dataGridView1.DataSource = instance.GetList();
}
}
}
2.配置文件实现:(推荐,这种方式才是真正的彻底不耦合)
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
<unity>
<typeAliases>
<typeAlias alias="IUserBLL" type="ZLP.IBLL.IUserBLL,ZLP.IBLL" />
<typeAlias alias="UserBLL" type="ZLP.BLL.UserBLL,ZLP.BLL" />
</typeAliases>
<containers>
<container name="defaultContainer">
<type type="IUserBLL" mapTo="UserBLL" name="a"></type >
</container>
</containers>
</unity>
</configuration>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;
using Microsoft.Practices.Unity.Configuration;
using Microsoft.Practices.Unity;
using ZLP.IBLL;
using ZLP.BLL; namespace ZLP.Win
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
var container = new UnityContainer();//创建容器对象
var section = (UnityConfigurationSection)ConfigurationManager.GetSection(UnityConfigurationSection.SectionName);
section.Configure(container, "defaultContainer");
var instance = container.Resolve<IUserBLL>("a");//配置name这里就需要a,没有配置就不需要,多个类实现一个接口,通过name区分
this.dataGridView1.DataSource = instance.GetList();
}
}
}
依赖注入之unity(winform方式)的更多相关文章
- ASP.NET MVC中使用Unity进行依赖注入的三种方式
在ASP.NET MVC中使用Unity进行依赖注入的三种方式 2013-12-15 21:07 by 小白哥哥, 146 阅读, 0 评论, 收藏, 编辑 在ASP.NET MVC4中,为了在解开C ...
- 依赖注入与Unity(一) 介绍
在你学习依赖注入和Unity之前,你需要明白你为什么要使用它们.为了明白为什么要使用它们,你应该明白依赖注入和Unity能够帮助你解决什么类型的问题.作为介绍部分,这一章不会涉及太多关于Uni ...
- 【依赖注入】Unity和Autofac
全面理解ASP.NET Core依赖注入:https://www.cnblogs.com/jesse2013/p/di-in-aspnetcore.html MSDN:https://docs.mic ...
- IoC 依赖注入容器 Unity
原文:IoC 依赖注入容器 Unity IoC 是什么? 在软件工程领域,“控制反转(Inversion of Control,缩写为IoC)”是一种编程技术,表述在面向对象编程中,可描述为在编译时静 ...
- DI 依赖注入之unity的MVC版本使用Microsoft.Practices.Unity1.2与2.0版本对比
DI 依赖注入之unity的MVC版本使用Microsoft.Practices.Unity1.2与2.0版本对比 参考:https://www.cnblogs.com/xishuai/p/36702 ...
- DI 依赖注入之unity(mvc)
DI 依赖注入之unity(使用unity.mvc) 一.nuget下载安装: 使用Nuget安装Unity.MVC 安装完成后会在~/App_Start/目录下自动生成UnityMvcActivat ...
- 控制反转、依赖注入、Unity容器
控制反转原则 依赖注入 Install-Package Unity:https://www.nuget.org/packages/Unity/ Github:https://github.com/un ...
- 峰Spring4学习(2)依赖注入的几种方式
一.装配一个bean 二.依赖注入的几种方式 com.cy.entity People.java: package com.cy.entity; public class People { pri ...
- 在ASP.NET MVC中使用Unity进行依赖注入的三种方式
在ASP.NET MVC4中,为了在解开Controller和Model的耦合,我们通常需要在Controller激活系统中引入IoC,用于处理用户请求的 Controller,让Controller ...
随机推荐
- WPF 精修篇 非UI进程后台更新UI进程
原文:WPF 精修篇 非UI进程后台更新UI进程 <Grid> <Grid.RowDefinitions> <RowDefinition Height="11* ...
- 2019 上海轻轻java面试笔试题 (含面试题解析)
本人5年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.上海轻轻等公司offer,岗位是Java后端开发,因为发展原因最终选择去了上海轻轻,入职一年时间了,也成为了面 ...
- P2995 [USACO10NOV]牛的照片(树状数组,逆序对)
题目: P2995 [USACO10NOV]牛的照片Cow Photographs P4545 [USACO10NOV]奶牛的图片Cow Photographs SP7809 COWPIC - Cow ...
- 如何从ubuntu或PC传递文件到板子,ubuntu如何上网?
3.3 如何从ubuntu或PC传递文件到板子,ubuntu如何上网? 答:以下将分别介绍如何在ubuntu和windows下如何传递文件. ubuntu如何配置上网?ubuntu 上网:打开Orac ...
- 移动前端viewPort的那些事
1.viewport简单说 一般来说,移动上的viewport都是大于浏览器窗口的,不同的设备有自己默认的viewport值(980px或1024px). 2.三个viewport的理解(layout ...
- PowerShell:标记“&&”不是此版本中的有效语句分隔符
将命令行语句中的 && 改为分号 ; 就好了,就是这么简单.
- 企业安全之APT攻击防护
现在针对企业APT[1]攻击越来越多了,企业安全也受到了严重的威胁,由于APT攻击比较隐匿的特性[2],攻击并不能被检测到,所以往往可以在企业内部网络潜伏很长时间. APT的攻击方式多种多样,导致企业 ...
- flink PageRank详解(批量迭代的页面排名算法的基本实现)
1.PageRank算法原理 2.基本数据准备 /** * numPages缺省15个测试页面 * * EDGES表示从一个pageId指向相连的另外一个pageId */ public clas ...
- telnet: connect to address 192.168.120.32: No route to host
原因是 防火墙没有开端口. telnet 测试 3306端口,报错 telnet: connect to address 192.168.120.32: No route to host 再次链接就可 ...
- 并发编程(六)--进程/线程池、协程、gevent第三方库
一.进程/线程池 1.进程池 (1)什么是进程池 如果需要创建的子进程数量不大,可以直接利用multiprocess中的Process来创建.但是当需要创建上百个或上千个,手动创建就较为繁琐,这时就可 ...