Entity Framework ObjectContext with Dependency Injection

别说谁变了你拦得住时间么 提交于 2020-01-21 10:28:54

问题


Well, it seems like I'm stuck in my application structure. Here's what I want to do:

  • UI layer: An ASP.NET webforms website.
  • BLL: Business logic layer which calls the repositories on DAL.
  • DAL: .EDMX file (Entity Model) and ObjectContext with Repository classes which abstract the CRUD operations for each entity.
  • Entities: The POCO Entities. Persistence Ignorant. Generated by Microsoft's ADO.Net POCO Entity Generator.

I'd like to create an obejctcontext per HttpContext in my repositories to prevent performance/thread [un]safety issues. Ideally it would be something like:

public MyDBEntities ctx
{
    get
    {
        string ocKey = "ctx_" + HttpContext.Current.GetHashCode().ToString("x");
        if (!HttpContext.Current.Items.Contains(ocKey))
            HttpContext.Current.Items.Add(ocKey, new MyDBEntities ());
        return HttpContext.Current.Items[ocKey] as MyDBEntities ;
    }
}  

The problem is the I don't want to access HttpContext in my DAL (Where the repositories are located). But I have to somehow pass HttpContext to DAL. based on the answer to my question here, I have to use IoC pattern. Ideally I'd like to achieve something like this in amulti-layered architecture.

I've checked out Autofac and it seems very promising. But I'm not sure how could I achieve this (Passing Httpcontext to make sure one ObjectContext is instantiated per HttpContext) in a multi-layered architecture. Could anyone give me some working example on how to achieve this? How can I be aware of HttpContext in DAL without directly accessing the HttpContext in DAL? I feel like I'm a bit lost in designing a multi-layered solution.


回答1:


I have never used IoC container with WebForms so get this as some high level solution which should probably be futher improved.

You can try creating some IoC provider as singleton:

public class IoCProvider
{
  private static IoCProvider _instance = new IoCProvider();

  private IWindsorContainer _container;

  public IWindsorContainer
  {
    get
    {
      return _container;
    }
  }

  public static IoCProvider GetInstance()
  {
    return _instance;
  }

  private IoCProvider()
  {
    _container = new WindsorContainer(new XmlInterpreter(new ConfigResource("castle")));
  }
}

Your web.config will have to contain sections like (the configuration is based on your previous post):

<configuration>
  <configSections>    
    <section name="castle" type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor" />
  </configSections>

  <castle>
    <components>
      <component id="DalLayer"
                 service="MyDal.IDalLayer, MyDal"
                 type="MyDal.MyDalLayer, MyDal"
                 lifestyle="PerWebRequest">
        <!-- 
             Here we define that lifestyle of DalLayer is PerWebRequest so each
             time the container resolves IDalLayer interface in the same Web request
             processing, it returns same instance of DalLayer class
          -->
        <parameters>
          <connectionString>...</connectionString>
        </parameters>
      </component>
      <component id="BusinessLayer"
                 service="MyBll.IBusinessLayer, MyBll"
                 type="MyBll.BusinessLayer, MyBll" />
      <!-- 
           Just example where BusinessLayer receives IDalLayer as
           constructor's parameter.
        -->
    </components>
  </castle>  

  <system.Web>
    ...
  </system.Web>
</configuration>

Implementation of these interfaces and classes can look like:

public IDalLayer
{
  IRepository<T> GetRepository<T>();  // Simplified solution with generic repository
  Commint(); // Unit of work
}

// DalLayer holds Object context. Bacause of PerWebRequest lifestyle you can 
// resolve this class several time during request processing and you will still
// get same instance = single ObjectContext.
public class DalLayer : IDalLayer, IDisposable
{
  private ObjectContext _context; // use context when creating repositories

  public DalLayer(string connectionString) { ... }

  ...
}

public interface IBusinessLayer
{
  // Each service implementation will receive necessary 
  // repositories from constructor. 
  // BusinessLayer will pass them when creating service
  // instance

  // Some business service exposing methods for UI layer
  ISomeService SomeService { get; } 
}

public class BusinessLayer : IBusinessLayer
{
  private IDalLayer _dalLayer;

  public BusinessLayer(IDalLayer dalLayer) { ... }

  ...
}

Than you can define base class for your pages and expose the business layer (you can do the same with any other class which can be resolved):

public abstract class MyBaseForm : Page
{
  private IBusinessLayer _businessLayer = null;
  protected IBusinessLayer BusinessLayer
  {
    get 
    { 
      if (_businessLayer == null)
      {
        _businessLayer = IoCProvider.GetInstance().Container.Resolve<IBusinessLayer>(); 
      }

      return _businessLayer;         
  }

  ...
}

Complex solution whould involve using custom PageHandlerFactory to resolve pages directly and inject dependencies. If you want to use such solution check Spring.NET framework (another API with IoC container).



来源:https://stackoverflow.com/questions/4619691/entity-framework-objectcontext-with-dependency-injection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!