本来想聊一下面试过程的,1个星期面了6家,4家当场给offer,2家技术通过(1家没下文,1家复试).从中也学习到一些东西,先还是继续Coding吧.
官网:http://autofac.org/
下载:Install-Package Autofac -Version 3.5.2
Autofac是轻量级的开源IOC容器,传说是速度最快的一个,同类的框架还有用过Castle Windsor、StructureMap、Unity、Spring.Net等.
本节目录
Hello World
1.建立项目
CA:一个控制台项目,引用Nuget Autofac包.
2.建立一个依赖外部的类
public class Test { public Test(string str) { Console.WriteLine("hello " + str); } }
3.根据参数名直接注入
static void Main(string[] args) {
//创建容器工厂 var builder = new ContainerBuilder(); //注册Test类型到工厂中 builder.RegisterType<Test>();
//通过工厂创建容器 using (var container = builder.Build()) {
//解析Test并返回Test实例 container.Resolve<Test>(new NamedParameter("str", "world")); } Console.ReadKey(); }
过程:首先需要创建容器工厂,然后将各种类型注册到工厂中,再创建容器,通过容器实例化对象.
注入高级对象
添加1个构造函数
public Test(ISay say) { Console.WriteLine("say:" + say.Get()); }
接口及其实现
public class Say : ISay { public string Get() { return "hello world"; } } public interface ISay { string Get(); }
注入实现(Autofac会自动装配)
static void Main(string[] args) { var builder = new ContainerBuilder(); builder.RegisterType<Test>(); builder.RegisterType<Say>().As<ISay>(); using (var container = builder.Build()) { container.Resolve<Test>(); } Console.ReadKey(); }
略作修改
public Test(Say say)//将接口改为指定具体实现类 { Console.WriteLine("say:" + say.Get()); }
此时运行代码会报错,因为在容器中我们只注册了ISay接口.改为As<Say>或以下方式
builder.RegisterType<Say>().As<ISay>().AsSelf();
对象生命周期
注册到工厂的类型默认都调用InstancePerDependency方法.实现每个依赖都创建1个新实例
单例模式:
builder.RegisterType<Test>().SingleInstance();
读取配置文件注入
需要引用Nuget Autofac.Configuration包
注册类型代码
builder.RegisterModule(new ConfigurationSettingsReader("autofac"));
App.config
<configSections> <section name="autofac" type="Autofac.Configuration.SectionHandler, Autofac.Configuration" /> </configSections> <autofac defaultAssembly="CA"> <components> <component type="CA.Say, CA" service="CA.ISay" /> <component type="CA.Say, CA" service="CA.Say" /> </components> </autofac>
service相当于As<T>中的T
结合ASP.NET MVC
1.Install-Package Autofac.Mvc5
2.Application_Start事件添加此方法
protected void Application_Start(object sender, EventArgs e) { #region 1.MVC Autofac注入 var builder = new ContainerBuilder(); //类名.EndsWith("Controller") builder.RegisterControllers(Assembly.GetExecutingAssembly()); //这样既支持接口 又支持自己的类型 builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()) .AsImplementedInterfaces().AsSelf(); //容器 var container = builder.Build(); //注入改为Autofac注入 DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); #endregion #region 2.注册MVC路由 var routes = RouteTable.Routes; routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); #endregion }
3.带参数的控制器构造函数
public class HomeController : Controller { public HomeController(Say say) { } public string Index() { return "Hello World"; } } public class Say { public string Get() { return "Hello World"; } }
4.浏览器访问~/
结合ASP.NET Web API
1.Install-Package Autofac.WebApi
2.由于WebAPI需要,引用系统自带Web.Http和Web.Http.WebHost
3.Application_Start添加此方法
protected void Application_Start(object sender, EventArgs e) { var configuration = GlobalConfiguration.Configuration; #region Autofac WebAPI注入 var builder = new ContainerBuilder(); builder.RegisterApiControllers(Assembly.GetExecutingAssembly()); builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()).AsImplementedInterfaces().AsSelf(); var container = builder.Build(); configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container); #endregion #region 注册Web API路由 configuration.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional }); #endregion }
4.带参数的控制器构造函数
public class ValuesController : ApiController { public ValuesController(Say say) { } public string Get() { return "Hello World"; } } public class Say { public string Get() { return "Hello World"; } }
5.浏览器访问~/api/values
结合WCF
1.Install-Package Autofac.Wcf
2.Application_Start添加此方法
protected void Application_Start(object sender, EventArgs e) { #region WCF Autofac注入 var builder = new ContainerBuilder(); builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()).AsImplementedInterfaces().AsSelf(); var container = builder.Build(); //WCF IOC容器 AutofacHostFactory.Container = container; #endregion }
3.新建svc服务
public class TestService : ITestService { public TestService(Say say) { } public string DoWork() { return "Hello World"; } } public class Say { public string Get() { return "Hello World"; } } [ServiceContract] public interface ITestService { [OperationContract] string DoWork(); }
4.svc指令中加一句:
Factory="Autofac.Integration.Wcf.AutofacServiceHostFactory, Autofac.Integration.Wcf"
5.wcftestclient
惭愧,好久没发博客了