Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using abstract factory as injectionfactory in Unity?

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.

like image 302
Carl R Avatar asked May 03 '11 18:05

Carl R


1 Answers

If your ILevelFactory is properly registered:

RegisterType<Levels>(new InjectionFactory((c) => c.Resolve<ILevelFactory>().GetLevels()))
like image 189
jeroenh Avatar answered Nov 09 '22 03:11

jeroenh