Note that there are some explanatory texts on larger screens.

plurals
  1. POQuestions about the Service Layer as Validation in asp.net mvc
    primarykey
    data
    text
    <p>I am a bit confused about the service layer and using it validation.</p> <p>So I am looking through this tutorial: <a href="http://www.asp.net/learn/mvc/tutorial-38-cs.aspx" rel="nofollow noreferrer">http://www.asp.net/learn/mvc/tutorial-38-cs.aspx</a></p> <p>First if you look at List 3</p> <pre><code>using System.Collections.Generic; using System.Web.Mvc; namespace MvcApplication1.Models { public class ProductService : MvcApplication1.Models.IProductService { private ModelStateDictionary _modelState; private IProductRepository _repository; public ProductService(ModelStateDictionary modelState, IProductRepository repository) { _modelState = modelState; _repository = repository; } protected bool ValidateProduct(Product productToValidate) { if (productToValidate.Name.Trim().Length == 0) _modelState.AddModelError("Name", "Name is required."); if (productToValidate.Description.Trim().Length == 0) _modelState.AddModelError("Description", "Description is required."); if (productToValidate.UnitsInStock &lt; 0) _modelState.AddModelError("UnitsInStock", "Units in stock cannot be less than zero."); return _modelState.IsValid; } public IEnumerable&lt;Product&gt; ListProducts() { return _repository.ListProducts(); } public bool CreateProduct(Product productToCreate) { // Validation logic if (!ValidateProduct(productToCreate)) return false; // Database logic try { _repository.CreateProduct(productToCreate); } catch { return false; } return true; } } public interface IProductService { bool CreateProduct(Product productToCreate); IEnumerable&lt;Product&gt; ListProducts(); } } </code></pre> <p>They same interface just with a different name basically why not just use one?</p> <pre><code> public interface IProductRepository { bool CreateProduct(Product productToCreate); IEnumerable&lt;Product&gt; ListProducts(); } public interface IProductService { bool CreateProduct(Product productToCreate); IEnumerable&lt;Product&gt; ListProducts(); } </code></pre> <p>In my book though(the author who I think wrote this tutorial) has changed it to have IProductRepository to void. So that confuses me even more.</p> <p>So can someone explain why I need 2 interfaces that seems to do the same thing?</p> <p>My next questions is my repository has a delete function. Do I put this one in my Service layer too(I guess mandatory if you use one Interface but if you use 2 like about then it could be optinal).</p> <p>So what would I have in my service layer? Would it just call delete function in the repository? Should it just be a void method or should it return bool? I don't think for this method any validation would need to be done?</p> <p>So I am not sure if a bool would be needed.</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.
 

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