5

Just a quick Q, Is the Fluent API is replaceable for Data Annotations in term of features? What the features in Data Annotations that is not included in Fluent API?

I want to use Fluent API because of Separation of Concern (between my model & persistence), convention over configuration (mapping defined in one place DbContext.OnModelCreating() but not at every model property) and I want to use VS 2010 Layer Validation to make sure my POCO classes will never have dependencies to EF, but what I miss if I totally remove Data Annotation from my source?

CallMeLaNN
  • 8,328
  • 7
  • 59
  • 74
  • 2
    "I want to use Fluent API because of SoC and convection over configuration and Layer validation so Poco namespace will never has dependencies to EF" -- umm...my parser failed. – Kirk Woll Oct 19 '12 at 04:41
  • Sorry, I was in hurry at that time, I just update the question. – CallMeLaNN Oct 19 '12 at 06:52
  • Found the answer: Fluent API > Data Annotations [Code First - Are Data Annotations or the Fluent API better?](http://stackoverflow.com/a/5356222/186334) – CallMeLaNN Oct 19 '12 at 07:40

2 Answers2

1

FluentValidation.NET offers the full range capabilities of Data Annotations and even more. So you are not missing absolutely anything if you use FV instead of Data Annotations.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Thanks for the suggestion. It's been around since few years past but wondering why it still has less download count. If I use it how about simple annotation like `[Display(Name = "Username")]` and support for `@Html.LabelFor()`? – CallMeLaNN Oct 19 '12 at 07:03
  • 1
    FV is for validation purposes. It's not for customizing labels and display names. You could do it though using the `.WithName` method but in general I would keep this information along with the view model and use FV to only define validation rules. – Darin Dimitrov Oct 19 '12 at 07:06
  • With the small exception that `[DefaultValue()]` can't be done using Fluent API :( – webnoob Feb 08 '14 at 18:12
  • @webnoob, FluentValidation is a library designed for validation purposes, not for defining default values. – Darin Dimitrov Feb 08 '14 at 22:12
  • Isn't Fluent API used for defining table structure etc when doing code first migrations!? – webnoob Feb 08 '14 at 22:16
  • @webnoob, no, it isn't. FluentValidation is a library for defining validation rules of your models. – Darin Dimitrov Feb 08 '14 at 22:26
  • Ok, just re-read the OP. The OP subject is about Fluent API but then goes on about FluentValidation as well which are different things. Ok, confusion gone. Sorry to dig up an old post. – webnoob Feb 08 '14 at 22:31
  • @webnoob, that's because the OP confuses the 2 notions. Actually he means the same thing in both cases which is FluentValidation. – Darin Dimitrov Feb 08 '14 at 22:36
  • So i should copy and paste my model in a viewmodel to just add a displayname annotation on top of it? Is it worth? So every project should have a "Model" a "ViewModel" and a "DTO" of each class? What's the point with that? – exSnake May 04 '18 at 13:16
1

I wanted to know this is because I want my POCO class totally isolated from EF (my repository and UoW pattern make kt possible to move to NHibernate).

Fluent API > Data Annotations, that is Fluent API has more features than Data Annotations and useful to map table and create relationships. However Fluent API does not have nice label and validation for @Html.LabelFor and @Html.EditorFor by using [Display(Name:=...)], [DisplayFormat(DataFormatString=...)] and [Required(ErrorMessage=...)]. This is what making me headache.

Now found the idea that:

  1. Uses Fluent API in Data layer so my model really POCO class (POCO project) and the dll can be use in WCF and other project which will subscribe to this service.

  2. Uses Data Annotation for ViewModel. Since this is only within UI layer and ViewModel never shared to other project except for MVC View, I dont mind to have Data Annotation attributes.

  3. Redo the constrain created in (1) into (2) like [Required] and [MaxLength]. I found some people said it is worth to repeat and volatile the DRY principle since ViewModel and domain model should be separated and does not related to each other even though I think MaxLength is some how related (just a small repeat and it should be ok for n-tier architecture + I can use static class and const to make the length the same for both side).

CallMeLaNN
  • 8,328
  • 7
  • 59
  • 74