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

  • SEARCH
  • Home
  • 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 4096272
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T20:01:32+00:00 2026-05-20T20:01:32+00:00

After reading this article about validating with service layer I have some doubts. First:

  • 0

After reading this article about validating with service layer I have some doubts.

First: Is it all right to pass the whole model view to the service as shown in article ? I have seen some example where rather then passing whole model that is used by controller (like bool success = _productService.CreateProduct(productModel)) they called services like this:

 bool success = _productService.CreateProduct(productModel.Name, productModel.Category, productModel.Cost)

What are pros/cons of both approaches ?

Second: I can see the logic in using same service for validating model and doing the actual work. On the other hand it means that the service will have to deal with 2 concerns: data validation and data processing. That means more code in the same service and worse testability, right ?

So instead of example code above would it be better to have:

bool valid = _productValidationService(productModel);
if(valid){
    _productService.CreateProduct(productModel);
    //or maybe _productService.CreateProduct(productModel.Name, productModel.Category, productModel.Cost);
}

What are the pros/cons ? Is there something that I don’t see ? What do you use ? What is standard approach ?

  • 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-20T20:01:33+00:00Added an answer on May 20, 2026 at 8:01 pm

    I prefer to pass the whole model to the service instead of individual properties to avoid cluttering the methods. As far as the validation logic is concerned you are saying that it has to deal with two concerns: business logic validation and actual saving. I think that those two concerns belong to the service. A consumer of this service shouldn’t be able to call only the save without validating first and perform those two things in the service ensures that this wouldn’t happen. Also the actual saving could be delegated by the service method to one or multiple CRUD repository calls.

    So in a controller:

    string error;
    if (!_productService.TryCreateProduct(productModel, out error))
    {
        ModelState.AddModelError("key", error);
        return View(viewModel);
    }
    

    In the article you have linked the following approach is used to initialize the service and pass it the model state so that it writes the errors directly:

    _productService = new ProductService(
        _productRepository, 
        new ModelStateValidationWrapper(this.ModelState)
    );
    

    so that later you could directly:

    _productService.CreateProduct(productModel);
    

    This is also a valid and good approach. The only problem is the following line _productService = new ProductService(... inside a controller which is very bad as it is tightly coupling the controller with a particular implementation of a service. The service should be injected by a DI framework but the problem is passing the ModelState as it would create circular dependency. There are some threads here addressing this issue.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After reading this article http://camendesign.com/code/developpeurs_sans_frontieres I have decided to follow what it says and
I got a little curious after reading this /. article over hijacking HTTPS cookies.
After reading this discussion and this discussion about using CrashRpt to generate a crash
I saw this article pop-up in my MSDN RSS feed, and after reading through
After reading an interesting article about unit testing behavior instead of state, I came
Right now I'm reading this article regarding Java Garbage Collection: http://www.javaworld.com/javaworld/jw-08-1996/jw-08-gc.html ? Here is
Greetings, After reading the following article I have a question: https://developer.mozilla.org/en/Introduction_to_Object-Oriented_JavaScript In the inheritance
After reading this article I made a point that int () yields 0 because
After reading this article Storing C++ template function definitions in a .CPP file ,
After reading an article on REST (Restful Grails), I have gotten the impression that

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.