Validating C# class fields using custom Attributes - c#-4.0

class Employee
{
[ValueNotEmpty("Empty strings not allowed"]
public string Name{get;set;}
}
"ValueNotEmpty" is a custom attribute.I tried using YABOV library but i have to call Validate method of the base class explicitly to validate the custom attributes and return the validation messages.
In the implementation class, i use reflection to set values on "Name" field.When i set value on "Name" field i expect the try block to throw an exception saying "Empty strings not allowed".Is there any way i can do this without explicitly calling a method to validate the class when i set value for the field?.

A few thoughts to this problem:
If your property calls a PropertyChanged event, there might be a mechanism which uses this to validate it. For instance in combination with data binding. If you don't have PropertyChanged, you need to call Validate somewhere explicitly, unless you're using AOP.
Executing code when you just set a value would require AOP. In .Net standard libraries, there is no AOP technology included, you would have to integrate one (eg. Spring). AOP requires code generation or byte-code enhancement. So it's not a trivial thing.

You could try inheritiing from IDataErrorInfo and the implementing as below, but I suppose that this is what you mean by calling the validation explicitly.
public class Employee : IDataErrorInfo
{
public string Name{get;set;}
string IDataErrorInfo.Error {get {return null;}}
string IDataErrorInfo.this[string propertyName]
{
get { return this.GetValidationError(propertyName);}
}
string GetValidationError(string propertyName)
{
string error = null;
switch(propertyName)
{
case "Name":
error = ValidateName();
break;
default:
error = "Unknown proeprty";
break;
}
}
string ValidateName()
{
if(!string.IsNullOrEmpty(this.Name))
{
return null;
}
return "Empty Name";
}
}

Related

DDD Invariants Business Rules and Validation

I am looking for advice on where to add validation rules for domain entities, and best practices for implementation. I did search and did not find what i was looking for, or i missed it.
I would like to know what the recommended way is for validating that properties are not null, in a certain range, or length, etc... I have seen several ways using an IsValid() and other discussions about enforcing in the constructor so the entity is never in an invalid state, or using preprocessing and postprocessing, and others using FluentValidation api, how invariants impact DRY and SRP.
Can someone give me a good example of where to put these sorts of checks, when using a App Service, Bounded Context, Domain Service, Aggregate Root, Entity layering. Where does this go, and what is the best approach?
Thanks.
When modeling your domain entity, it is best to consider real-world implications. Let's say you are dealing with a Employee entity.
Employees need a name
We know that in the real-world an employee must always have a name. It is impossible for an employee not to have a name. In other words, one cannot 'construct' an employee without specifying its name. So, use parameterised constructors! We also know that an employees name cannot change - so we prevent this from even happening by creating a private setter. Using the .NET type system to verify your employee is a very strong form of validation.
public string Name { get; private set; }
public Employee(string name)
{
Name = name;
}
Valid names have some rules
Now it starts to get interesting. A name has certain rules. Let's just take the simplistic route and assume that a valid name is one which is not null or empty. In the code example above, the following business rule is not validated against. At this point, we can still currently create invalid employees! Let's prevent this from EVER occurring by amending our setter:
public string Name
{
get
{
return name;
}
private set
{
if (String.IsNullOrWhiteSpace(value))
{
throw new ArgumentOutOfRangeException("value", "Employee name cannot be an empty value");
}
name = value;
}
}
Personally I prefer to have this logic in the private setter than in the constructor. The setter is not completely invisible. The entity itself can still change it, and we need to ensure validity. Also, always throw exceptions!
What about exposing some form of IsValid() method?
Take the above Employee entity. Where and how would an IsValid() method work?
Would you allow an invalid Employee to be created and then expect the developer to check it's validity with an IsValid() check? This is a weak design - before you know it, nameless Employees are going to be cruising around your system causing havoc.
But perhaps you would like to expose the name validation logic?
We don't want to catch exceptions for control flow. Exceptions are for catastrophic system failure. We also don't want to duplicate these validation rules in our codebase. So, perhaps exposing this validation logic isn't such a bad idea (but still not the greatest!).
What you could do is provide a static IsValidName(string) method:
public static bool IsValidName(string name)
{
return (String.IsNullOrWhiteSpace(value))
}
Our property would now change somewhat:
public string Name
{
get
{
return name;
}
private set
{
if (!Employee.IsValidName(value))
{
throw new ArgumentOutOfRangeException("value", "Employee name cannot be an empty value");
}
name = value;
}
}
But there is something fishy about this design...
We now are starting to spawn validation methods for individual properties of our entity. If a property has all kinds of rules and behavior attached to it, perhaps this is a sign that we can create an value object for it!
public PersonName : IEquatable<PersonName>
{
public string Name
{
get
{
return name;
}
private set
{
if (!PersonName.IsValid(value))
{
throw new ArgumentOutOfRangeException("value", "Person name cannot be an empty value");
}
name = value;
}
}
private PersonName(string name)
{
Name = name;
}
public static PersonName From(string name)
{
return new PersonName(name);
}
public static bool IsValid(string name)
{
return !String.IsNullOrWhiteSpace(value);
}
// Don't forget to override .Equals
}
Now our Employee entity can be simplified (I have excluded a null reference check):
public Employee
{
public PersonName Name { get; private set; }
public Employee(PersonName name)
{
Name = name;
}
}
Our client code can now look something like this:
if(PersonName.IsValid(name))
{
employee = new Employee(PersonName.From(name));
}
else
{
// Send a validation message to the user or something
}
So what have we done here?
We have ensured that our domain model is always consistent. Extremely important. An invalid entity cannot be created. In addition, we have used value objects to provide further 'richness'. PersonName has given the client code more control and more power and has also simplified Employee.
I built a library that can help you.
https://github.com/mersocarlin/ddd-validation

Can I use mutiple RuleSet withe in a same Validator Attribute in Microsoft Enterprise Library Validation Framework

Helllo..... I am quite new in Microsoft Enterprise Library Validation Framework. My question is that I want same validation condition in two different RuleSet.
Is it possible to put two rule set with in the same Validator like below
/// <summary>
///
/// </summary>
[StringLengthValidator(1,25,Ruleset="DetailRuleSet",Ruleset="MainRuleSet",Tag="First Name")]
public string FirstName
{
get { return firstName; }
set { firstName = value; }
}
or I have mentioned it by writing the same in two time with different ruleset name like below
/// <summary>
///
/// </summary>
[StringLengthValidator(1,25,Ruleset="DetailRuleSet",Tag="First Name")]
[StringLengthValidator(1, 25, Ruleset = "MainRuleSet", Tag = "First Name")]
public string FirstName
{
get { return firstName; }
set { firstName = value; }
}
Any help would be appreciated!!
First a disclaimer: I haven't worked so much on the Enterprise Library Application Validation Block, however, having been a programmer for over a decade and a half, and having used Validation models from ASP.NET to MVC Data Annotations, I can tell you that the API for validation in Enterprise Library is pretty similar. It took me about 20 minutes to download the Enterprise Library source code and look up the answer to this question. So, here's my answer.
Yes, you can apply more than one validation attribute to a given model property, each validation attribute specifying a different rule set.
However, in such a case, you will have to explicitly invoke the validator on the model type for that particular rule set.
If you do not do that, the Enterprise Library will execute the validator for the default rule-set.
In the context of your example, you can say:
StringLengthValidator(1,25,Ruleset="DetailRuleSet",Tag="First Name")]
[StringLengthValidator(1, 25, Ruleset = "MainRuleSet", Tag = "First Name")]
public string FirstName
{
get { return firstName; }
set { firstName = value; }
}
However, in this case, you have to specifically invoke one of the rule-sets for validation, like so:
var yourModelObjectValidator =
yourValidatorFactory.CreateValidator<YourModelClass>("yourRuleSetName");
var yourModelObject =
new YourModelClass { Foo = "foo", Bar = "bar", Gar = 2 };
var results =
yourModelObjectValidator.Validate(yourModelObject);
if (!results.IsValid)
{
foreach(var result in results)
{
/* run the state machine, do whatever, print */
}
}
If you do not specify the rule set name like we did above, the Enterprise Library will execute your validations in the context of a default rule set which has no name, and hence none of the two rules you specified above using the validation attributes will get executed.
UPDATE
Based on your comment, I see what your real question is.
Your question then really is: Can I specify more than a single rule-set in a single validation attribute declaration?
The answer is as simple as the question: No. Because the property RuleSet is declared simply as string and not as IEnumerable<string> in the BaseValidationAttribute class, the mother of all ValidatorAttribute classes in the EntLib source code.

Faking enums in Entity Framework 4.0

There are a lot of workarounds for the missing support of enumerations in the Entity Framework 4.0. From all of them I like this one at most:
http://blogs.msdn.com/b/alexj/archive/2009/06/05/tip-23-how-to-fake-enums-in-ef-4.aspx?PageIndex=2#comments
This workaround allows you to use enums in your LINQ queries which is what i exactly need. However, I have a problem with this workaround. I get for every complex type I'm using a new partial autogenerated class.Therefore the code does not compile any more because I already have a wrapper class with this name in the same namespace which converts betwen the backed integer in the database and the enum in my POCO classes. If I make my wrapper a partial class, the code still does not compile as it now contains two properties with the same name "Value". The only possibility is to remove the Value property by hand everytime I generate the POCO classes because the DB model changed (which during the development phase happens very often).
Do you know how to prevent a partial class to be generated out of complex property everytime the EF model changes?
Can you recommend me some other workarounds supporting enumerations in LINQ queries?
That workaround is based on the fact that you are writing your POCO classes yourselves = no autogeneration. If you want to use it with autogeneration you must heavily modify T4 template itself.
Other workaround is wrapping enum conversion to custom extension methods.
public static IQueryable<MyEntity> FilterByMyEnum(this IQueryable<MyEntity> query, MyEnum enumValue)
{
int val = (int)enumValue;
return query.Where(e => e.MyEnumValue == val);
}
You will then call just:
var data = context.MyEntitites.FilterByMyEnum(MyEnum.SomeValue).ToList();
I am using an approach based on the one described in your link without any modifications of the T4 templates. The contents of my partial wrapper classes are as follows:
public partial class PriorityWrapper
{
public Priority EnumValue
{
get
{
return (Priority)Value;
}
set
{
Value = (int)value;
}
}
public static implicit operator PriorityWrapper(Priority value)
{
return new PriorityWrapper { EnumValue = value };
}
public static implicit operator Priority(PriorityWrapper value)
{
if (value == null)
return Priority.High;
else
return value.EnumValue;
}
}
I've only changed that instead of a back store variable with enum value I am using the autogenerated int typed Value property. Consequently Value can be an auto-implemented property and EnumValue property needs to do the conversion in getter and setter methods.

