Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I had the same problem but my issue with the @Slauma's solution (although great in certain instances) is that it recommends that I pass the context into the service which implies that the context is available from my controller. It also forces tight coupling between my controller and service layers. </p> <p>I'm using Dependency Injection to inject the service/repository layers into the controller and as such do not have access to the context from the controller. </p> <p>My solution was to have the service/repository layers use the same instance of the context - Singleton. </p> <p><strong>Context Singleton Class:</strong> </p> <p>Reference: <a href="http://msdn.microsoft.com/en-us/library/ff650316.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/ff650316.aspx</a><br> and <a href="http://csharpindepth.com/Articles/General/Singleton.aspx" rel="nofollow">http://csharpindepth.com/Articles/General/Singleton.aspx</a></p> <pre><code>public sealed class MyModelDbContextSingleton { private static readonly MyModelDbContext instance = new MyModelDbContext(); static MyModelDbContextSingleton() { } private MyModelDbContextSingleton() { } public static MyModelDbContext Instance { get { return instance; } } } </code></pre> <p><strong>Repository Class:</strong></p> <pre><code>public class ProjectRepository : IProjectRepository { MyModelDbContext context = MyModelDbContextSingleton.Instance; </code></pre> <p>Other solutions do exist such as instantiating the context once and passing it into the constructors of your service/repository layers or another I read about which is implementing the Unit of Work pattern. I'm sure there are more...</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.
    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