Note that there are some explanatory texts on larger screens.

plurals
  1. PODependency Injection and Factories
    primarykey
    data
    text
    <p>What is a decent way to handle conditional sub-factories with a DI approach. The scenario is the loader object that gets injected into an entry is dependent on the settings of that entry. Originally I was injecting the IoC container into the factory and using that to resolve based on naming conventions. However, I'd really like to keep the factory clean of the container.</p> <p>The factory is injected into a class that will load all the settings from a database, and then use the factory to create a set of entries. The settings determine which loader will be used inside that given entry.</p> <p>Edit: Changing code to better highlight the actual question. The issue is that multiple database managers must be supported simultaneously, if this were not the case then it would be simple. The database manager type is determined by the entry settings stored for a particular entry.</p> <pre><code>public class Entry : IEntry { private ISomething loader; public Entry(ISomething something) { this.loader = something; } } public class EntryFactory : IEntryFactory { IEntry BuildEntry(IEntrySetting setting) { //Use setting object to determine which database manager will be used } } public class EntryManager { public EntryManager(IEntryFactory entryFactory) { var entrySettings = this.settings.Load(); foreach(var setting in entrySettings) { this.entries.Add(entryFactory.BuildEntry(setting)); } } } </code></pre> <p>I had considered having the sub-factories register with a primary factory and resolve them that way, but I don't know if there is a better approach.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload