In my spare time, I’m creating a small business framework that I can reuse for my school projects. I suppose it would be similar to a dumbed down CSLA, if that can give you an idea.
I’m having a bit of trouble with the business rule class though. Here’s what it looks like right now :
public class Rule
{
public string PropertyName { get; }
public string Description { get; }
public Func<object, bool> ValidationRule { get; }
public Rule(string propertyName, string message, Func<object, bool> validationRule)
{
this.PropertyName = propertyName;
this.Description = description;
this.ValidationRule = validationRule;
}
public bool IsBroken(object value)
{
return ValidationRule(value);
}
}
I’m not a huge fan of the boxing and unboxing I’m doing when I’m checking to see if the rule is broken (value can be any type).
Of course, I could just make the whole class generic and have my IsBroken function take an object of type T (probably a better idea than using objects anyway), but I was wondering if it would be possible to do something similar to the following :
public class Rule
{
public Func<T, bool> ValidationRule { get; }
public bool IsBroken<T>(T value)
{
return ValidationRule(value);
}
}
Without declaring the class with a generic type?
Any other tips are welcome.
No, it’s not possible. How would the compiler and/or the runtime know what type is T for property declaration?