|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionThe brain of the application is represented by the domain. The domain consists of the business rules which make the application function properly. The rules must be validated in order to guarantee a successful operation. In this article we will build a simple domain object validation framework using custom attributes and reflection. What does Domain Object Validation Means?First we need to understand what domain objects validation means. Let’s consider a simple class Customer which is domain object. The The Class DiagramTake a look at the complete class diagram of the validation framework. Now, let’s take a dive into the implementation. Creating the Abstract ValidationAttribute Custom AttributeThe first task is to create custom attributes. To create an attribute your class must inherit from public abstract class ValidationAttribute : System.Attribute
{
public string Message { get; set; }
public abstract bool IsValid(object item);
}
The Implementing the NotNullOrEmptyAttributeLet’s implement the first validation attribute “ [AttributeUsage(AttributeTargets.Property)]
public class NotNullOrEmptyAttribute : ValidationAttribute
{
public NotNullOrEmptyAttribute(string message)
{
Message = message;
}
public override bool IsValid(object item)
{
if (String.IsNullOrEmpty((string)item))
return false;
return true;
}
}
As, you can see the implementation is quite simple! The public class Customer : BusinessBase
{
[NotNullOrEmpty("First name cannot be null or empty")]
public string FirstName { get; set; }
[NotNullOrEmpty("First name cannot be null or empty")]
public string LastName { get; set; }
}
Implementing the ValidationEngineThe public static bool Validate<T>(T item) where T : BusinessBase
{
var properties = item.GetType().GetProperties(
BindingFlags.Public | BindingFlags.Instance);
foreach (var property in properties)
{
var customAtt = property.GetCustomAttributes(typeof(ValidationAttribute),
true);
foreach (var att in customAtt)
{
var valAtt = att as ValidationAttribute;
if (valAtt == null) continue;
if (valAtt.IsValid(property.GetValue(item, null))) continue;
var brokenRule = new BrokenRule
{
Message = String.Format("{0}:{1}",
property.Name, valAtt.Message),
PropertyName = property.Name
};
item.BrokenRules.Add(brokenRule);
}
}
return (item.BrokenRules.Count == 0);
}
The If you are more interested in validating your objects using the Using the ValidationEngine
static void Main(string[] args)
{
var customer = new Customer();
ValidationEngine.Validate(customer);
foreach(var brokenRule in customer.BrokenRules)
{
Console.WriteLine(brokenRule.Message);
}
}
Also, note that ConclusionIn this article we learned how to validate the business objects using custom attributes and reflection. You can extend the
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||