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 3789768
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T12:12:34+00:00 2026-05-19T12:12:34+00:00

The one thing that can still get me is what to return from methods?

  • 0

The one thing that can still get me is what to return from methods? I know that you should be as specific as possible(ie don’t return object if you need to return an int)

However it’s cases like this

I have a service layer that contains business logic. Say I have a method called CreateAnEvent()

Basically it saves an event to the database and if successful it will return a string with “saved to database”

Now what happens if I first need to check if this event meets some business rules.

  1. Can’t be less than today’s date.
  2. Can’t be greater than one week from today.
  3. Can’t be created on a Friday.

Something like that where I need to first do some business checks that could either lead to validation errors or maybe some sort of other error(maybe even an exception) or a success message still gets me what I should be returning.

So lets go first with validation. Say the user fails all these validation rules what now? I am returning a string so this won’t be pretty I going to have to use csv to return all the errors and parse them out.

So that just seems very bad.

I could first have a separate method called Validate() but now I have to remember to call this before I call CreateAnEvent(). So I don’t think this is that great.

I am using ViewModels in my mvc project so maybe I should pass the entire viewModel to the service layer and then return this ViewModel.

In this view model I could have a collection of Errors that I could add all these errors to as well as a string to contain a success message.

Once it gets returned just do a check first for errors if count is zero then assume success msg is filled.

I am not returning the most specific type back but it would solve my problem. However I don’t think the service layer should know anything about a viewModel.

I know some people do the validation in the viewmodel but I consider that more for basic non business logic rules like is a field blank or not. I don’t think I should be doing testing like I described above in a viewmodel.

So the last option I can think of is send in the Domain Model(that would be in a viewmModel since there is a good chance I would be using it in the view) and in this Domain Model I would add a string for success messages and a collection for errors.

I would then return this domain model.

So it basically same idea as using ViewModels but this time extracting out the domain model and using it.

So is this the best way to do it or are there better ways?

  • 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-19T12:12:35+00:00Added an answer on May 19, 2026 at 12:12 pm

    I prefer to separate my validation logic into classes of their own. The reason for this is entities can have various layers of validation. My validator classes implement the following interface:

    public interface IValidator<T>
    {
        bool IsValid(T entity);
        IEnumerable<string> BrokenRules(T entity);
    }
    

    I then inject this validator into my service. The service can then return a boolean if the method succeeds / fails. On failures the additional information contained in the ‘BrokenRules’ enumerable is available. I have documented this in a blog post:

    http://blog.bobcravens.com/2010/09/the-repository-pattern-part-2/

    Let me know if you have any questions. Hope this helps.

    Bob

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

Sidebar

Related Questions

One of the cool things about the .NET Compact Framework is that you can
One thing that's really been making life difficult in getting up to speed on
One thing that bugs me about IE is that when it goes to load
One thing that annoys me when debugging programs in Visual Studio (2005 in my
One thing that always strikes me as a non-cryptographer: Why is it so important
One thing that's always confused me is input escaping and whether or not you're
There is one thing that I do not understand... Imagine you have a text
I'm currently evaluating CodeRush and one thing that I liked most when reading the
I have a solid understanding of most OOP theory but the one thing that
I've used ConfigParser for quite a while for simple configs. One thing that's bugged

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.