.NET鎵嬭锛岮utofac鍏ラ棬Getting Started

鍐呭涓昏缈昏瘧鑷畼鏂规枃妗o紝鍘熸枃璇风湅锛�http://autofac.readthedocs.org/en/latest/getting-started/index.html#application-startup

灏咥utofac闆嗘垚杩涗綘鐨勫簲鐢ㄧ殑鍩烘湰妯″紡:

  • 鍦ㄨ剳娴蜂腑鏋勯�犲熀浜庢帶鍒跺弽杞紙IoC锛夌殑搴旂敤绋嬪簭鏋舵瀯
  • 娣诲姞Autofac寮曠敤.
  • 鍦ㄥ簲鐢ㄥ惎鍔ㄩ厤缃祦绋�...
  • 鍒涘缓涓�涓� ContainerBuilder.
  • 娉ㄥ唽缁勪欢锛坈omponents锛�.
  • build瀹氫箟鐨凜ontainerBuilder鐢熸垚Autofac瀹瑰櫒锛屽苟瀛樺偍瀹冧互渚涘悗缁娇鐢�.
  • 鍦ㄧ▼搴忚繍琛屾椂...
  • 浠嶢utofac瀹瑰櫒锛坈ontainer锛夊垱寤虹敓鍛藉懆鏈熷煙锛坙ifetime scope锛�.
  • 浣跨敤鐢熷懡鍛ㄦ湡鍩熸潵瑙f瀽鍑虹粍浠跺疄渚�.

杩欑瘒鎸囧灏嗕細浣跨敤涓�涓帶鍒跺彴绋嬪簭涓�姝ヤ竴姝ユ紨绀哄浣曚娇鐢ˋutofac. 涓�鏃︿綘鏈変簡鍩虹鐨勪簡瑙o紝鍙互鍒颁互涓媤iki绔欑偣鏌ョ湅鏇撮珮绾х殑浣跨敤鏂规硶鍜屾妧宸э細 integration information for WCF, ASP.NET, and other application types.

 

鏋勫缓搴旂敤鏋舵瀯

 

鍩轰簬IoC鐨勬兂娉曞簲璇ユ槸瀵硅薄鍦ㄨ鍒涘缓鐨勬椂鍊欙紝鐢变竴涓皟鎺х郴缁熷唴鎵�鏈夊璞$殑澶栫晫瀹炰綋灏嗗叾鎵�渚濊禆鐨勫璞$殑寮曠敤浼犻�掔粰瀹冿紝鑰屼笉鏄妸鎵�鏈夌被缁戝湪涓�璧凤紝姣忔閮借閫氳繃渚濊禆new鏉ヤ紶閫掑疄渚�.濡傛灉浣犳兂浜嗚В鏇村璇风湅锛�Martin Fowler has an excellent article explaining dependency injection/inversion of control.

瀵逛簬鎴戜滑鐨勭ず渚嬪簲鐢ㄧ▼搴忥紝鎴戜滑灏嗗畾涔変竴涓緭鍑哄綋鍓嶆棩鏈熺殑绫汇�備絾鏄紝鎴戜滑涓嶅笇鏈涘畠鍜屾帶鍒跺彴缁戝湪涓�璧凤紝鍥犱负鎴戜滑甯屾湜浠ュ悗鑳藉娴嬭瘯瀹冩垨鐢ㄥ湪涓�涓病鏈夋帶鍒跺彴绋嬪簭鐨勫湴鏂广��
鎴戜滑杩樹細鍘诲敖閲忔娊璞¤緭鍑烘棩鏈熺殑鏈哄埗锛屼互渚夸簬鎴戜滑鍚庣画閲嶅浣跨敤銆�
鎴戜滑浼氬仛杩欐牱鐨勪簨鎯咃細

using System;

namespace DemoApp
{
  // This interface helps decouple the concept of
  // "writing output" from the Console class. We
  // don't really "care" how the Write operation
  // happens, just that we can write.
  public interface IOutput
  {
    void Write(string content);
  }

  // This implementation of the IOutput interface
  // is actually how we write to the Console. Technically
  // we could also implement IOutput to write to Debug
  // or Trace... or anywhere else.
  public class ConsoleOutput : IOutput
  {
    public void Write(string content)
    {
      Console.WriteLine(content);
    }
  }

  // This interface decouples the notion of writing
  // a date from the actual mechanism that performs
  // the writing. Like with IOutput, the process
  // is abstracted behind an interface.
  public interface IDateWriter
  {
    void WriteDate();
  }

  // This TodayWriter is where it all comes together.
  // Notice it takes a constructor parameter of type
  // IOutput - that lets the writer write to anywhere
  // based on the implementation. Further, it implements
  // WriteDate such that today's date is written out;
  // you could have one that writes in a different format
  // or a different date.
  public class TodayWriter : IDateWriter
  {
    private IOutput _output;
    public TodayWriter(IOutput output)
    {
      this._output = output;
    }

    public void WriteDate()
    {
      this._output.Write(DateTime.Today.ToShortDateString());
    }
  }
}

 

