使用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. 利用工具MailUtils实现邮件的发送,遇到的大坑,高能预警!!

    java实现邮件的发送依赖的jar包有两个:mail.jar和activation.jar,我也找到了一个工具包:itcast-tools-1.4.jar,实现原理大家可以查看源码,先放出资源链接 h ...

  2. Windows API 之 FineFirstFile、FindNextFile

    参考:https://msdn.microsoft.com/en-us/library/aa364418%28VS.85%29.aspx FindFirstFile Searches a direct ...

  3. hdu_2222_Keywords Search(AC自动机板子)

    题目连接:hdu_2222_Keywords Search 存个自己写的AC自动机 #include<cstdio> #include<cstring> #define F(i ...

  4. 安装apk时出现错误Failure [INSTALL_FAILED_DEXOPT]问题解决的方法

    在android4.0源码里面编译出来apk后,用adb install (或adb install -r 重装)安装时,报错[INSTALL_FAILED_DEXOPT]. xu@xu-PC:~$  ...

  5. linux dmesg命令

    linux dmesg命令详解   功能说明:显示开机信息. 语 法:dmesg [-cn][-s ] 补充说明:kernel会将开机信息存储在ring buffer,若是开机时来不及查看信息,可利用 ...

  6. gridview如何隐藏一列数据,但又可以使用这列数据

    解决方案在RowCreated事件中书写如下代码 void GridView1_RowCreated(object sender, GridViewRowEventArgs e)    {       ...

  7. HDU 2087 剪花布条(KMP基础应用)

    KMP基础,注意输入 #include<cstdio> #include<cstring> #include<iostream> using namespace s ...

  8. POJ 1185炮兵阵地 (状压DP)

    题目链接 POJ 1185 今天艾教留了一大堆线段树,表示做不动了,就补补前面的题.QAQ 这个题,我第一次写还是像前面HDU 2167那样写,发现这次影响第 i 行的还用i-2行那样,那以前的方法就 ...

  9. javascript 实现加法分离。 plus(3)(4); // => 得到 7

    原文地址:http://cnodejs.org/topic/5230d5f0101e574521c86ff4 JavaScript 的设计是典型的函数式的编程范式匿名函数 JSON数据本身就是字符串, ...

  10. DataTable的名称要后设置

    string sqldatabase = string.Format(dr["sql"].ToString(), drpat["PATIENT_ID"].ToS ...