依赖注入之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 ...
随机推荐
- Delphi - 鼠标上下滚动基础消息事件
Delphi实现对鼠标上下滚动基础消息的截获并处理 前几天有客户提出需求:由于个人PC界面限制,有时候电子图档显示不全,希望通过鼠标上下滚动用来控制电子图档的放大和缩小. 下面通过一个测试Demo来说 ...
- Windows怎么安装配置Elasticsearch
进入Elasticsearch官网,点击Download,Elasticsearch默认端口9200 然后进入下图:有各种版本,我选择windows版本 下载之后,解压得到Elasticsearch文 ...
- centOs6和Centos7开放/关闭端口区别
#centos6启动防火墙 service iptables start #centos6停止防火墙/关闭防火墙 service iptables stop #centos6重启防火墙 servic ...
- jQuery中的动画(七)
一.jQuery对象样式相关方法1.设置高度和宽度height([num]) [获取或设置样式属性height的值]获取匹配元素中第一个元素的height样式值或给匹配所有元素设置height样 ...
- 解决 new file()在IOS下不兼容 的问题
最近 做项目,做的要是拍照后上传相片,以file格式上传..所以 拍照 后用canvas生成base64格式再转file..在PC和安卓都是没有问题,到IOS上面不行..new file后就是生成一个 ...
- 通过SOFA看Java服务端如何实现运行时的模块化
本文阅读时间大约7分钟. 今天我们谈谈SOFA模块化,首先看一段SOFA的介绍: SOFABoot是蚂蚁金服开源的基于Spring Boot的研发框架,它在Spring Boot的基础上,提供了诸如 ...
- elasticsearch regexp查询特殊字符处理
regexp表面意思就是正则查询,但是如果遇到,查询条件中包含特殊的字符串, 就会发现,需要进行相应的转义处理 需要处理Lucene regexps即可: /** * 转义字符串中的特殊字符 * 仅过 ...
- CENTOS安装xwindow
CentOS6安装图形界面 [root@centos6~]# yum -y install xorg* [root@centos6 ~]# yum -y groupinstall "X Wi ...
- flask Gunicorn和uwsgi并发对比(转载)
转载 结果 吞吐量(要求/秒) 响应时间(毫秒) 失误 吞吐量的标准偏差(要求/秒) 尽管uWSGI的性能在高负载下确实有些不稳定,但它看起来像Python应用服务器.uWSGI不仅速度快得离谱,而且 ...
- springboot中配置urlrewrite实现url伪静态强化网站seo
关于urlrewrite urlrewrite使用强大的自定义规则来使用用户更容易记住.搜索引擎更容易找到的URL(对于seo比较重要).通过使用规则模板.重写映射,Web管理员可以轻松地设置规则,根 ...