簡體   English   中英

如何在創建所有者生命周期范圍時自動激活Autofac組件?

[英]How to auto activate Autofac component on creating of Owner lifetimescope?

想象一下我有以下注冊:

builder.RegisterType<ContactsManager>().InstancePerOwned<IDialPad>();

當我創建Owned<IDialpad> ,IDialPad范圍內沒有解析的類依賴於ContactsManager,因此不會創建ContactsManager。 但是我仍然希望在作用域創建時創建它。

當我這樣做時:

builder.RegisterType<ContactsManager>().InstancePerOwned<IDialPad>().AutoActivate();

它不是在作用域創建時激活的,而是在Autofac容器構建時激活的,當然它無法激活。

我知道,如果我向ContactsManager甚至偽造的依賴項添加到范圍內的任何類,那么將自動創建ContactsManager。 但這不是我想要的。

我可以看到很多方法來做你想要的。

  1. 如果可以修改IDialpad注冊,則可以使用OnActivated事件。

     builder.RegisterType<Foo>() .As<IFoo>() .InstancePerOwned<IBar>(); builder.RegisterType<Bar>() .As<IBar>() .OnActivated(e => e.Context.Resolve<IFoo>()); 
  2. 如果您無權訪問此注冊,則可以執行將執行相同操作的模塊。

     class TestModule : Module { protected override void AttachToComponentRegistration( IComponentRegistry componentRegistry, IComponentRegistration registration) { if (registration.Services .Any(s => s is IServiceWithType && ((IServiceWithType)s).ServiceType == typeof(IBar))) { registration.Activated += (sender, e) => { e.Context.Resolve<IFoo>(); }; } } } 
  3. 根據以前的解決方案,您可以提供一種新的服務類型,該服務類型將指示Autofac在激活另一個服務時激活指定的服務。

    最終注冊將如下所示:

      ContainerBuilder builder = new ContainerBuilder(); builder.RegisterModule(new AutoActivateModule()); builder.RegisterType<Foo>() .As<IFoo>() .As(AutoActivateService.From<IBar>()) .InstancePerOwned<IBar>(); builder.RegisterType<Bar>() .As<IBar>(); 

    AutoActivateServiceAutoActivate代碼如下:

     public class AutoActivateModule : Module { protected override void AttachToComponentRegistration( IComponentRegistry componentRegistry, IComponentRegistration registration) { foreach (IServiceWithType typedService in registration.Services.OfType<IServiceWithType>()) { registration.Activated += (sender, e) => { Service autoActivateService = AutoActivateService.From(typedService.ServiceType); foreach (IComponentRegistration r in componentRegistry.RegistrationsFor(autoActivateService)) { e.Context.ResolveComponent(r, new Parameter[0]); } }; } } } public class AutoActivateService : Service, IEquatable<AutoActivateService> { public static AutoActivateService From<T>() { return new AutoActivateService(typeof(T)); } public static AutoActivateService From(Type targetType) { return new AutoActivateService(targetType); } private AutoActivateService(Type targetType) { this._targetType = targetType; } private readonly Type _targetType; public override String Description { get { return this.ToString(); } } public Type TargetType { get { return this._targetType; } } public Boolean Equals(AutoActivateService other) { return other != null && this._targetType == other._targetType; } public override Boolean Equals(Object obj) { return this.Equals(obj as AutoActivateService); } public override Int32 GetHashCode() { return this._targetType.GetHashCode(); } public override String ToString() { return String.Format("Autoactivate service for {0}", this._targetType); } } 
  4. 您也可以使用BeginLifetimeScope事件。

    激活Owned<T>服務ILifetimeScope將創建一個新的ILifetimeScope ,其標記將為關聯的服務。

     container.ChildLifetimeScopeBeginning += ChildLifetimeScopeBeginning; // ... private static void ChildLifetimeScopeBeginning( Object sender, LifetimeScopeBeginningEventArgs e) { e.LifetimeScope.ChildLifetimeScopeBeginning += ChildLifetimeScopeBeginning; IServiceWithType typedService = e.LifetimeScope.Tag as IServiceWithType; if (typedService != null && typedService.ServiceType == typeof(IBar)) { e.LifetimeScope.Resolve<IFoo>(); } } 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM