Using abstract factory as injectionfactory in Unity?

混江龙づ霸主 提交于 2019-12-04 23:51:07

问题


I have an abstract factory registered for injection in some controller instances. Can I register that abstract factory and use it as an injection factory?

This is what I have:

public interface ILevelFactory
{
    Levels Create();
}

.RegisterType<ILevelFactory, LevelFactory>()
.RegisterType<Levels>(new InjectionFactory((c) => StaticLevelFactory.GetLevels()))

Desired situation:

.RegisterType<ILevelFactory, LevelFactory>()
.RegisterType<Levels>(*** look up and use ILevelFactory ***)

In short, I want to get rid of the StaticLevelFactory.


回答1:


If your ILevelFactory is properly registered:

RegisterType<Levels>(new InjectionFactory((c) => c.Resolve<ILevelFactory>().GetLevels()))


来源:https://stackoverflow.com/questions/5873757/using-abstract-factory-as-injectionfactory-in-unity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!