使用autofac 实现依赖注入

1.引用 autofac.dll 和 autofac.configuration.dll

2.新增接口 IDAL

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace AutoFacTest
{
public interface IDAL
{ void select(string msg); }
}

2.新增 SqlserverDAL 类和 OracleDAL类,并继承IDAL

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace AutoFacTest
{
public class SqlServerDAL:IDAL
{
public void select(string msg)
{
Console.WriteLine("this is sqlserver:"+msg);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace AutoFacTest
{
public class OracleDAL:IDAL
{ public void select(string msg)
{
Console.WriteLine("this is Oracle:" + msg);
}
}
}

3.  在程序里直接实现IOC注入

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autofac;
namespace AutoFacTest
{
class Program
{
static void Main(string[] args)
{
// //直接指定实例类型
var builder = new ContainerBuilder();
builder.RegisterType<IDAL>();
builder.RegisterType<OracleDAL>().As<IDAL>();
using (var container = builder.Build())
{
var manager=container.Resolve<IDAL>();
manager.select("小xiaoniao"); }
Console.ReadLine(); }
}
} 4.也可以通过引用Autofac.Configuration.dll 来配置 App.config或Web.config 配置文件注入
如下:
<configuration>
<configSections>
<section name="autofac" type="Autofac.Configuration.SectionHandler, Autofac.Configuration"/>
</configSections>
<autofac defaultAssembly="AutoFacTest">
<components>
<component type="AutoFacTest.OracleDAL, AutoFacTest" service="AutoFacTest.IDAL" />
</components>
</autofac>

5. 实现直接注入和通过配置文件注入


 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autofac;
using Autofac.Configuration;
namespace AutoFacTest
{
    class Program
    {
        static void Main(string[] args)
        {
            test1();//直接注入
            test2();//配置文件注入
            Console.ReadLine();
        }
        /// <summary>
        /// 直接注入
        /// </summary>
        private static void test1()
        {
            //   //直接指定实例类型
            var builder = new ContainerBuilder();
            builder.RegisterType<IDAL>();
            builder.RegisterType<OracleDAL>().As<IDAL>();
            using (var container = builder.Build())
            {
                var manager = container.Resolve<IDAL>();
                manager.select("直接注入,小xiaoniao");

}
          
            }

/// <summary>
        /// 配置文件注入
        /// </summary>
        private static void test2()
        {
            //   //直接指定实例类型
            var builder = new ContainerBuilder();
            builder.RegisterType<IDAL>();
            builder.RegisterModule(new ConfigurationSettingsReader("autofac"));
            using (var container = builder.Build())
            {
                var manager = container.Resolve<IDAL>();
                manager.select("配置文件注入,小xiaoniao");

}
           
        }
    }
}

直接注入显示结果:this is Oracle:小xiaoniao

将 builder.RegisterType<OracleDAL>().As<IDAL>(); 改成 builder.RegisterType<SqlserverDAL>().As<IDAL>();   则会显示:this is sqlserver:小xiaoniao

直接注入和配置文件注入显示:
this is Oracle:直接注入,小xiaoniao
this is Oracle:配置文件注入,小xiaoniao

DEMO源码

IOC:AutoFac使用demo的更多相关文章

  1. 在asp.net web api 2 (ioc autofac) 使用 Serilog 记录日志

    Serilog是.net里面非常不错的记录日志的库,另外一个我认为比较好的Log库是NLog. 在我个人的asp.net web api 2 基础框架(Github地址)里,我原来使用的是NLog,但 ...

  2. 开源项目 08 IOC Autofac

    using Autofac; using System; using System.Collections.Generic; using System.Linq; using System.Text; ...

  3. Ioc Autofac心得

    对于这个容器注入,个人也不是很熟悉,很多还不懂,只会基本的操作,几天把它记录下来,说不定以后帮助就大了呢,这方面跟安卓差距还是挺大的 下面记录下应用的流程 步骤: 1.添加应用 2.重写工厂(这里讲的 ...

  4. ioc autofac简单示例

    1.winform用法: nuget安装autofac public interface ILog { bool Log(string msg); } public class TXTLogger : ...

  5. asp.net core 四 IOC&DI Autofac

    其实关于IOC,DI已经有了很多的文章,但是自己在使用中还是有很多困惑,而且相信自己使用下,印象还是会比较深刻的 关于这段时间一直在学习.net core,但是这篇文章是比较重要的,也是我自己觉得学习 ...

  6. IOC容器-Autofac在MVC中实现json方式注入使用

    在你阅读时,默认已经了解IOC和autofac的基本用法, 我在最近的我的博客项目中运用了IOC autofac 实现了依赖注入 由于我的项目时asp.net MVC所以我目前向大家展示MVC中如何使 ...

  7. Topshelf的Ioc实现

    在前面使用Topshelf的文章里,我们的工作类TownCrier使用的是无参数的构造函数,满足测试的目的.在实际的开发过程中,我们常常需要使用带有参数的构造函数,就不可避免的使用Ioc的技术.在这里 ...

  8. never下ioc

    生命周期 当前分单例,作用域(范围),短暂.单例是整个服务中只有一个实例,短暂则是每一次得到的都是新的实例,作用域就是在该一套行动中内得到的是同一个实例,该行动中指的是什么?我们看看demo下的sta ...

  9. 【Android开发经验】来,咱们自己写一个Android的IOC框架!

    到眼下位置.afinal开发框架也是用了好几个月了,还记得第一次使用凝视完毕控件的初始化和事件绑定的时候,当时的心情是多么的兴奋- -代码居然能够这样写!然后随着不断的学习,也慢慢的对IOC框架和注解 ...

随机推荐

  1. android xml文件中出现如下提醒:This tag and its children can be replaced by one <TextView/> and a compound drawable

    第一个感叹号 是跟你说 让你把Imageview 和textview 结合起来 只用 textview textview有个属性叫  android:drawable...(top/bottom/.. ...

  2. springMVC拦截器简单配置

    <!-- 拦截器 -->    <mvc:interceptors>        <mvc:interceptor>            <!-- 拦截所 ...

  3. 剑指offer反转链表

    way1: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 3 ...

  4. emulator shortcut

    Alt+Enter Maximizes the emulator. Ctrl+F11 Changes the orientation of the emulator from landscape to ...

  5. 用Eclipse 统计代码行数小技巧

    今天公司SQA问我目前项目代码行数有多少,我当时就是想,以前好像写过类似的统计工具但是一时又找不到 公司网络又不能下载,所以想想eclipse是不是又类似功能,找了下没有,但突然一想有一个转弯方法:统 ...

  6. Hibernate的generator属性

    本文讲述Hibernate的generator属性的意义.Generator属性有7种class,本文简略描述了这7种class的意义和用法. <class name="onlyfun ...

  7. Android Fragment 真正的完全解析(上)--转

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/37970961 自从Fragment出现,曾经有段时间,感觉大家谈什么都能跟Fra ...

  8. whm 设置共享IP

    点击 Main >>Resellers>>resellers centers (manages ressellers center's IP有几个IP代表几个)

  9. 行内元素为何不能设置margin-top、margin-bottom;padding-top、padding-bottom值

    曾经学过的教程中写明:行内元素的特点有: 1.与其他元素在同一行 2.宽度(width).高度(height).内边距的top/bottom(padding-top/padding-bottom)和外 ...

  10. html5中的postMessage解决跨域问题

    解决跨域问题的方法有很多,如:图像ping(简单).jsonp(缺点是不能实现跨域post).CROS(CORS的本质让服务器通过新增响应头Access-Control-Allow-Origin,通过 ...