zoukankan      html  css  js  c++  java
  • Unity+单例模式的依赖注入

    Unity这个微软自家出的依赖注入,相信很多人都应该听过了,不过貌似现在很少中文的教程,唉,本人E文不好。。只学到点皮毛。

    而公司的网站也是采用Unity来做依赖注入的,在这里就分享一个小Demo出来,希望大家能对Unity有个了解。虽然Spring.Net的也挺好用的,

    不过本人有严重的微软癖,所以还是用微软的。

    1.首先当然是先下载Unity,找不到的可以上google搜索。

    2.为Web项目添加Microsoft.Practices.Unity.dll和Microsoft.Practices.Unity.Configuration.dll两个引用。

    3.在web.config文件中的configSections中添加如下节点

    <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration, Version=1.1.0.0,                  Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

    4.在web.config文件中的configuration中添加如下节点

        <!--具体的Untiy配置项-->
        
    <unity>
            
    <containers>
                
    <container name="Service">
                    
    <types>
                        
    <type type="TestUnity.ITest,TestUnity" mapTo="TestUnity.Test,TestUnity"/>
                    
    </types>
                
    </container>
                
    <container name="Repository">
                    
    <types>
                        
    <type type="TestUnity.IRepo,TestUnity" mapTo="TestUnity.Repo,TestUnity"/>
                    
    </types>
                
    </container>
            
    </containers>
        
    </unity>
        
    <!--结束untiy-->

     container中的name是随便起,type中的type是对应的接口,要把命名空间也加上去,mapTo是接口对应的实现类

    之后添加一个类UnityContext,如下:

        public class UnityContext
        {
            
    public static UnityContext _instance;

            
    private UnityContainer _container;

            
    public static UnityContext Instance
            {
                
    get
                {
                    
    if (_instance == null) _instance = new UnityContext();
                    
    return _instance;
                }
            }

            
    /// <summary>
            
    /// 构造函数
            
    /// </summary>
            public UnityContext()
            {
                _container 
    = new UnityContainer();
                UnityConfigurationSection setion 
    = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
                setion.Containers[
    "Service"].Configure(_container);
                setion.Containers[
    "Repository"].Configure(_container);
            }

            
    public T Load<T>()
            {
                
    return _container.Resolve<T>();
            }
        }

     这是一个简单的单例模式的管理Unity的类。。。写得有点烂。。。其中"Service"和"Repository"就是对应配置文件中的container的name属性。

    public T Load<T>()方法是返回指定类型接口的实现对象。

     调用的话,如下:

                ITest test = UnityContext.Instance.Load<ITest>();
                IRepo repo 
    = UnityContext.Instance.Load<IRepo>();
                
    string hell = test.SayHello();
                
    string hi = repo.SayHi();

     其中ITest,Test,IRepo,Repo为接口与对应的实现类,都是只有简单返回一个"Hello"或者"Hi"的方法。只是为了测试。

    这样的话,基本上就完成了。。至于好处嘛,其实我不太清楚,可能是降低耦合吧。。还有很多不懂的地方。写得很烂,只是希望有个启发各位的效果。有什么不足和不对的地方希望各位多多指教。

    下面是我用的:

    1.Asp.net MVC框架

    2.LinQ to SQL框架

    3.Unity1.1


  • 相关阅读:
    web service
    常用的正则表达式
    xml
    sql helper
    sql server 表连接
    asp.net页面生命周期
    创建简单的ajax对象
    checkbox选中问题
    ES6之扩展运算符 三个点(...)
    Object.assign()的用法 -- 用于将所有可枚举属性的值从一个或多个源对象复制到目标对象,返回目标对象
  • 原文地址:https://www.cnblogs.com/floyd/p/1505117.html
Copyright © 2011-2022 走看看