Search code examples
entity-frameworkdata-annotationsfluent

Entity Framework Fluent API: Violation of DRY?


I just stumbled across an EF Code First tutorial that goes over creating POCO configurations using the Fluent API. I was unaware this was available in EF and had only seem Data Annotations previously.

After watching this tutorial I read probably a dozen articles/topics on "Fluent API vs. Data Annotations". It seems like the consensus is that Fluent API allows you to separate your "dumb" domain POCOs from your persistence logic. The glaring problem I see with that is that you don't get the front-end validation that come along with the Data Annotations. I have yet to read a response from the pro-Fluent API crowd as to how this is tackled.

When using Fluent API, are you forced to duplicate your logic in the configuration files as well as on the front-end?


Solution

  • Yes, when using the fluent API approach it's likely that you will end up duplicating entity metadata in your front-end validation.

    One big benefit of the fluent API approach is that your entities have no dependency on Entity Framework. This issue has been fixed in .NET 4.5 by moving the data annotation attributes to the System.ComponentModel.DataAnnotations assembly.

    It's also common with complex entity models, that your front-end will not actually use the domain entities, but will use DTOs or some front-end specific model projected from the entities. In this case you are going to lose your data annotation attributes anyway.