娣诲姞Autofac寮曠敤

 

棣栧厛娣诲姞Autofac寮曠敤鍒颁綘鐨勯」鐩紝杩欎釜渚嬪瓙涓垜浠粎娣诲姞Autofac Core寮曠敤锛屽叾浠栫被鍨嬪簲鐢ㄧ▼搴忓彲鑳戒細鐢ㄥ埌涓嶅悓鐨凙utofac.Integration绫诲簱銆�

閫氳繃NuGet鍙互寰堝鏄撲负椤圭洰寮曞叆寮曠敤锛屽鍥撅細

 

.NET鎵嬭锛岮utofac鍏ラ棬Getting Started_第1张图片

 

搴旂敤鍚姩锛圓pplication Startup锛�

 

鍦焾猴笍鍚姩鏃讹紝浣犻渶瑕佸垱寤轰竴涓狢ontainerBuilder骞朵娇鐢ㄥ畠娉ㄥ唽鍚勭缁勪欢. 缁勪欢鍙互鏄〃杈惧紡銆�.NET绫诲瀷鎴栬�呬竴浜涚敤浜庢毚闇插唴閮ㄦ湇鍔$殑浠g爜銆�

绠�鍗曠殑鏉ヨ锛屾湁涓疄鐜颁簡鎺ュ彛鐨勭被鍨嬶紝濡備笅锛�

 

public class SomeType : IService
{
}

 

浣犲彲浠ラ�氳繃涓ょ鏂瑰紡瀹氫綅鍒板畠锛氶�氳繃绫诲瀷SomeType锛屾垨鑰呴�氳繃鎺ュ彛IService.

鍦ㄨ繖涓緥瀛愪腑锛岀粍浠舵槸SomeType锛屽悓鏃舵毚闇茬殑鏈嶅姟鏄寚SomeType绫诲瀷鍜孖Service鎺ュ彛銆�

鍦ˋutofac涓紝浣犲簲璇ラ�氳繃ContainerBuilder鏉ユ敞鍐屽畠浠��

 

// Create your builder.
var builder = new ContainerBuilder();

// Usually you're only interested in exposing the type
// via its interface:
builder.RegisterType<SomeType>().As<IService>();

// However, if you want BOTH services (not as common)
// you can say so:
builder.RegisterType<SomeType>().AsSelf().As<IService>();

 

瀵逛簬鎴戜滑鐨勭ず渚嬪簲鐢ㄧ▼搴忥紝鎴戜滑闇�瑕佹敞鍐屾垜浠墍鏈夌殑缁勪欢锛堢被锛夛紝骞舵毚闇蹭粬浠殑鏈嶅姟锛堟帴鍙o級銆�

鎴戜滑杩橀渶瑕佸瓨鍌ㄥ鍣紝杩欐牱瀹冧箣鍚庡彲鐢ㄤ簬瑙f瀽鍑哄悇绉嶇被鍨嬪疄渚嬨��

 

using System;
using Autofac;

namespace DemoApp
{
  public class Program
  {
    private static IContainer Container { get; set; }

    static void Main(string[] args)
    {
      var builder = new ContainerBuilder();
      builder.RegisterType<ConsoleOutput>().As<IOutput>();
      builder.RegisterType<TodayWriter>().As<IDateWriter>();
      Container = builder.Build();

      // The WriteDate method is where we'll make use
      // of our dependency injection. We'll define that
      // in a bit.
      WriteDate();
    }
  }
}

 

杩欐牱鎴戜滑灏辨嫢鏈変簡涓�涓敞鍐屼簡鎵�鏈夌粍浠剁殑瀹瑰櫒锛岃繖浜涚粍浠跺悓鏃舵毚闇蹭簡瀹冧滑鐨勬湇鍔°�傜幇鍦ㄨ鎴戜滑鏉ヤ娇鐢ㄨ繖涓鍣ㄣ��

 

搴旂敤杩愯鏃�

 

鍦ㄥ簲鐢ㄨ繍琛屾湡闂达紝浣犻渶瑕佷粠瀹瑰櫒鐢熷懡鍛ㄦ湡鍩熶腑瑙f瀽鍑虹粍浠跺疄渚嬫潵浣跨敤瀹冧滑銆傚鍣ㄨ嚜韬氨鏄竴涓敓鍛藉懆鏈熷煙锛坙ifetime scope锛夛紝鎶�鏈笂鏉ヨ锛屼綘鍙互鐩存帴浠庡鍣ㄤ腑瑙f瀽鍑洪渶瑕佺殑缁勪欢銆�

浣嗘槸鎴戜滑涓嶆帹鑽愮洿鎺ユ搷浣滃鍣紝杩欎細瀵艰嚧鍐呭瓨娉勬紡銆�

