0

I understand that its a best practice to put your business rules in a business layer and your data access in a separate data access layer, while the entities are part of your model.

So for example i'm working with a Customer entity defined in my model, i can fetch all customers from the database via the Data access layer. I can also add a new customer, via the DAL.

However before I add the customer, i need to run a validation on it - so i guess i need to define the rules in the business layer - but i'm not quite sure how to go about it.

Does my business layer only have methods which accept the entities as parameters? Something like BLL.Customers.Validate(Model.Customer customer) ?

Or

Does my business layer extend my Entities? Should I make the entity classes like Customer partial classes? so that the BLL can extend them further with business rules?

I'm not sure how to design the business layer.

newbie
  • 1,485
  • 2
  • 18
  • 43
  • 1
    There is an excellent discussion on validation in a domain driven design on stackoverflow. Visit the following link for more details http://stackoverflow.com/questions/516615/validation-in-a-domain-driven-design. – Hans Oct 29 '11 at 07:46
  • This question might also be of interest to you: http://stackoverflow.com/questions/5818898/where-to-put-global-rules-validation-in-ddd/5832618#5832618 – Marijn Oct 30 '11 at 14:19

1 Answers1

0

Your entities should really carry all their own business behaviour, so there would be a Validate method on the entity itself. I also like to have a static factory method in my entities for creating an entity:

public class Cusotmer
{
    private long? _id;
    private string _name;
    //other attributes...

    public static Customer Create(string name)
    {
        Customer customer = new Customer();
    }

    public void Validate()
    {
        if(_name == null)
            throw new ValidationException("Name has not been set.");
    }
}

I'm not sure why you need partial classes, except maybe if you're code generating your entities from a database perhaps?

Paul T Davies
  • 2,527
  • 2
  • 22
  • 39
  • 1
    If i tie the validate method with the entity and i have different validation requirements based on the usage then the entity gets bloated. I kind of like the Visitor pattern of usage. – newbie Oct 30 '11 at 18:34
  • account for the different in the one validation method with a few if statements. Or maybe you need to have different entities in different systems for the different usages: read up on bounded contexts. – Paul T Davies Oct 30 '11 at 20:07