Ioc容器Autofac介绍
2012-08-22 15:29
363 查看
Autofac是轻量级的开源Ioc容器,在这里可以下载http://code.google.com/p/autofac/。如果你用过其他的Ioc容器,那么学习Autofac使用也会比较容易,下面将通过一些例子来讲解其用法。
先看一个例子:
首先新建一个工程,添加Autofac引用。
准备代码,和之前的一样
接下来就是获取对象的实例并调用
调用配置文件注册组件
public static IContainer GetContainers()
{
var builder = new ContainerBuilder();
// builder.Register<IDal>(c => new OracleDal()).SingleInstance();
builder.RegisterModule(new ConfigurationSettingsReader("autofac"));
return builder.Build() ;
}
先看一个例子:
首先新建一个工程,添加Autofac引用。
准备代码,和之前的一样
class DataFactory { public static IContainer GetContainers() { var builder = new ContainerBuilder(); builder.Register<IDal>(c => new OracleDal()).SingleInstance(); return builder.Build() ; } }
接下来就是获取对象的实例并调用
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="autofac" type="Autofac.Configuration.SectionHandler, Autofac.Configuration"/> </configSections> <autofac> <components> <component type="AutofacDemo.OracleDal, AutofacDemo" service="AutofacDemo.IDal, AutofacDemo" /> </components> </autofac> </configuration>
调用配置文件注册组件
public static IContainer GetContainers()
{
var builder = new ContainerBuilder();
// builder.Register<IDal>(c => new OracleDal()).SingleInstance();
builder.RegisterModule(new ConfigurationSettingsReader("autofac"));
return builder.Build() ;
}
相关文章推荐
- Ioc容器Autofac介绍
- Ioc容器Autofac介绍
- IoC容器Autofac
- .net core2.0下Ioc容器Autofac使用
- IoC容器Autofac正篇之简单实例
- 注册接口使用StructureMap和Autofac等Ioc容器注册接口
- Ioc容器-Autofac 之四-依赖注入框架Autofac的简单使用
- Ioc容器Autofac系列(3)-- 三种注册组件的方式
- IoC容器Autofac
- IoC容器Autofac
- Spring框架介绍和IoC容器中Bean配置(Spring框架)
- Ioc容器Autofac系列(1)-- 初窥
- .NET开源项目介绍及资源推荐:IOC容器
- .NET领域最为流行的IOC框架之一Autofac WebAPI2使用Autofac实现IOC属性注入完美解决方案 AutoFac容器初步
- IoC容器1——Spring IoC容器 和 bean 介绍
- IoC容器Autofac(3) - 理解Autofac原理,我实现的部分Autofac功能(附源码)
- IoC容器Autofac之实例引入(一)
- [置顶] Spring框架介绍和IoC容器中Bean配置(Spring框架)
- Ioc容器Autofac系列(1)-- 初窥
- Ioc容器Autofac系列