褰撴垜浠В鏋愬嚭涓�涓粍浠舵椂锛屼緷璧栦簬鎴戜滑瀹氫箟鐨刲ifetime scope锛屼竴涓柊鐨勫璞″疄渚嬩細琚垱寤恒�傚叾涓竴浜涚粍浠跺彲鑳介渶瑕佽閲婃斁锛屼緥濡傚疄鐜癐Dispose鎺ュ彛鐨勫璞°�傚綋鐢熷懡鍛ㄦ湡鍩熻閲婃斁鏃讹紝Autofac鍙互鑷姩澶勭悊缁勪欢璧勬簮鐨勯噴鏀俱��

浣嗘槸瀹瑰櫒瀛樺湪浜庢暣涓簲鐢ㄧ▼搴忕敓鍛藉懆鏈熴�傚鏋滀綘鐩存帴浠庡鍣ㄤ腑瑙f瀽鍑哄ぇ閲忕粍浠讹紝鍙兘浼氬鑷村緢澶氱粍浠朵竴鐩寸瓑寰呰閲婃斁銆傝繖鏍峰仛娉曞苟涓嶅ソ锛岄�氬父浼氬鑷村唴瀛樻硠闇层��

鏇夸唬鏂规鏄紝姣忔鎴戜滑浠庡鍣ㄦ潵鍒涘缓瀛愮敓鍛藉懆鏈熷煙锛屻�傚綋浣犱粠瀛愬煙瀹屾垚瑙f瀽缁勪欢鍚庯紝瀛愬煙灏嗕細琚噴鏀撅紝鍚屾椂鎵�鏈夎祫婧愰兘浼氳娓呯悊鎺夈��

瀵逛簬鎴戜滑鐨勭ず渚嬬▼搴忔潵璇达紝鎴戜滑灏嗕細浠庡瓙鍩熶腑瑙f瀽鍑篒DateWriter瀹炰緥锛屽苟杩愯WriterDate()鏂规硶銆傚悓鏃跺湪鎴戜滑瀹屾垚鍚庯紝瀛愬煙灏嗕細琚噴鏀俱��

 

namespace DemoApp
{
  public class Program
  {
    private static IContainer Container { get; set; }

    static void Main(string[] args)
    {
      // ...the stuff you saw earlier...
    }

    public static void WriteDate()
    {
      // Create the scope, resolve your IDateWriter,
      // use it, then dispose of the scope.
      using (var scope = Container.BeginLifetimeScope())
      {
        var writer = scope.Resolve<IDateWriter>();
        writer.WriteDate();
      }
    }
  }
}

 

鐜板湪褰撲綘杩愯浣犵殑绋嬪簭鏃�...

  • WriteDate()鏂规硶浠嶢utofac涓姹侷DateWriter瀹炰緥.
  • Autofac鍙戠幇IDateWriter鍙互鏄犲皠鍒癟odayWriter绫诲瀷锛屾墍浠ュ垱寤轰簡涓�涓猅odayWriter瀹炰緥.
  • Autofac鍙戠幇TodayWriter鍦ㄥ畠鐨勬瀯閫犲嚱鏁颁腑闇�瑕佷竴涓�IOutput瀹炰緥.
  • Autofac鍙戠幇IOutput 鍙互鏄犲皠鍒�ConsoleOutput锛屾墍浠ュ垱寤轰簡涓�涓�ConsoleOutput鏂板疄渚�.
  • Autofac浣跨敤ConsoleOutput瀹炰緥瀹屾垚鏋勯�犱竴涓�TodayWriter瀹炰緥.
  • Autofac杩斿洖浜嗕竴涓畬鍏ㄦ瀯閫犲ソ鐨�TodayWriter瀹炰緥鏉ョ敤浜�“WriteDate” 鏂规硶.

涔嬪悗锛屽鏋滀綘鐨勭▼搴忔兂瑕佸疄鐜拌緭鍑轰笉鍚屾棩鏈�, 浣犲彲浠ユ瀯閫犱笉鍚屽疄鐜�IDateWriter鎺ュ彛鐨勭被鍨�锛屽悓鏃跺湪搴旂敤鍚姩鏃舵敞鍐�. 浣犱笉蹇呮敼鍙樺叾浠栦换浣曠殑绫�. 杩欏氨鏄帶鍒跺弽杞�!

 

娉ㄦ剰锛氭甯告潵璇磗ervice location寰堝ぇ绋嬪害涓婅璁や负鏄弽妯″紡銆備篃灏辨槸璇达紝鍒板鎵嬪姩鍒涘缓鍩燂紙scope锛夊拰澶ч噺浣跨敤瀹瑰櫒涓嶄竴瀹氭槸鏈�濂界殑鏂规銆備娇鐢ˋutofac浣犻�氬父涓嶅繀鍋氭垜浠湪绀轰緥搴旂敤涓殑鍋氱殑浜嬫儏銆傜粍浠朵細浠庝竴涓簲鐢ㄧ殑涓績鎴栬�呴《灞傛潵瑙f瀽锛屾墜鍐欑殑瑙e喅鏂规寰堢綍瑙佺殑銆傚綋鐒讹紝濡備綍璁捐搴旂敤绋嬪簭鏄敱浣犺嚜宸卞喅瀹氱殑銆�

你可能感兴趣的:(.NET鎵嬭锛岮utofac鍏ラ棬Getting Started)