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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T03:46:04+00:00 2026-06-02T03:46:04+00:00

I have read the book professional asp.net design patterns by scott millett and in

  • 0

I have read the book professional asp.net design patterns by scott millett and in his example he validates his business logic within a Validate() method and if any broken rules are broken they get added to a collection and the service layer calls a method on the model called GetBrokenRules().

Now I have also read several books, blogs and forums regarding DDD and it says that in DDD an object should never get into an invalid state.

All the examples I have seen regarding DDD throw errors when a business rule has been broken instead of passing back a collection of broken rules. I have even downloaded the latest source code by scott millett and he has now changed his code which now throws errors instead of passing back the list of broken rules. I have also seen the same approach with other DDD code samples.

I am having a debate with a team member who believes that throwing errors is resource expensive and we should NOT throw the error but return a collection of broken rules like we currently do. However by doing this we are passing around an object that is invalid because it has dodgy data and we only check for its broken rules at the end.

I was just thinking what other peoples opinons are on this subject. Should we throw an error as soon as a business rule fails? If so, can you highlight any pros and cons of doing so.
I don’t know how resource expensive throwing errors are in .net so I can not argue against that point but I was wondering if this is also a matter of personal opinoin instead of coding standards.

Mike

  • 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-06-02T03:46:07+00:00Added an answer on June 2, 2026 at 3:46 am

    What @Oded said is perfectly valid.

    However there is another way to handle the validation, without throwing exceptions and without passing around an invalid object. Simply put, you always create the object via a factory method. If everything is good, an isntance is returned otherwise it returns null. Of course, this means you have to check for null once when you creat the object but that’s about it.
    The factory method (which can be part of a service) can take as argument an IValidationDictionary (not a BCL interface) on which the factory can set the errors. But we talk here only about input data (formatting validation) from the user. No business rules here. If the object will be used in a context in which its state is invalid then it has to throw an exception.

    You can avoid that 99% by having a method on the object or on its aggregate root like CanDoThat(). If object.CanDoThat(context) do that and avoid an exception. The exception may be triggered though if because of concurrency things change in the mean time.

    I usually have the following workflow:

    • user input validation at the controller layer (which is the anti corruption layer for the app). Only data formatting is handled here.
    • Once data is valid is send forward to be processed (usually via a command handler)
    • When processing I ask the aggregate root if it can do a specific action. If it can everything moves forward.
    • If can not then there are 2 cases:

      1. It is probable that something is wrong like a bug so an exception is appropriate here (that’s 99% cases).
      2. It’s a valid response in that context so it’s simply ignored.
    • Of course you may have the case where everything is ok at the aggregate root level, but it fails at the repository level because of a db constraint. This means the repository throws an exception, which I handle (because I exepct that case to happen). If the user expects a response then I’ll throw a specific exception to signal the error which will set the View’s ModelState.

    Now this is a tricky situation, because it looks like I’m using exception to control the code flow but that’s not the case. While I expect the db to throw a constraint violated exception, it’s still an exceptional case because that doesn’t happen every time and it’s not a situation the user can avoid. I’m throwing next the specific exception because the first one is something persistence related, but my controller doesn’t know about it so I have to communicate that exceptional state in a way it understands.

    Ok, long anwser but the idea is that some errors, while expected ar still exceptions and they need to be treated as such. However if there is a formatting validation error on ipnut data or the input data is invalid in a specific way, that should be handled in the first anti corruption layer, the controller itself and no exceptions are needed. The controller should not let invalid input go forward. If it goes forward then it is a bug, an exceptional situation.

    Hope, I wasn’t very confusing 🙂

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

Sidebar

Related Questions

I have read from the book Cocoa Design Patterns that the decorator pattern is
I've not read the Modern C++ Design book but have found the idea of
I have read Marcus Zarra's chapter on multithreading in his Core Data book and
I have read around 4-5 books on design patterns, but still I don't feel
I have started learning VB.NET lately and have read 1 Step-By-Step beginners book and
I have read in a Java book that says: Because a String is immutable,
In the book Linux System Programming I have read some like this: fgetc returns
I have just read this in the book OpenCV 2 Computer Vision Application Programming
I've read this book section about git branches. I have create a branch called
I'm new in c#. I have read some book about c# for beginners. Now

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.