I have the following class (part of it):
class SearchViewModel : BaseViewModel<SearchResultItem>
{
private readonly IDownloader _downloader;
public SearchViewModel( IDownloader downloader)
: base(model)
{
_downloader = downloader;
}
private void Download(object sender, DoWorkEventArgs e)
{
_downloader.Download(item);
}
}
I used constructor injection for IDownloader and it had worked fine before multithreading come to my life.
_downloader has a state and I need to run _downloader.Download(item) in separate threads (User clicks download buttons on search result page).
The goal: before _downloader.Download(item)
, a new instance of _downloader
should be initialized. I can use _container.Resolve(IDownloader)
, but it would corrupt a Composition Root principle.
I have created the question to discuss the best solution, because I think that direct initialization (new()) or reference to a container is not the answer.