Note that there are some explanatory texts on larger screens.

plurals
  1. PODependency Injection: Where should I inject? Who should control it?
    primarykey
    data
    text
    <p>Disclaimer: Code written without a compiler, you should discard any syntax errors. :)</p> <p>I am wondering about what I am doing is "right".</p> <p>Currently, I have a Manager class with static methods. Those methods each call a method on my Repository class (eg. Manager.Get will call Repository.Get, and so on).</p> <p><em>The Repository implementation will be injected!</em></p> <p>Manager has a static field that references a singleton instance to itself, as well as a reference to an instance of the repository.</p> <pre><code>static Manager _me = new Manager(); [Dependency] public IRepository Repo; </code></pre> <p><strong>Note: This is how I currently do the resolving! I need feedback on this!</strong></p> <p>In the managers Ctor, I resolve the dependency</p> <pre><code>public Manager() { // Feedback would be nice on this as well! // I have a singleton reference to the container, // being set from the startup project. Repo = Singleton.UnityContainer.Resolve&lt;IRepository&gt;(); } </code></pre> <p>Manager's methods would look like this:</p> <pre><code>public static MyObject Get(int id) { return _me.Repo.Get(id); } </code></pre> <p>In my main program (e.g a Console application), I will register my implementation.</p> <pre><code>static void Main(string[] args) { var container = Singleton.UnityContainer = new UnityContainer(); // The implementation to inject, could change over time // to a MSSQL or MySQL, who knows? Hence the DI! var myXmlRepo = new XmlRepository("C:\My.xml"); // Register the instance container.RegisterInstance(myXmlRepo); container.RegisterInstance&lt;IRepository&gt;(myXmlRepo, new ContainerControlledLifetimeManager()); // Why do I need to register TWICE? } </code></pre> <p>So, to summarize my question:</p> <ol> <li>Is it a bad idea to use a singleton reference (not only to a container, but at all?) in the way shown above?</li> <li>Am I doing the resolving right? Or should I have used BuildUp somewhere? Should I have called Resolve from the startup projects code?</li> </ol> <p>Thank you for your time!</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.
 

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