Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 6th, 2012  |  syntax: None  |  size: 3.14 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Entity Framework ObjectContext with Dependency Injection
  2. public MyDBEntities ctx
  3. {
  4.     get
  5.     {
  6.         string ocKey = "ctx_" + HttpContext.Current.GetHashCode().ToString("x");
  7.         if (!HttpContext.Current.Items.Contains(ocKey))
  8.             HttpContext.Current.Items.Add(ocKey, new MyDBEntities ());
  9.         return HttpContext.Current.Items[ocKey] as MyDBEntities ;
  10.     }
  11. }
  12.        
  13. public class IoCProvider
  14. {
  15.   private static IoCProvider _instance = new IoCProvider();
  16.  
  17.   private IWindsorContainer _container;
  18.  
  19.   public IWindsorContainer
  20.   {
  21.     get
  22.     {
  23.       return _container;
  24.     }
  25.   }
  26.  
  27.   public static IoCProvider GetInstance()
  28.   {
  29.     return _instance;
  30.   }
  31.  
  32.   private IoCProvider()
  33.   {
  34.     _container = new WindsorContainer(new XmlInterpreter(new ConfigResource("castle")));
  35.   }
  36. }
  37.        
  38. <configuration>
  39.   <configSections>    
  40.     <section name="castle" type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler, Castle.Windsor" />
  41.   </configSections>
  42.  
  43.   <castle>
  44.     <components>
  45.       <component id="DalLayer"
  46.                  service="MyDal.IDalLayer, MyDal"
  47.                  type="MyDal.MyDalLayer, MyDal"
  48.                  lifestyle="PerWebRequest">
  49.         <!--
  50.              Here we define that lifestyle of DalLayer is PerWebRequest so each
  51.              time the container resolves IDalLayer interface in the same Web request
  52.              processing, it returns same instance of DalLayer class
  53.           -->
  54.         <parameters>
  55.           <connectionString>...</connectionString>
  56.         </parameters>
  57.       </component>
  58.       <component id="BusinessLayer"
  59.                  service="MyBll.IBusinessLayer, MyBll"
  60.                  type="MyBll.BusinessLayer, MyBll" />
  61.       <!--
  62.            Just example where BusinessLayer receives IDalLayer as
  63.            constructor's parameter.
  64.         -->
  65.     </components>
  66.   </castle>  
  67.  
  68.   <system.Web>
  69.     ...
  70.   </system.Web>
  71. </configuration>
  72.        
  73. public IDalLayer
  74. {
  75.   IRepository<T> GetRepository<T>();  // Simplified solution with generic repository
  76.   Commint(); // Unit of work
  77. }
  78.  
  79. // DalLayer holds Object context. Bacause of PerWebRequest lifestyle you can
  80. // resolve this class several time during request processing and you will still
  81. // get same instance = single ObjectContext.
  82. public class DalLayer : IDalLayer, IDisposable
  83. {
  84.   private ObjectContext _context; // use context when creating repositories
  85.  
  86.   public DalLayer(string connectionString) { ... }
  87.  
  88.   ...
  89. }
  90.  
  91. public interface IBusinessLayer
  92. {
  93.   // Each service implementation will receive necessary
  94.   // repositories from constructor.
  95.   // BusinessLayer will pass them when creating service
  96.   // instance
  97.  
  98.   // Some business service exposing methods for UI layer
  99.   ISomeService SomeService { get; }
  100. }
  101.  
  102. public class BusinessLayer : IBusinessLayer
  103. {
  104.   private IDalLayer _dalLayer;
  105.  
  106.   public BusinessLayer(IDalLayer dalLayer) { ... }
  107.  
  108.   ...
  109. }
  110.        
  111. public abstract class MyBaseForm : Page
  112. {
  113.   private IBusinessLayer _businessLayer = null;
  114.   protected IBusinessLayer BusinessLayer
  115.   {
  116.     get
  117.     {
  118.       if (_businessLayer == null)
  119.       {
  120.         _businessLayer = IoCProvider.GetInstance().Container.Resolve<IBusinessLayer>();
  121.       }
  122.  
  123.       return _businessLayer;        
  124.   }
  125.  
  126.   ...
  127. }