Unity auto-factory with params

前端 未结 4 1124
感情败类
感情败类 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:38

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

    Container.RegisterInstance>(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 is a new instance, and I can't really think of a scenario that requires this.

提交回复
热议问题