Problem binding a bean property to an element in JSF

I have an input (JSF) that should be bound to a property in my bean. This property represents another bean and has an auxiliar method that checks if it's null (I use this method a lot).
The problem is that the binding is failing to get the proper getter and setter. Instead of reading the method that returns the bean, it reads the one that return a boolean value.
The property name is guest. The methods are:
getGuest;
setGuest;
isGuest (checks if guest is null).
JSF is trying to bind the object to isGuest and setGuest, instead of getGuest and setGuest.
I cannot rename isGuest to guestIsNull or something, because that would'nt make to much sense (see the class below).
Finally, my question is: how can I bind this property to the object without renaming my methods? Is it possible?
I also accept suggestions of a better method name (but the meaning must be the same).
Entity
#Entity
public class Passenger {
private Employee employee;
private Guest guest;
public Passenger() {
}
#Transient
public boolean isEmployee() {
return null != this.employee;
}
#Transient
public boolean isGuest() {
return null != this.guest;
}
#OneToOne
public Employee getEmployee() {
return this.employee;
}
public void setEmployee(Employee employee) {
this.employee = employee;
}
#OneToOne
public Guest getGuest() {
return this.guest;
}
public void setGuest(Guest guest) {
this.guest = guest;
}
}
JSF
<h:inputText value="#{passenger.employee}" />
<h:inputText value="#{passenger.guest}" />
Change the method name to isGuestNull.
The problem you're seeing is due to the fact that the EL lets you use getFoo or isFoo as the naming style for getter methods that return booleans.
No, that's not possible. You've to rename them.
Another way is to add a single getter returning an enum which covers all cases.
public enum Type {
GUEST, EMPLOYEE;
}
public Type getType() {
return guest != null ? Type.GUEST
: employee != null ? Type.EMPLOYEE
: null;
}
with
<h:something rendered="#{passenger.type == 'GUEST'}">
Binding to any property using any method is possible and quite easy if you create your custom ELResolver (apidocs). elresolvers are registered in faces config, and they are responsible, given an Object and a String defining a property, for determining the value and type of the given properties (and, as the need arises, to change it).
You could easily write your own ELResolver that would only work for your chosen, single type, and use (for example in a switch statement) the specific methods you need to write and read properties. And for other types it would delegate resolving up the resolver chain. It's really easy to do, much easier than it sounds.
But don't do it. The standard naming pattern of properties predates EL by many years. It is part of the JavaBeans™ standard - one of the very few undisputed standards in Javaland, working everywhere - from ant scripts, through spring configuration files to JSF. Seeing methods isPerson and getPerson in one class actually makes me fill uneasy, as it breaks something I always take for granted and can always count on.
If you like DDD and want to have your method's names pure, use an adapter. It's easy, fun, and gives a couple of additional lines, which is not something to sneer at if you get paid for the ammount of code produced:
public class MyNotReallyBean {
public String checkName() { ... }
public String lookUpLastName() { ... }
public String carefullyAskAboutAge() { ... }
public class BeanAdapter {
public String getName() { return checkName(); }
public String getLastName() { return lookUpLastName(); }
public String getAge() { return carefullyAskAboutAge(); }
}
private static BeanAdapter beanAdapter = new BeanAdapter();
private BeanAdapter getBeanAdapter(){ return beanAdapter; }
}

Is it bad to return same property in get accessor? public string ProjectID { get { return ProjectID; } etc

I have a simple class that I want to implement INotifyPropertyChanged. I don't need to have a private version of this property. The class is being passed via a WCF service and a Silverlight client.
My question: Is it OK to structure the get accessor this way? Just does not seem right to me.
public ProjectID
{
get
{
return this.ProjectID;
}
set
{
ProjectID = value;
NotifyPropertyChanged("ProjectID");
}
}
I think the code above will throw a stack overflow exception, you may have to implement a member to support the interface you want to
That would result in a StackOverflowException, because the get property would keep recursively calling itself.

Resources