Unity auto-factory with params

前端 未结 4 1116
感情败类
感情败类 2020-12-08 22:02

I\'m trying to figure out the correct way to inject an auto-factory which takes params, or even if this is possible with Unity.

For example I know I can do this:

相关标签:
4条回答
  • 2020-12-08 22:33

    Sorry to be one of those annoying people who answer their own questions but I figured it out.

    public class TestLog
    {
        private Func<string, ILog> logFactory;
    
        public TestLog(Func<string, ILog> logFactory)
        {
             this.logFactory = logFactory;
        }
        public ILog CreateLog(string name)
        {
            return logFactory(name);
        }
    }
    
    Container.RegisterType<Func<string, ILog>>(
         new InjectionFactory(c => 
            new Func<string, ILog>(name => new Log(name))
         ));
    
    TestLog test = Container.Resolve<TestLog>();
    ILog log = test.CreateLog("Test Name");
    
    0 讨论(0)
  • 2020-12-08 22:38

    The answer by @TheCodeKing works fine, but in most (possibly all?) cases could be shortened to the following:

    Container.RegisterInstance<Func<string, ILog>>(name => new Log(name));
    

    (note that I'm using RegisterInstance() instead of RegisterType())

    Since the Func<> implementation is already a kind of factory there's usually no need to wrap it in a InjectionFactory. It only ensures that each resolution of the Func<string, ILog> is a new instance, and I can't really think of a scenario that requires this.

    0 讨论(0)
  • 2020-12-08 22:40

    Autofac has parameterized instantiation to handle scenarios which need an auto-factory with parameters.

    While Unity doesn't support this out of the box, it's possible to implement an extension which will work in a way similar to Autofac's.

    A shameless plug: I implemented such an extension -- Parameterized Auto Factory.
    It can be used in a way similar to this:

    public class Log
    {
        public void Info(string info) { /* ... */ }
        public void Warn(string info) { /* ... */ }
        public void Error(string info) { /* ... */ }
    }
    
    public class LogConsumer
    {
        private readonly Log _log;
        private readonly string _consumerName;
    
        public LogConsumer(Log log, string consumerName)
        {
            _log = log;
            _consumerName = consumerName;
        }
    
        public void Frobnicate()
            => _log.Info($"{nameof(Frobnicate)} called on {_consumerName}");
    }
    
    var container = new UnityContainer()
        .AddNewExtension<UnityParameterizedAutoFactoryExtension>();
    
    var logConsumerFactory = container.Resolve<Func<string, LogConsumer>>();
    var gadget = logConsumerFactory("Gadget");
    gadget.Frobnicate();
    

    Please notice:

    • Func<string, LogConsumer> is resolved from container, but it wasn't registered anywhere -- it's generated automatically.
    • Func<string, LogConsumer> only provides the string consumerName parameter to LogConsumer's constructor. As a result, the Log log parameter is resolved from the container. If the auto-factory func looked like this Func<string, Log, LogConsumer> instead, then all the parameters of LogConsumer's constructor would've been supplied through the auto-factory.

    Basically, the extension works like this:

    1. It registers a so called BuilderStrategy in Unity's pipeline.
    2. This strategy gets invoked whenever Unity is going to build a type's instance.
    3. If the type to be built hasn't been registered explicitly and is a Func with parameters, the extension intercepts the instance building process.
    4. Now we only need to dynamically create a Funcof the given type which resolves its return type from the container and passes the Func's parameters as a ResolverOverride collection to the IUnityContainer.Resolve method.
    0 讨论(0)
  • 2020-12-08 22:48

    In case you're looking for a fully typed factory interface (allowing for XML documentation and parameter names, for instance), you could use a NuGet package I created, which you can leverage simply by defining an interface for the factory, and then associating it with the concrete type you want to instantiate.

    Code lives in GitHub: https://github.com/PombeirP/FactoryGenerator

    0 讨论(0)
提交回复
热议问题