1、新建asp.net mvc项目,安装Autofac。
【工具】---->【库程序包管理器】---->【程序包管理器控制台】
Install-package autofac (-version 3.5.2)
2、要使用配置文件还需安装Autofac.Configuration
install-package Autofac.Configuration
3、新建项目BUS,接口:IOutput,两个实现:PcWebOutput,CellPhoneOutput,一个使用类:OutputBase
namespace Bus
{
public interface IOutput
{
string Bold(string content);
}
public class PcWebOutput : IOutput
{
public string Bold(string content)
{
return string.Format("<b class=\"pcBlod\">{0}</b>", content);
}
}
public class CellPhoneOutput : IOutput
{
public string Bold(string content)
{
return string.Format("<b class=\"cellPhoneBlod\">{0}</b>", content);
}
}
public class OutputBase
{
public OutputBase(IOutput iOutput)
{
_iOutput = iOutput;
}
public string WriteBold(string content)
{
return _iOutput.Bold(content);
}
public IOutput _iOutput;
}
}
4、在HomeController的Index中使用autofac
public ActionResult Index()
{
ViewBag.Message = "第一个学习示例.";
var builder = new ContainerBuilder();
// 这里通过ContainerBuilder方法RegisterType对OutputBase进行注册,注册的类型在相应得到的容器中可以Resolve你的OutputBase实例。
// RegisterType是以注册类型;Register是以Lampda方式进行注册;RegisterInstance是注册对象实例。
builder.RegisterType<OutputBase>();
// builder.RegisterType<PcWebOutput>().As<IOutput>();通过AS可以让OutputBase类中通过构造函数依赖注入类型相应的接口。
builder.RegisterType<PcWebOutput>().As<IOutput>();
//Build()方法生成一个对应的Container实例,这样,就可以通过Resolve解析到注册的类型实例。
using (var container = builder.Build())
{
var mgr = container.Resolve<OutputBase>();
ViewBag.Test = mgr.WriteBold("autofac 测试!");
}
var builder2 = new ContainerBuilder();
builder2.RegisterType<OutputBase>();
builder2.RegisterModule(new ConfigurationSettingsReader("autofac"));
using(var container2 = builder2.Build())
{
var mgr = container2.Resolve<OutputBase>();
ViewBag.Test2 = mgr.WriteBold("autofac 测试model注册!");
}
var builder3 = new ContainerBuilder();
builder3.RegisterModule(new ConfigurationSettingsReader("autofac"));
builder3.Register(c => new OutputBase(c.Resolve<IOutput>()));
using (var container3 = builder3.Build())
{
var manager = container3.Resolve<OutputBase>();
ViewBag.Test3 = manager.WriteBold("autofac 测试Register注册!");
}
return View();
}
页面:
@{
ViewBag.Title = "autofac demo";
}
@section featured {
<section class="featured">
<div class="content-wrapper">
<hgroup class="title">
<h1>@ViewBag.Title</h1>
<h2>@ViewBag.Message</h2>
</hgroup>
</div>
</section>
}
<h3>AutoFac的第一个demo:</h3>
<div>
<span>1:</span>
@Html.Raw(ViewBag.Test)
</div>
<div>
<span>2:</span>
@Html.Raw(ViewBag.Test2)
</div>
<div>
<span>3:</span>
@Html.Raw(ViewBag.Test3)
</div>
配置文件:
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<section name="autofac" type="Autofac.Configuration.SectionHandler, Autofac.Configuration" />
</configSections>
<autofac defaultAssembly="Bus">
<components>
<component type="Bus.CellPhoneOutput, Bus" service="Bus.IOutput" />
</components>
</autofac>
结果:
第一个解析的类型是PcWebOutput,第2、3则是通过配置文件<component type="Bus.CellPhoneOutput, Bus" service="Bus.IOutput" />注册了CellPhoneOutput类型。
先贴出demo下载地址:
http://download.youkuaiyun.com/download/xue251248603/9440703