Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Yes <a href="https://stackoverflow.com/questions/3653009/entity-framework-and-connection-pooling/3653392#3653392">do not share context</a> and use one context per request. You can also check linked questions in that post to see all problems which a shared context caused.</p> <p>Now about Unity. Idea of <code>PerCallContextLifetimeManager</code> works but I think provided implementation will not work for more than one object. You should use <code>PerHttpRequestLifetimeManager</code> directly:</p> <pre><code>public class PerHttpRequestLifetime : LifetimeManager { // This is very important part and the reason why I believe mentioned // PerCallContext implementation is wrong. private readonly Guid _key = Guid.NewGuid(); public override object GetValue() { return HttpContext.Current.Items[_key]; } public override void SetValue(object newValue) { HttpContext.Current.Items[_key] = newValue; } public override void RemoveValue() { var obj = GetValue(); HttpContext.Current.Items.Remove(obj); } } </code></pre> <p>Be aware that Unity will not dispose context for you. Also be aware that default <code>UnityContainer</code> implementation will never call <code>RemoveValue</code> method. </p> <p>If your implementation resolves all repositories in single <code>Resolve</code> call (for example if your controllers receives instances of repositories in constructor and you are resolving controllers) you don't need this lifetime manager. In such case use build-in (Unity 2.0) <code>PerResolveLifetimeManager</code>. </p> <p><strong>Edit:</strong></p> <p>I see pretty big problem in your provided configuration of <code>UnityContainer</code>. You are registering both repositories with <code>ContainerControllerLifetimeManager</code>. This lifetime manager means Singleton instance per container lifetime. It means that both repositories will be instantiated only once and instance will be stored and reused for subsequent calls. Because of that it doesn't matter what lifetime did you assign to <code>MyEntities</code>. It is injected to repositories' constructors which will be called only once. Both repositories will use still that single instance of <code>MyEntities</code> created during their construction = they will use single instance for whole lifetime of your <code>AppDomain</code>. That is the worst scenario you can achieve.</p> <p>Rewrite your configuration this way:</p> <pre><code>var container = new UnityContainer(); container .RegisterType&lt;ProductsRepository&gt;() .RegisterType&lt;CategoryRepository&gt;() .RegisterType&lt;MyEntities&gt;(new PerResolveLifetimeManager(), dbConnectionString); </code></pre> <p>Why this is enough? You are resolving controller which is dependent on repsitories but no repository instance is needed more then once so you can use default <code>TransientLifetimeManager</code> which will create new instance for each call. Because of that repository constructor is called and <code>MyEntities</code> instance must be resolved. But you know that multiple repositories can need this instance so you will set it with <code>PerResolveLifetimeManager</code> => each resolving of controller will produce only one instance of <code>MyEntities</code>.</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