Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.NET MVC Software Design Pattern: DI,Repository,Service Layer
    primarykey
    data
    text
    <p><strong>EDIT</strong></p> <p>Should i put service layer and repository layer into one project, so the web project is able to reference to DbContext objects ? Now my web(controllers) are unable to reference to dbcontext objects. what is the right way?</p> <pre><code>// service and repository are together (View &lt;- Controller) -&gt; (Service -&gt; Repository -&gt; EF DbContext) -&gt; (DB) // separate service and repository layer (View &lt;- Controller) -&gt; (Service) -&gt; (Repository -&gt; EF DbContext) -&gt; (DB) </code></pre> <p><strong>Below are original question</strong></p> <p>i know SO is an excellent community to post my questions about mvc design patterns. Please give me your advice and I will appreciate your help. Thank you!</p> <p>We are planning for a new project and our priority is to develop an application that is extensible and loosely-coupled. </p> <p>I am new to software development; I did some reading on <a href="http://www.asp.net/mvc/tutorials/mvc-music-store/mvc-music-store-part-1" rel="nofollow">MVC Music Store Tutorial</a>, and followed by a book called <a href="http://rads.stackoverflow.com/amzn/click/1430234040" rel="nofollow">Pro ASP.NET MVC 3 Framework by Steven Sanderson (Apress)</a>,From the book, I learnt about DDD(Domain driven design) and some other concepts like repository and dependency injection. I have followed the book to build the SportsStore website, and gained some basic understanding about DI. But I personally think that the example did not separate the business logic layer, so i did a research on that, i found a pattern called Service Layer Pattern, and from what i understand, it separates the Business logic layer. Based on this, I came out with a structure for my new project(sample project below).</p> <p>Do i need to implement <strong>IDisposable</strong> interface? if yes, where and why? Is this structure feasible for a relatively big-scale project?</p> <p>sample database design: <strong>Product(one)----(many)ProductCategoryRs(many)----(one)Category</strong></p> <p>the solution contains 3 projects: Repository,Service,Web</p> <p><strong>Repository:</strong></p> <p><strong>Define IRepository interface , basic CRUD operations</strong></p> <p><strong>Are these signatures sufficient? Should I add <em>TEntity GetById(object id);</em>?</strong></p> <pre><code>public interface IRepository&lt;TEntity&gt; { IQueryable&lt;TEntity&gt; All { get; } void Create(TEntity item); void Update(TEntity item); void Delete(TEntity item); void SaveChanges(); } </code></pre> <p><strong>Implement generic Repository class</strong></p> <pre><code>public class Repository&lt;TEntity&gt; : IRepository&lt;TEntity&gt; where TEntity : class { STOREEntities context; public Repository() { context = new STOREEntities(); } public IQueryable&lt;TEntity&gt; All { get { return context.Set&lt;TEntity&gt;(); } } public void Create(TEntity item) { context.Set&lt;TEntity&gt;().Add(item); } public void Update(TEntity item) { context.Entry&lt;TEntity&gt;(item).State = System.Data.EntityState.Modified; } public void Delete(TEntity item) { context.Set&lt;TEntity&gt;().Remove(item); } public void SaveChanges() { context.SaveChanges(); } } </code></pre> <p><strong>Service: Define IProductService interface, extend business logic here.</strong></p> <pre><code>public interface IProductService { IEnumerable&lt;Product&gt; Products { get; } IEnumerable&lt;Product&gt; Get(Expression&lt;Func&lt;Product, Boolean&gt;&gt; filter); Product GetByProductId(int productId); void AddProduct(Product product); void EditProduct(Product product); void RemoveProduct(Product product); void SaveChanges(); } </code></pre> <p><strong>Implement Product Service</strong></p> <pre><code> public class ProductService : IProductService { IRepository&lt;Product&gt; repository; //Inject public ProductService(IRepository&lt;Product&gt; repo) { repository = repo; } public IEnumerable&lt;Product&gt; Products { get { return repository.All; } } public IEnumerable&lt;Product&gt; Get(Expression&lt;Func&lt;Product, bool&gt;&gt; filter) { return repository.All.Where(filter); } public Product GetByProductId(int productId) { return repository.All.SingleOrDefault(p =&gt; p.ProductID == productId); } public void AddProduct(Product product) { repository.Create(product); } public void EditProduct(Product product) { repository.Update(product); } public void RemoveProduct(Product product) { repository.Delete(product); } public void SaveChanges() { repository.SaveChanges(); } } </code></pre> <p><strong>Web project, retrieve data from service and convert to viewmodel and display. ProductController code</strong></p> <pre><code>public class ProductController : Controller { IProductService productService; //inject public ProductController(IProductService service) { productService = service; } public ActionResult Index() { var products = productService.Products; //retrieve from service layer return View(products); } } </code></pre>
    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.
 

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