Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Professional ASP.NET Design Patterns
This is the forum to discuss the Wrox book Professional ASP.NET Design Patterns by Scott Millett; ISBN: 978-0-470-29278-5
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional ASP.NET Design Patterns section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old October 29th, 2011, 08:15 AM
Registered User
Points: 8, Level: 1
Points: 8, Level: 1 Points: 8, Level: 1 Points: 8, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Validation of businessrules

Hi Scott,
First of all I would like to thank you for a great book!

I have a question about the way you validate businessrules in the service layer in the book. You throw an exception if a businessrule are broken.
In my opinion, exceptions should be thrown if something unexpected happens. Validation errors can be checked by logic and are therefore not unexpected. But maybe I'm wrong

Are there some other ways or best practices on how to notify the user about broken rules that happens in the service layer?
Should I return some kind of object that contains the errors from the service layer or should I call the Models Validate() method from the Controller class?

Regards
Erik
Reply With Quote
  #2 (permalink)  
Old December 5th, 2011, 11:47 AM
elbandit's Avatar
Wrox Author
Points: 599, Level: 8
Points: 599, Level: 8 Points: 599, Level: 8 Points: 599, Level: 8
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2007
Location: Southsea, Portsmouth, Hampshire, United Kingdom.
Posts: 107
Thanks: 10
Thanked 17 Times in 15 Posts
Default

Hi Erik.

Thanks for buying the book and sorry or the late reply. I have changed the way I handle validation in the domain now. I now try and not let my entities get into an invalid state.

I wrote a post about object validation a while ago that might be useful http://www.elbandit.co.uk/blog/post/...alidation.aspx.

I also use the Specification pattern (http://en.wikipedia.org/wiki/Specification_pattern) to implement in-variants in the domain which can also be used to check before trying to update a domain entity and there you can return error objects back to the domain.

Let me know if you need an example of if you have an example that I can use as context to make myself a bit clearer.

Cheers
Scott
Reply With Quote
  #3 (permalink)  
Old January 14th, 2012, 11:24 AM
Registered User
Points: 8, Level: 1
Points: 8, Level: 1 Points: 8, Level: 1 Points: 8, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Scott,

Thank you for your reply. The first link are interesting about validation. But I'm not sure I would like to throw an exception if one of the business rules are broken.
I haven't used the Specification pattern yet but I will take a closer look at it.

I have included a quick example of how I would like to solve this "problem" about validation of business rules. I would appriciate to know what you think about this approch.

ValidationError.cs contains validation error message.
Code:
public class ValidationError
{
    public string Property { get; private set; }
    public string Message { get; private set; }

    public ValidationError(string property, string message)
    {
        Property = property;
        Message = message;
    }
}
User.cs This is the user model.
Code:
public class User
{
    public string Username { get; set; }
    public string Password { get; set; }
    public string Firstname { get; set; }
    public string Lastname { get; set; }
    public List<ValidationError> ValidationErrors { get; set; }

    public User()
    {
        ValidationErrors = new List<ValidationError>();
    }

    public bool Validate()
    {
        ValidationErrors.Clear();
        ValidationRules();

        return ValidationErrors.Count == 0;
    }

    private void ValidationRules()
    {
        if (String.IsNullOrEmpty(Username))
        ValidationErrors.Add(new ValidationError("Username", "You must enter a username"));
    }
}
UserService.cs The CreateUser mothod validates the user before saving and returning false if the validation contains errors.
Code:
public class UserService
{
    public bool CreateUser(User user)
    {
        if (!user.Validate())
        return false;

        // Insert user to database.

        return true;
    }
}
Here follows an example of how I create a user.
Code:
User user = new User();
UserService service = new UserService();

if (service.CreateUser(user))
{
    Response.Write("The user was successfully created.");
}
else
{
    Response.Write(user.ValidationErrors[0].Message);
}
Here I can use an if-statement that tells me if the creation fails and get the get the validation messages.
If I instead throw exceptions if validation fails I need to place the call of CreateUser in a try catch block to catch the validation errors. This is for me harder to remember when I need to create a user instead of just using an if-statement. What is your opinion about this approch?

Cheers
Erik
Reply With Quote
  #4 (permalink)  
Old February 7th, 2012, 02:04 PM
Registered User
Points: 8, Level: 1
Points: 8, Level: 1 Points: 8, Level: 1 Points: 8, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to handle validation that requries database access from within an entity?

Scott,

Again, just wanted to say great book! I had initially posted a question about the Validation framework in the final chapter of your book, but I now have a question about the Business Rule validation in your book from the final Agatha's project.

From what I can see, the EntityBase exposes AddBrokenRule as a protected method, which means only implementing classes (the Entities) can add to this collection.

The framework is great, but one question I had is how to use this framework in conjunction with a business rule that requires a check to the database?

As an example, let's pretend for a moment that the Customer entity in your book has a business that says "customer first and last name must be unique". Obviously, this is for illustrative purposes only, but in my case, I'm currently working on a system where we have a Company entity, and to avoid duplicates in the database, we do not want to allow two Companies with the same name to be persisted to the db.

Given the current architecture in your book, since the entity is really the only object that can call AddBrokenRule, I need a way to represent, and check this rule IN the entity itself. Since the check would require a database read of all Customers in the system to see if the current Customer about to be persisted has a unique first and last name, that would mean I'd have to inject some type of Service or Repository class into my Entity, which I believe is a "Bad Thing To Do".

I can't use setter injection for Service or a Repository into the .Validate method, b/c that method in the Customer entity is overridden in EntityBase, and their signatures need to be the same.

Any ideas here? Perhaps to pull out the BrokenRules for the entity into a separate CustomerValidation class, and then inject my needed dependencies into that class in order to validate the rules that would need to read a database?

The only other thing I can think of is to let the Service Layer do this type of db dependency business rule checking, but to me, that seems like I'm allowing very important information to leak out of my domain into my Service Layer, and that does not seem like a good idea.

Thanks,
Mike
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Standalone validation + web form validation morbo Struts 0 August 19th, 2008 04:02 AM
Validation using Validation Framework kalyangvd Struts 1 January 2nd, 2008 06:53 AM
Validation Sheikha General .NET 4 October 8th, 2004 04:05 AM
Validation ? dinosaur_uk VB.NET 2002/2003 Basics 2 September 15th, 2004 10:28 AM
validation pab006 Classic ASP Basics 2 November 28th, 2003 05:25 AM



All times are GMT -4. The time now is 04:51 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.