Custom object factory extension for Unity

后端 未结 4 1258
你的背包
你的背包 2021-01-02 06:05

I am using the Unity IoC container, and I need to intercept any calls to Resolve for a certain base interface, and run my own custom code to construct those types.

相关标签:
4条回答
  • 2021-01-02 06:27

    For completeness, I should add another answer that works under Unity 2, since my other answer no longer works. It is slightly more involved since you need to make a custom builder policy. Thanks to ctavares from the Unity project who provided lots of help on this thread in implementing this:

    public class FactoryUnityExtension : UnityContainerExtension
    {
        private ICustomFactory factory;
        private CustomFactoryBuildStrategy strategy;
    
        public FactoryUnityExtension(ICustomFactory factory)
        {
            this.factory = factory;
        }
    
        protected override void Initialize()
        {
            this.strategy = new CustomFactoryBuildStrategy(factory, Context);
            Context.Strategies.Add(strategy, UnityBuildStage.PreCreation);
            Context.Policies.Set<ParentMarkerPolicy>(new ParentMarkerPolicy(Context.Lifetime), new NamedTypeBuildKey<ParentMarkerPolicy>());
        }
    }
    
    public class ParentMarkerPolicy : IBuilderPolicy
    {
        private ILifetimeContainer lifetime;
    
        public ParentMarkerPolicy(ILifetimeContainer lifetime)
        {
            this.lifetime = lifetime;
        }
    
        public void AddToLifetime(object o)
        {
            lifetime.Add(o);
        }
    }
    
    public interface ICustomFactory
    {
        object Create(Type t);
        bool CanCreate(Type t);
    }
    
    public class CustomFactoryBuildStrategy : BuilderStrategy
    {
        private ExtensionContext baseContext;
        private ICustomFactory factory;
    
    
        public CustomFactoryBuildStrategy(ICustomFactory factory, ExtensionContext baseContext)
        {
            this.factory = factory;
            this.baseContext = baseContext;
        }
    
        public override void PreBuildUp(IBuilderContext context)
        {
            var key = (NamedTypeBuildKey)context.OriginalBuildKey;
    
            if (factory.CanCreate(key.Type) && context.Existing == null)
            {
                context.Existing = factory.Create(key.Type);
                var ltm = new ContainerControlledLifetimeManager();
                ltm.SetValue(context.Existing);
    
                // Find the container to add this to
                IPolicyList parentPolicies;
                var parentMarker = context.Policies.Get<ParentMarkerPolicy>(new NamedTypeBuildKey<ParentMarkerPolicy>(), out parentPolicies);
    
                // TODO: add error check - if policy is missing, extension is misconfigured
    
                // Add lifetime manager to container
                parentPolicies.Set<ILifetimePolicy>(ltm, new NamedTypeBuildKey(key.Type));
                // And add to LifetimeContainer so it gets disposed
                parentMarker.AddToLifetime(ltm);
    
                // Short circuit the rest of the chain, object's already created
                context.BuildComplete = true;
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-02 06:33

    Update This answer was for Unity 1.2. For a solution that works with Unity 2, see my other answer.


    OK, I have implemented the extension myself. In the builder I cache the object as I want it to be a singleton w.r.t my container. The reason for baseContext is that I want it to be cached in the top level container rather than in any child containers from which it was requested.

     public class FactoryMethodUnityExtension<T> : UnityContainerExtension
     {
         private Func<Type,T> factory; 
    
         public FactoryMethodUnityExtension(Func<Type,T> factory)
         {
             this.factory = factory;
         }
    
         protected override void Initialize()
         {
             var strategy = new CustomFactoryBuildStrategy<T>(factory, this.Context);
             Context.Strategies.Add(strategy, UnityBuildStage.PreCreation);             
         }
     } 
    
     public class CustomFactoryBuildStrategy<T> : BuilderStrategy
     {
         private Func<Type,T> factory;
         private ExtensionContext baseContext;
    
         public CustomFactoryBuildStrategy(Func<Type,T> factory, ExtensionContext baseContext)
         {
            this.factory = factory;
            this.baseContext = baseContext;
         }
    
         public override void PreBuildUp(IBuilderContext context)
         {
             var key = (NamedTypeBuildKey)context.OriginalBuildKey;
    
             if (key.Type.IsInterface && typeof(T).IsAssignableFrom(key.Type))
             {
                 object existing = baseContext.Locator.Get(key.Type);
                 if (existing == null)
                 {
                     // create it
                     context.Existing = factory(key.Type);
                     // cache it
                     baseContext.Locator.Add(key.Type, context.Existing);
                 }
                 else
                 {
                     context.Existing = existing;
                 }
             }
         }
     }
    

    Adding the extension is quite simple:

    MyFactory factory = new MyFactory();
    container = new UnityContainer();
    container.AddExtension(new FactoryMethodUnityExtension<IBase>(factory.Create));
    
    0 讨论(0)
  • 2021-01-02 06:35

    The Unity container already acts as a factory that knows how to construct (and perform dependency injection for) arbitrary types, so your factory that accepts a Type t appears redundant. Can you elaborate more on why this is not possible?

    If this is truly not possible (more likely just too much work), then perhaps you can register your factory with the container instead?

    0 讨论(0)
  • 2021-01-02 06:43

    Unity (v2) allows you to specify a factory. It allows a couple of different functions, including taking the type to build up/ name, etc. Simple example:

    UnityContainer cont = new UnityContainer();
    

    have a simple create test method - but this can be expanded with whatever factory you want.
    this will circumvent the normal creation process which is (for the sake of brevity) call the longest constructor all later behaviors including propert setting will still be executed.

    cont.RegisterType<TestClass>(new InjectionFactory(c => CreateTest()));
    
    var svc = cont.Resolve<TestClass>();
    
    0 讨论(0)
提交回复
热议问题