日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Autofac 依赖注入框架

發(fā)布時(shí)間:2025/3/15 编程问答 14 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Autofac 依赖注入框架 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Autofac是一款I(lǐng)OC框架,比較于其他的IOC框架,如Spring.NET,Unity,Castle等等所包含的,它很輕量級(jí)性能上非常高。

官方網(wǎng)站http://autofac.org/

優(yōu)點(diǎn):依賴注入不是目的,它是一系列工具和手段,最終的目的是幫助我們開發(fā)出松散耦合(loose coupled)、可維護(hù)、可測試的代碼和程序。這條原則的做法是大家熟知的面向接口,或者說是面向抽象編程。

IOC理解:假如我們要干一個(gè)工程,需要10個(gè)人,傳統(tǒng)方法就是項(xiàng)目開始,這10個(gè)人都必須在場,捆綁在一起;而IOC就是項(xiàng)目開始的時(shí)候,在這里報(bào)個(gè)名,項(xiàng)目進(jìn)程中,需要誰的時(shí)候,再讓誰過來,減輕了他們之間的耦合,可以把IOC理解為一個(gè)類的管理器,項(xiàng)目啟動(dòng)的時(shí)候,注冊(cè)所有的類,然后再根據(jù)實(shí)際需要,分別實(shí)例化

方法一:注冊(cè)組件,Register Components

var builder = new ContainerBuilder();

// Register individual components
builder.RegisterInstance(new TaskRepository()).As<ITaskRepository>();
builder.RegisterType<TaskController>();
builder.Register(c => new LogManager(DateTime.Now)).As<ILogger>();

// Scan an assembly for components
builder.RegisterAssemblyTypes(myAssembly).Where(t => t.Name.EndsWith("Repository")).AsImplementedInterfaces();

var container = builder.Build();

方法二:表達(dá)的相關(guān)性Express Dependencies

public class TaskController
{
  private ITaskRepository _repository;
  private ILogger _logger;

  // Autofac will automatically find the registered
  // values and pass them in for you.
  public TaskController(ITaskRepository repository,?ILogger logger)
  {
    this._repository = repository;
    this._logger = logger;
  }
}

注:可以自動(dòng)實(shí)現(xiàn)ILogger 等的構(gòu)造初始化,當(dāng)一個(gè)類被調(diào)用AutoFac會(huì)自動(dòng)掃描這個(gè)類的構(gòu)造函數(shù),如果發(fā)現(xiàn)構(gòu)造函數(shù)的參數(shù)類型和AutoFac一致,他就會(huì)自動(dòng)取出該對(duì)象,并將該對(duì)象注入到當(dāng)前類中,這過程就叫注入。

方法三:MVC | 依賴注入 AutoFac (構(gòu)造函數(shù)注入)

var builder = new ContainerBuilder();
builder.RegisterControllers(System.Reflection.Assembly.GetExecutingAssembly());?//注冊(cè)mvc容器的實(shí)現(xiàn)
var container = builder.Build();??//Build()方法是表示:創(chuàng)建一個(gè)容器?
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));?//注冊(cè)MVC容器?

創(chuàng)建實(shí)例方法:

1、InstancePerDependency
對(duì)每一個(gè)依賴或每一次調(diào)用創(chuàng)建一個(gè)新的唯一的實(shí)例。這也是默認(rèn)的創(chuàng)建實(shí)例的方式。

官方文檔解釋:Configure the component so that every dependent component or call to Resolve() gets a new, unique instance (default.)

2、InstancePerLifetimeScope
在一個(gè)生命周期域中,每一個(gè)依賴或調(diào)用創(chuàng)建一個(gè)單一的共享的實(shí)例,且每一個(gè)不同的生命周期域,實(shí)例是唯一的,不共享的。

官方文檔解釋:Configure the component so that every dependent component or call to Resolve() within a single ILifetimeScope gets the same, shared instance. Dependent components in different lifetime scopes will get different instances.

3、InstancePerMatchingLifetimeScope
在一個(gè)做標(biāo)識(shí)的生命周期域中,每一個(gè)依賴或調(diào)用創(chuàng)建一個(gè)單一的共享的實(shí)例。打了標(biāo)識(shí)了的生命周期域中的子標(biāo)識(shí)域中可以共享父級(jí)域中的實(shí)例。若在整個(gè)繼承層次中沒有找到打標(biāo)識(shí)的生命周期域,則會(huì)拋出異常:DependencyResolutionException。

官方文檔解釋:Configure the component so that every dependent component or call to Resolve() within a ILifetimeScope tagged with any of the provided tags value gets the same, shared instance. Dependent components in lifetime scopes that are children of the tagged scope will share the parent's instance. If no appropriately tagged scope can be found in the hierarchy an DependencyResolutionException is thrown.

4、InstancePerOwned
在一個(gè)生命周期域中所擁有的實(shí)例創(chuàng)建的生命周期中,每一個(gè)依賴組件或調(diào)用Resolve()方法創(chuàng)建一個(gè)單一的共享的實(shí)例,并且子生命周期域共享父生命周期域中的實(shí)例。若在繼承層級(jí)中沒有發(fā)現(xiàn)合適的擁有子實(shí)例的生命周期域,則拋出異常:DependencyResolutionException。

官方文檔解釋:

Configure the component so that every dependent component or call to Resolve() within a ILifetimeScope created by an owned instance gets the same, shared instance. Dependent components in lifetime scopes that are children of the owned instance scope will share the parent's instance. If no appropriate owned instance scope can be found in the hierarchy an DependencyResolutionException is thrown.

5、SingleInstance
每一次依賴組件或調(diào)用Resolve()方法都會(huì)得到一個(gè)相同的共享的實(shí)例。其實(shí)就是單例模式。

官方文檔解釋:Configure the component so that every dependent component or call to Resolve() gets the same, shared instance.

6、InstancePerHttpRequest
在一次Http請(qǐng)求上下文中,共享一個(gè)組件實(shí)例。僅適用于asp.net mvc開發(fā)。

參考鏈接:

autofac 創(chuàng)建實(shí)例方法總結(jié):http://www.cnblogs.com/manglu/p/4115128.html

AutoFac使用方法總結(jié):Part I:http://niuyi.github.io/blog/2012/04/06/autofac-by-unit-test/

轉(zhuǎn)載于:https://www.cnblogs.com/honzhez/p/6344437.html

總結(jié)

以上是生活随笔為你收集整理的Autofac 依赖注入框架的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。