zoukankan      html  css  js  c++  java
  • 依赖注入(Autofac)

    1.下载autofac压缩包:http://code.google.com/p/autofac/downloads/list

    2.解压获得autofac.dll和autofac.configuration.dll文件

    依赖注入顾名思义 要存在依赖关系

    例如:

       class DataBaseManager
        {
            IDataBase _idatabase;
            public DataBaseManager(IDataBase idatabase)
            {
                
            }
        }
    

     下面是上图中需要的接口和相应的基类:

     interface IDataBase
        {
            string Name { get;}
            void Select(string commandText);
            void Insert(string commandText);
            void Update(string commandText);
            void Delete(string commandText);
        }
     class SqlDataBase : IDataBase
        {
            public string Name { get { return "sql"; } }
            public void Select(string commandText)
            {
                Console.WriteLine("'{0}'  is  {1}",commandText,Name);
            }
            public void Insert(string commandText)
            {
                Console.WriteLine("'{0}'  is  {1}", commandText, Name);
            }
            public void Update(string commandText)
            {
                Console.WriteLine("'{0}'  is  {1}", commandText, Name);
            }
            public void Delete(string commandText)
            {
                Console.WriteLine("'{0}'  is  {1}", commandText, Name);
            }
        }
        class OracleDataBase : IDataBase
        {
            public string Name { get { return "oracel"; } }
            public void Select(string commandText)
            {
                Console.WriteLine("'{0}'  is  {1}", commandText, Name);
            }
            public void Insert(string commandText)
            {
                Console.WriteLine("'{0}'  is  {1}", commandText, Name);
            }
            public void Update(string commandText)
            {
                Console.WriteLine("'{0}'  is  {1}", commandText, Name);
            }
            public void Delete(string commandText)
            {
                Console.WriteLine("'{0}'  is  {1}", commandText, Name);
            }
        }


    下面我们在主函数中通过autofac实现简单的注入:

    注入前首先要对部分类进行注册 注册的方法有好多种 RegisterInstance RegisterType  Register RegisterMoudle RegisterControllers RegisterAssemblyTypes 我知道的就这些

    static void Main(string[] args)
            {          
                var builder = new ContainerBuilder();
                builder.RegisterType<SqlDataBase>().As<IDataBase>();
                //builder.RegisterInstance(new OracelDataBase()).As<IDataBase>();
    //builder.RegisterType<DataBaseManager>();和下面一行的结果相同 builder.Register(c=>new DataBaseManager(c.Resolve<IDataBase>())); using(var container=builder.Build()) { var manager=container.Resolve<DataBaseManager>(); manager.Select("Select * from database"); } Console.ReadKey(); }
  • 相关阅读:
    仅此一文让你明白ASP.NET MVC 之View的显示
    仅此一文让你明白ASP.NET MVC原理
    MVC4相关Razor语法以及Form表单
    关于jQuery UI 使用心得及技巧
    jQuery编程的最佳实践
    前端不为人知的一面--前端冷知识集锦
    jQuery插件开发精品教程,让你的jQuery提升一个台阶
    常用数据结构及复杂度
    初探12306售票算法
    ajax省市县三级联动
  • 原文地址:https://www.cnblogs.com/steben/p/3399620.html
Copyright © 2011-2022 走看看