Guidelines for working with ADO.Net Entity Framework

I would like to share with you some Guidelines for working with ADO.Net Entity Framework and other Linq flavored Framework.

  • We should enable all the integrity from the database. The validation of the integrity is not provided by the framework. It will be more effective if the database can return the index or the foreign key that is failing. I will need to do some test on this.
  • Don’t add instance properties or methods to the entities. Entities are not like business objects, they are only data container. Also, using instance properties inside entity reduce the easiness to write queries using the linq syntax.

    Ex: Supposed you’ve add the property MyProperty by code to the entity Employee. Now, supposed someone want to query the employee with your property:

    var q = from e in db.Employees
    where e.MyProperty != null
    select e;

    This will throw an exception at runtime because MyProperty cannot be translated to SQL.
  • Consider not putting validation into the model.
  • Don’t put UI stuff in the model. It may seems evident but ...
  • Use an interface over your model. Consider design for testability.

I will probably add more in the future.

Stay tune!

Dany

0 comments: