Autofac的高级使用——Autofac.2.6.3.862
1. 使用代码方式进行组件注册【依赖服务类和组件类】
- /// <summary>
- /// 管理类
- /// </summary>
- public partial class Mgr
- {
- private static IContainer container = null;
- /// <summary>
- /// 自定义容器和组件注册
- /// </summary>
- /// <returns></returns>
- public static IContainer GetContainer()
- {
- if (container == null)
- {
- var builder = new ContainerBuilder();
- //builder.RegisterType<SqlDatabase>().As<IDatabase>();
- builder.RegisterType<SqlDatabase>().Named<IDatabase>("AutofacDemo.Lib.Sql.SqlDatabase");
- builder.RegisterType<OracleDatabase>().Named<IDatabase>("AutofacDemo.Lib.Oracle.OracleDatabase");
- container = builder.Build();
- }
- return container;
- }
- }
/// <summary>
/// 管理类
/// </summary>
public partial class Mgr
{
private static IContainer container = null; /// <summary>
/// 自定义容器和组件注册
/// </summary>
/// <returns></returns>
public static IContainer GetContainer()
{ if (container == null)
{
var builder = new ContainerBuilder();
//builder.RegisterType<SqlDatabase>().As<IDatabase>();
builder.RegisterType<SqlDatabase>().Named<IDatabase>("AutofacDemo.Lib.Sql.SqlDatabase");
builder.RegisterType<OracleDatabase>().Named<IDatabase>("AutofacDemo.Lib.Oracle.OracleDatabase"); container = builder.Build(); }
return container;
}
}
2. 使用配置文件进行组件注册【不需要依赖】
2.1. 定义配置文件
- <?xml version="1.0"?>
- <configuration>
- <configSections>
- <section name="autofac" type="Autofac.Configuration.SectionHandler, Autofac.Configuration"/>
- </configSections>
- <autofac>
- <components>
- <component name="AutofacDemo.Lib.Oracle.OracleDatabase" type="AutofacDemo.Lib.Oracle.OracleDatabase, AutofacDemo.Lib.Oracle" service="AutofacDemo.Lib.IDatabase, AutofacDemo.Lib"/>
- <component name="AutofacDemo.Lib.Sql.SqlDatabase" type="AutofacDemo.Lib.Sql.SqlDatabase, AutofacDemo.Lib.Sql" service="AutofacDemo.Lib.IDatabase, AutofacDemo.Lib"/>
- </components>
- </autofac>
- <startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="autofac" type="Autofac.Configuration.SectionHandler, Autofac.Configuration"/>
</configSections>
<autofac>
<components>
<component name="AutofacDemo.Lib.Oracle.OracleDatabase" type="AutofacDemo.Lib.Oracle.OracleDatabase, AutofacDemo.Lib.Oracle" service="AutofacDemo.Lib.IDatabase, AutofacDemo.Lib"/>
<component name="AutofacDemo.Lib.Sql.SqlDatabase" type="AutofacDemo.Lib.Sql.SqlDatabase, AutofacDemo.Lib.Sql" service="AutofacDemo.Lib.IDatabase, AutofacDemo.Lib"/>
</components>
</autofac>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>
2.2. 读取config配置文件进行组件注册
- /// <summary>
- /// 根据配置文件里的服务名生成对象
- /// </summary>
- public static void GetFrom_Config()
- {
- var builder = new ContainerBuilder();
- //从.config配置文件中取得相关的组件注册
- builder.RegisterModule(new ConfigurationSettingsReader("autofac"));
- using (var container = builder.Build())
- {
- string vServiceName = "AutofacDemo.Lib.Oracle.OracleDatabase";//服务名
- //vServiceName = "AutofacDemo.Lib.Sql.SqlDatabase";
- //是否存在服务名
- if (container != null)
- {
- if (container.IsRegisteredWithName(vServiceName, typeof(IDatabase)))
- {
- IDatabase db = container.ResolveNamed<IDatabase>(vServiceName);
- if (db != null)
- {
- db.Select("..........");
- }
- }
- }
- }
- }
/// <summary>
/// 根据配置文件里的服务名生成对象
/// </summary>
public static void GetFrom_Config()
{ var builder = new ContainerBuilder(); //从.config配置文件中取得相关的组件注册
builder.RegisterModule(new ConfigurationSettingsReader("autofac")); using (var container = builder.Build())
{
string vServiceName = "AutofacDemo.Lib.Oracle.OracleDatabase";//服务名
//vServiceName = "AutofacDemo.Lib.Sql.SqlDatabase"; //是否存在服务名
if (container != null)
{
if (container.IsRegisteredWithName(vServiceName, typeof(IDatabase)))
{
IDatabase db = container.ResolveNamed<IDatabase>(vServiceName);
if (db != null)
{
db.Select("..........");
}
}
}
}
}
2.3. 是否存在服务名,存在,则根据服务名取得对象
- //是否存在服务名
- if (container.IsRegisteredWithName(vServiceName, typeof(IDatabase)))
- {
- IDatabase db = container.ResolveNamed<IDatabase>(vServiceName);
- if (db != null)
- {
- db.Select("..........");
- }
- }
//是否存在服务名
if (container.IsRegisteredWithName(vServiceName, typeof(IDatabase)))
{
IDatabase db = container.ResolveNamed<IDatabase>(vServiceName);
if (db != null)
{
db.Select("..........");
}
}
3. Demo下载
4. 使用代码方式进行组件注册【不需要依赖】【类似反射的全字符串形式】
- /// <summary>
- /// 自定义容器和通过反射进行组件注册
- /// </summary>
- /// <returns></returns>
- public static IContainer GetContainer()
- {
- if (container == null)
- {
- var builder = new ContainerBuilder();
- Type objType = Type.GetType("AutofacDemo.Lib.IDatabase, AutofacDemo.Lib");
- Type objTypeA = Type.GetType("AutofacDemo.Lib.Oracle.OracleDatabase, AutofacDemo.Lib.Oracle");
- Type objTypeB = Type.GetType("AutofacDemo.Lib.Sql.SqlDatabase, AutofacDemo.Lib.Sql");
- builder.RegisterType(objTypeA).Named("AutofacDemo.Lib.Oracle.OracleDatabase", objType);
- builder.RegisterType(objTypeB).Named("AutofacDemo.Lib.Sql.SqlDatabase", objType);
- ////需要知道接口
- //builder.RegisterType(objTypeA).Named<objType>("AutofacDemo.Lib.Sql.SqlDatabase");
- //builder.RegisterType(objTypeB).Named<objType>("AutofacDemo.Lib.Oracle.OracleDatabase");
- container = builder.Build();
- }
- return container;
- }
/// <summary>
/// 自定义容器和通过反射进行组件注册
/// </summary>
/// <returns></returns>
public static IContainer GetContainer()
{ if (container == null)
{
var builder = new ContainerBuilder(); Type objType = Type.GetType("AutofacDemo.Lib.IDatabase, AutofacDemo.Lib");
Type objTypeA = Type.GetType("AutofacDemo.Lib.Oracle.OracleDatabase, AutofacDemo.Lib.Oracle");
Type objTypeB = Type.GetType("AutofacDemo.Lib.Sql.SqlDatabase, AutofacDemo.Lib.Sql"); builder.RegisterType(objTypeA).Named("AutofacDemo.Lib.Oracle.OracleDatabase", objType);
builder.RegisterType(objTypeB).Named("AutofacDemo.Lib.Sql.SqlDatabase", objType); ////需要知道接口
//builder.RegisterType(objTypeA).Named<objType>("AutofacDemo.Lib.Sql.SqlDatabase");
//builder.RegisterType(objTypeB).Named<objType>("AutofacDemo.Lib.Oracle.OracleDatabase"); container = builder.Build(); }
return container;
}
Autofac的高级使用——Autofac.2.6.3.862的更多相关文章
- IoC之AutoFac(四)——AutoFac在MVC中的使用
阅读目录 Mvc中使用Autofac 第一步:在mvc中添加dll文件,可以通过Nuget直接添加 第二步:在App_Start文件夹中添加一个AutofacConfig类 第三步:在Global.a ...
- .net Core Autofac稍微高级一点的方法
前情摘要 前段时间写了autofac的注入但是每次都需要去修改startup这应该不是大家想要的. 至少不是我想要的. 网上有朋友说可以创建一个基础类来时间. 好了吹牛时间结束我们开始干点正事. 创建 ...
- [Solution] 使用Autofac在MVC、Web API、WCF中实现IOC
本来想聊一下面试过程的,1个星期面了6家,4家当场给offer,2家技术通过(1家没下文,1家复试).从中也学习到一些东西,先还是继续Coding吧. 官网:http://autofac.org/ 下 ...
- ASP.NET Core中使用IOC三部曲(二.采用Autofac来替换IOC容器,并实现属性注入)
前言 本文主要是详解一下在ASP.NET Core中,自带的IOC容器相关的使用方式和注入类型的生命周期. 这里就不详细的赘述IOC是什么 以及DI是什么了.. emm..不懂的可以自行百度. 目录 ...
- 一步一步创建ASP.NET MVC5程序[Repository+Autofac+Automapper+SqlSugar](七)
前言 大家好,我依旧是你们的老朋友Rector,很高兴又在周五的时候准时和大家见面. Rector的系列文章[一步一步创建ASP.NET MVC5程序[Repository+Autofac+Autom ...
- .NET手记-Autofac入门Getting Started
内容主要翻译自官方文档,原文请看:http://autofac.readthedocs.org/en/latest/getting-started/index.html#application-sta ...
- ASP.NET Core 整合Autofac和Castle实现自动AOP拦截
前言: 除了ASP.NETCore自带的IOC容器外,我们还可以使用其他成熟的DI框架,如Autofac,StructureMap等(笔者只用过Unity,Ninject和Castle). 1.ASP ...
- C#开发微信门户及应用(42)--使用Autofac实现微信接口处理的控制反转处理
在很多情况下,我们利用IOC控制反转可以很方便实现一些接口的适配处理,可以在需要的时候切换不同的接口实现,使用这种方式在调用的时候,只需要知道相应的接口接口,具体调用哪个实现类,可以在配置文件中动态指 ...
- Autofac
程序集准备 Assembly: Autofac/Autofac.Integration.Mvc/System.Web.Mvc/System.Web.Helpers/System.Web.WebPage ...
随机推荐
- Theano2.1.1-基础知识之准备工作
来源:http://deeplearning.net/software/theano/tutorial/index.html#tutorial 这里介绍的是使用theano的一些基础知识,虽然thea ...
- jquery的getjson与jsonp
仔细的学习jquery的getjson的用法. http://www.cnblogs.com/leejersey/p/3750232.html http://www.jb51.net/article/ ...
- BGP--边界网关协议
要全面了解BGP,首先我们要回答以下看上去很简单的问题:为什么需要BGP,也就是说BGP是如何产生的,它解决了什么问题.带着以上问题,我们先简单的回顾一个路由协议发展的轨迹. 首先路由的实质是描述一个 ...
- android之fragment的使用
android中的fragment与html中的div很类似,下图中通过左边的按键可以控制右边的显示内容.右边的内容就是一个fragment,通过点击按键来控制fragment的实现. 工程目录 需要 ...
- 页面切换语言包使用session不用cookie
cookie的问题,ifame中的cookie不一致 在父页面设置的语言包cookie,在iframe中获取不到.为什么呢? 为什么语言包这个事跟cookie过不去,有什么特殊的? iframe的sr ...
- T3 任职定级面试准备
山东大学计算机专业本科毕业,工作8年,以前在华为工作,来YY正好1年. 个人心态开放积极,对未知事物好奇心很强,前沿科学.古老宗教皆有涉猎.英语口语能力较强,能和老外流程的交流.技术涉猎广泛,喜好研究 ...
- mysql 注释
mysql> SELECT 1+1; # This comment continues to the end of line mysql> SELECT 1+1; -- This comm ...
- zabbix的安装
1 lamp环境搭建以及zabbix安装 方便的话使用yum方式(yum安装的是2.2版本) 安装epel环境 yum install -y epel-release 安装lamp环境 yum in ...
- Android消息机制之ThreadLocal的工作原理
来源: http://blog.csdn.net/singwhatiwanna/article/details/48350919 很多人认为Handler的作用是更新UI,这说的的确没错,但是更新UI ...
- phpwind9.0模板制作教程——制作论坛风格
由于论坛模板机制和门户等模板机制不同,所以今天我就先重点讲讲论坛模板制作的大概过程. 一.先来熟悉下phpwind9.0的论坛模板机制. 其实phpwind9.0的模板机制和discuzx2.5差不多 ...