Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Repository Layer gives you additional level of abstraction over data access. Instead of writing</p> <pre><code>var context = new DatabaseContext(); return CreateObjectQuery&lt;Type&gt;().Where(t =&gt; t.ID == param).First(); </code></pre> <p>to get a single item from database, you use repository interface</p> <pre><code>public interface IRepository&lt;T&gt; { IQueryable&lt;T&gt; List(); bool Create(T item); bool Delete(int id); T Get(int id); bool SaveChanges(); } </code></pre> <p>and call <code>Get(id)</code>. Repository layer exposes basic <strong>CRUD</strong> operations.</p> <p>Service layer exposes business logic, which uses repository. Example service could look like:</p> <pre><code>public interface IUserService { User GetByUserName(string userName); string GetUserNameByEmail(string email); bool EditBasicUserData(User user); User GetUserByID(int id); bool DeleteUser(int id); IQueryable&lt;User&gt; ListUsers(); bool ChangePassword(string userName, string newPassword); bool SendPasswordReminder(string userName); bool RegisterNewUser(RegisterNewUserModel model); } </code></pre> <p>While <code>List()</code> method of repository returns all users, <code>ListUsers()</code> of IUserService could return only ones, user has access to.</p> <p>In ASP.NET MVC + EF + SQL SERVER, I have this flow of communication:</p> <blockquote> <p><strong>Views &lt;- Controllers -> Service layer -> Repository layer -> EF -> SQL Server</strong></p> <p><strong>Service layer -> Repository layer -> EF</strong> This part operates on models.</p> <p><strong>Views &lt;- Controllers -> Service layer</strong> This part operates on view models.</p> </blockquote> <p><strong>EDIT:</strong> </p> <p>Example of flow for /Orders/ByClient/5 (we want to see order for specific client):</p> <pre><code>public class OrderController { private IOrderService _orderService; public OrderController(IOrderService orderService) { _orderService = orderService; // injected by IOC container } public ActionResult ByClient(int id) { var model = _orderService.GetByClient(id); return View(model); } } </code></pre> <p>This is interface for order service:</p> <pre><code>public interface IOrderService { OrdersByClientViewModel GetByClient(int id); } </code></pre> <p>This interface returns view model:</p> <pre><code>public class OrdersByClientViewModel { CientViewModel Client { get; set; } //instead of ClientView, in simple project EF Client class could be used IEnumerable&lt;OrderViewModel&gt; Orders { get; set; } } </code></pre> <p>This is interface implementation. It uses model classes and repository to create view model:</p> <pre><code>public class OrderService : IOrderService { IRepository&lt;Client&gt; _clientRepository; public OrderService(IRepository&lt;Client&gt; clientRepository) { _clientRepository = clientRepository; //injected } public OrdersByClientViewModel GetByClient(int id) { return _clientRepository.Get(id).Select(c =&gt; new OrdersByClientViewModel { Cient = new ClientViewModel { ...init with values from c...} Orders = c.Orders.Select(o =&gt; new OrderViewModel { ...init with values from o...} } ); } } </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.
    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