All my service methods use a facade to talk to the business logic. This means that in the facade the DataContracts (I'm using WCF) are translated to entities (Entity Framework) and then passed on to the business layer. The problem I'm facing right now is that I want to put certain validation logic in the business logic, but this keeps from from translating the data contract to an entity.
Example: I have a method on my facade called CreateUser(). The incoming UserDataContract has a property Name. This is a mandatory field in the database, so Entity Framework has put a not-nullable attribute above this member ([EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
to be exact). I can put the validation logic in the business layer, no problem there. But when a UserDataContract with a null in the Name field is translated a ConstraintException is thrown: Entity Framework prohibits me from setting a null value in the Name field of the UserEntity class.
What's an elegant way to avoid this? Can I somehow remove the check of this field in Enti开发者_开发知识库ty Framework (setting the nullable property to true will make EF complain about a mapping to a not-nullable column). Is there some pattern I can use to introduce a validation mechanism into the business layer and use this from the facades?
I have multiple facades, with multiple data contracts for the same entity. But still one entity type and one business layer.
I'm using .Net 4.0 here.
Thanks!
- Use POCOs: Same class/object in database layer and business layer = business object
- Add validation attributes to your business objects using for instance DataAnnotations.
精彩评论