鍐呭涓昏缈昏瘧鑷畼鏂规枃妗o紝鍘熸枃璇风湅锛�http://autofac.readthedocs.org/en/latest/getting-started/index.html#application-startup
灏咥utofac闆嗘垚杩涗綘鐨勫簲鐢ㄧ殑鍩烘湰妯″紡:
杩欑瘒鎸囧灏嗕細浣跨敤涓�涓帶鍒跺彴绋嬪簭涓�姝ヤ竴姝ユ紨绀哄浣曚娇鐢ˋ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鍙互寰堝鏄撲负椤圭洰寮曞叆寮曠敤锛屽鍥撅細
搴旂敤鍚姩锛圓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(); } } } }
鐜板湪褰撲綘杩愯浣犵殑绋嬪簭鏃�...
涔嬪悗锛屽鏋滀綘鐨勭▼搴忔兂瑕佸疄鐜拌緭鍑轰笉鍚屾棩鏈�, 浣犲彲浠ユ瀯閫犱笉鍚屽疄鐜�IDateWriter鎺ュ彛鐨勭被鍨�锛屽悓鏃跺湪搴旂敤鍚姩鏃舵敞鍐�. 浣犱笉蹇呮敼鍙樺叾浠栦换浣曠殑绫�. 杩欏氨鏄帶鍒跺弽杞�!
娉ㄦ剰锛氭甯告潵璇磗ervice location寰堝ぇ绋嬪害涓婅璁や负鏄弽妯″紡銆備篃灏辨槸璇达紝鍒板鎵嬪姩鍒涘缓鍩燂紙scope锛夊拰澶ч噺浣跨敤瀹瑰櫒涓嶄竴瀹氭槸鏈�濂界殑鏂规銆備娇鐢ˋutofac浣犻�氬父涓嶅繀鍋氭垜浠湪绀轰緥搴旂敤涓殑鍋氱殑浜嬫儏銆傜粍浠朵細浠庝竴涓簲鐢ㄧ殑涓績鎴栬�呴《灞傛潵瑙f瀽锛屾墜鍐欑殑瑙e喅鏂规寰堢綍瑙佺殑銆傚綋鐒讹紝濡備綍璁捐搴旂敤绋嬪簭鏄敱浣犺嚜宸卞喅瀹氱殑銆