I have the following code:
_container = new UnityContainer();
_container.RegisterType<IDownloader, Downloader>();
_container.RegisterType<INewObject, NewObject>();
_container.RegisterType<SearchViewModel>();
SearchViewModel class with constructor injection:
class SearchViewModel
{
private readonly Func<IDownloader> _downloaderFactory;
private readonly INewObject _newObject;
private IDownloader _downloader;
public SearchViewModel(Func<IDownloader> downloaderFactory, INewObject newObject)
{
_downloaderFactory = downloaderFactory;
_newObject = newObject;
}
}
The question: How to register SearchViewModel that has Fun<> as parameter?
_container.RegisterType<SearchViewModel>(new InjectionConstructor(DownloaderFactory()));
The code above works only without INewObject.
The goal: Resolve factory with InjectionConstructor and resolve INewObject, INewObject2, INewObject3 automatically (like without parameters: RegisterType<SearchViewModel>()).
Is it possible? Maybe alternates?
I have solved the problem:
new Func is a key, because before I tried:
Also the better way to use
IDownloaderFactoryinstead ofFunc<IDownloader> downloaderFactory.IDownloaderFactorycan encapsulate the delegate.Also I think that using a delegate as dependency inside factory is better solution than broken Composition Root.