vendredi 20 mars 2015

Design ideas - validation based on conditions outside of the domain


I am facing a very interesting problem here, and I'd like to see some design ideas from domain driven design perspective.


To make it easier to express what I need I have following hypothetical situation. I have a bounded-context named Customer, which is used to handle create new customer, update customer, searching customer and so on. I have a customer class look like



public class Customer
{
public int Id { get; set ; }
public int SocialInsuranceNumber { get; set ; }

public bool IsValid()
{
// my logic to validate social insurance number
}
}


So I have a straightforward logic in my entity to validate the data. The Canadian Social Insurance Number validation rule indicates all values begins with 0 should not be used, but Canadian government might use it for some reason. So there are possible 2 validation rules here now.


The business has the requirement of one single application - for majority of the situations when user enters a social insurance number we have to make sure the value cannot start with 0 and the value must be a valid SIN, but in some unique situation we must allow a valid SIN starting with 0.


My domain service code looks like



public class CustomerService
{
public void AddCustomer(Customer customer)
{
if (customer.IsValid())
{
// save to database
}
}
}


If I add a boolean parameter to Customer class's IsValid() function to make to look like IsValid(bool allowLeadingZero), I could make the code work. But I think it looks ugly.


From design perspective how would you suggest a more elegant way to deal with such requirement?





Aucun commentaire:

Enregistrer un commentaire