Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 333275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T09:56:46+00:00 2026-05-12T09:56:46+00:00

I am a bit confused about the service layer and using it validation. So

  • 0

I am a bit confused about the service layer and using it validation.

So I am looking through this tutorial: http://www.asp.net/learn/mvc/tutorial-38-cs.aspx

First if you look at List 3

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 < 0)
                _modelState.AddModelError("UnitsInStock", "Units in stock cannot be less than zero.");
            return _modelState.IsValid;
        }

        public IEnumerable<Product> 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<Product> ListProducts();
    }
}

They same interface just with a different name basically why not just use one?

    public interface IProductRepository
    {
        bool CreateProduct(Product productToCreate);
        IEnumerable<Product> ListProducts();
    }

   public interface IProductService
    {
        bool CreateProduct(Product productToCreate);
        IEnumerable<Product> ListProducts();
    }

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.

So can someone explain why I need 2 interfaces that seems to do the same thing?

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).

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?

So I am not sure if a bool would be needed.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T09:56:46+00:00Added an answer on May 12, 2026 at 9:56 am

    From the tutorial you are reading:

    So, application flow control logic
    belongs in a controller and data
    access logic belongs in a repository.
    In that case, where do you put your
    validation logic? One option is to
    place your validation logic in a
    service layer.

    A service layer is an additional layer
    in an ASP.NET MVC application that
    mediates communication between a
    controller and repository layer. The
    service layer contains business logic.
    In particular, it contains validation
    logic.

    EDIT:

    I’m not sure if I can explain it to you in a clear way(’cause I’m not fluent in English), but I will try:

    A service layer is an additional layer in an ASP.NET MVC application that mediates communication between a controller and repository layer, in that you can handle both validation and application businness. Sometimes you service will need to work with two or more methods of its correspondent repository layer so it doesnt need to have the same interface.

    A basic example, let’s think you have a register form.

    you will have the following interfaces

    public interface IUserService
    
    {
        bool Register(User mUser);
        bool Validate(User mUser);
    }
    
    public interface IUserRepository
    {
        User FindUserByEmail(string Email);
        bool Insert(User mUser);
    }
    

    so you will end up with two class that will do something like:
    public class UserRepository: IUserRepository{

        User FindUserByEmail(string Email)
    {
        //do a ninja search and return an user or null
    }
        bool Insert(User mUser);
        {
            //Insert user into db
        }   
    }
    
    public class UserService: IUserService
    {
        public bool Validate(User mUser)
        {
            //validate user
        }
        IUserRepository _respository = new UserRepository();
        bool Register(User mUser)
        {
            if(Validate(mUser);
            var hasUser = _respository.FindUserByEmail(User.Email);
            if(hasUser==null)
                return _respository.Insert(mUser);
            return false;
        }   
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 380k
  • Answers 380k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer There is no equivalent to git clean in the core… May 14, 2026 at 9:59 pm
  • Editorial Team
    Editorial Team added an answer That depends. If you store separate data on the different… May 14, 2026 at 9:59 pm
  • Editorial Team
    Editorial Team added an answer You can use STS (spring tool suite) a new spring… May 14, 2026 at 9:59 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.