Note that there are some explanatory texts on larger screens.

plurals
  1. POImplementing Repository without ORM or LINQ in 3 tier architecture
    primarykey
    data
    text
    <p>I am trying to implement a simple repository pattern with IOC in my current project. The Project is 3 tier application with a UI Layer (a Web application), Business Logic Layer (a Class Library) and Data Access Layer (a Class Library). I want to use Unity Container from the Microsoft Enterprise Library for DI. I don't want to use any ORM like NHibernate, Entity Framework etc. I also don't want to use LINQ to SQL. Unfortunately I am not able to find any example of repository pattern implementation for my scenario. So I have decided to go ahead and implement the Repository pattern based on my understanding of the concept.</p> <p>In my Business Layer I have defined a generic Repository Interface:</p> <pre class="lang-cs prettyprint-override"><code>// namespace BusinessLayer interface IRepository&lt;T, U&gt; { // Add entity to the repository void Add(T entity); // Get single entity from the repository by id T GetById(U id); // Get list of entities from the repository // specified by parameters IEnumerable&lt;T&gt; GetList(IDictionary&lt;string,U&gt; parameters); ... ... } </code></pre> <p>I use the IRepository interface in my Business Service (which is also in the Business Layer) for Data Persistence Operations:</p> <pre class="lang-cs prettyprint-override"><code>// namespace BusinessLayer class BusinessService { IRepository&lt;BusinessObject, string&gt; _myRepository; //Constructor Injection public BusinessService( IRepository&lt;BusinessObject,string&gt; repository) { this._myRepository = repository; } public void Add(BusinessObject obj) { _myRepository.Add(obj); } public BusinessObject GetById(string guid) { return _myRepository.GetById(guid); } .... .... } public class BusinessObject { public string BusinessProperty { get; set; } } </code></pre> <p>The Data Access Layer implements the IRepository. So I have a reference of the Business Layer Library in the Data Layer</p> <pre class="lang-cs prettyprint-override"><code>using BusinessLayer; // namespace DataAccessLayer public class BusinessObjectRepository&lt;BusinessObject, String&gt; : IRepository&lt;BusinessObject, String&gt; { public void IRepository&lt;BusinessObject, String&gt;.Add( BusinessObject entity) { //Implement Database Insert here. } public BusinessObject IRepository&lt;BusinessObject, String&gt;.GetById( string id) { //Implement Database fetch here. } .... .... } </code></pre> <p>Finally in the UI layer I have references to both, the BusinessLayer and the DataAccessLayer:</p> <pre class="lang-cs prettyprint-override"><code>using BusinessLayer; using DataAccessLayer; namespace UILayer { void Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) { var businessObj = new BusinessService( new BusinessObjectRepository&lt;BusinessObject, String&gt;()); } } </code></pre> <p>Is this how you implement the Repository pattern ? <S>Can someone lead me on how can I use the Unity block in above case for DI ? </S></p> <p>As per rule of 3 tier, the UI Layer should not be aware of the Data Access Layer. But doing an Inversion of Control from Data Access Layer to Business Layer would mean switching the dependencies in reverse order. So now the concrete instance of the Repository has to be supplied in the UI Layer. Then how does one maintain the rule of 3 tier when doing an IoC ?</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.
 

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