Yazmış olduğumuz entity lerin propertylerinde bir çok biz rule olabilir, boş geçilmemesi, geçilirse bunların loglanması veya hata verdirilmesi gibi, örneklendirirsek müşterimizin siparişinin kredi limitinin üzerinde olmaması yine iş kuralıdır. Aşağıdaki kodlarda çok basit bir örnek ile property değerleri atanmadığı durumda hata fırlatılan bir örnek.
İlk Başta Attribute yazalım:
[AttributeUsage(AttributeTargets.Property)]
public sealed class RequiredAttribute : Attribute
{
#region
Constructors
/// <summary>
/// Default constructor.
/// </summary>
//public RequiredAttribute(string propertyName, string errorMessage) : this(propertyName, errorMessage) { }
/// <summary>
/// Constructor that takes the null value as parameter.
/// </summary>
public RequiredAttribute(string propertyName, string errorMessage)
{
if (string.IsNullOrEmpty(propertyName))
{
throw new ArgumentNullException("ArgumentNullException");
}
if (string.IsNullOrEmpty(errorMessage))
{
throw new ArgumentNullException("ArgumentNullException");
}
this.propertyName = propertyName;
this.errorMessage = errorMessage;
}
#endregion
Constructors
#region
Fields
private string propertyName;
private string errorMessage;
private object nullValue;
#endregion
Fields
#region
Properties
/// <summary>
/// Hangi Property
/// </summary>
public string PropertyName
{
get { return propertyName; }
}
/// <summary>
/// Hata Mesajı
/// </summary>
public string ErrorMessage
{
get { return errorMessage; }
}
#endregion
Properties
}
Bunu kontrol eden fonksiyonumuzu yazalım
public class businessRule
{
public bool ValidateRequiredProperties(BusinessObjectType objectToBeSaved)
{
bool isValid = true;
//Propertileri alalım
foreach (PropertyInfo pi in objectToBeSaved.GetType().GetProperties())
{
//Kendi Attribute cekelim
if (pi.GetCustomAttributes(typeof(RequiredAttribute), true).Length > 0)
{
RequiredAttribute req = pi.GetCustomAttributes(typeof(RequiredAttribute), true)[0] as RequiredAttribute;
if (req != null)
{
object value = pi.GetValue(objectToBeSaved, null);
switch (pi.PropertyType.FullName)
{
case "System.String":
{
if (string.IsNullOrEmpty((string)value))
{
throw new Exception(" PropertyName : " +req.PropertyName + "\nError Message : " + req.ErrorMessage);
isValid =
false;
}
break;
}
case "System.DateTime":
{
if (((DateTime)value) == DateTime.MinValue
&& value !=
null)
{
throw new Exception(" PropertyName : " + req.PropertyName + "\nError Message : " + req.ErrorMessage);
isValid =
false;
}
break;
}
case "System.Int32":
if ((int)value==0)
{
throw new Exception(" PropertyName : " + req.PropertyName + "\nError Message : " + req.ErrorMessage);
isValid =
false;
}
break;
default:
{
if (value == null)
{
throw new Exception(" PropertyName : " + req.PropertyName + "\nError Message : " + req.ErrorMessage);
isValid =
false;
}
break;
}
}
}
}
}
return isValid;
}
}
Entity Kullanımı
public
class BusinessObjectType
{
private string _Ad;
[
Required("Ad", " Ad Alanı Boş Geçemezsin")]
public string Ad
{
get { return _Ad; }
set { _Ad = value; }
}
private DateTime _Tarih;
[
Required("Tarih", "Tarihi alanı Boş Geçilemez")]
public DateTime Tarih
{
get { return _Tarih; }
set { _Tarih = value; }
}
private int myVar;
[
Required("ID","ID değer alamalı")]
public int ID
{
get { return myVar; }
set { myVar = value; }
}
}
UI tarafı kontrol
class Program
{
static void Main(string[] args)
{
try
{
BusinessObjectType b = new BusinessObjectType();
businessRule br = new businessRule();
br.ValidateRequiredProperties(b);
Console.WriteLine(b.Ad);
Console.Read();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.Read();
}
}
}