Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>For any object which lifetime must explicitly managed (such as objects that implement <code>IDisposable</code>) or matters to the user, try not to inject them, but inject a factory that allows creating such objects instead. Define this interface for instance:</p> <pre><code>public interface IDbDataContextFactory { dbDataContext CreateNew(); } </code></pre> <p>And use it as follows:</p> <pre><code>public class SqlPupilBlockService { private IDbDataContextFactory contextFactory; public SqlPupilBlockService( IDbDataContextFactory contextFactory) { this.contextFactory = contextFactory; } public void DoSomeOperation() { using (var db = this.contextFactory.CreateNew()) { // use the dbDataContext here. } } } </code></pre> <p>An implementation of would be very simple, like this:</p> <pre><code>public class DbDataContextFactory : IDbDataContextFactory { public dbDataContext CreateNew() { return new dbDataContext(); } } </code></pre> <p>And registration goes like this:</p> <pre><code>Bind&lt;IDbDataContextFactory&gt;().To&lt;DbDataContextFactory&gt;(); </code></pre> <p>The use of a factory makes it very explicit who is the owner of the created object and who should control its lifetime. This makes your code more readable and follows the <a href="http://en.wikipedia.org/wiki/Principle_of_least_astonishment" rel="nofollow noreferrer">principle of least surprise</a>.</p> <p><strong>UPDATE</strong></p> <p>More than a year has past since I submitted this answer. Instead of using factories, I now often inject the data context itself, and register it on a per (web) request basis. However; a shift in how you need to design your application might be needed, so as always: it depends. Please take a look at <a href="https://stackoverflow.com/a/10588594/264697">this answer</a>.</p>
    singulars
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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