Fluent Mapping or Data Annotations?

Asked

Viewed 321 times

2

I was watching a video class on ASP.NET MVC, when I watch the Fluent Mapping of the Entity Framework for the first time.

Then the doubt came to me.

Which of the two is best recommended? Fluent Mapping or Data Annotations?

I really liked the Fluent Mapping because I can play the validations for a separate class keeping my class clean, but the Data Annotations has the [Display(Name"")] that I use a lot.

It is possible to merge the two?

1 answer

2


Which of the two is best recommended? Fluent Mapping or Data Annotations?

There is no "best recommended" here. They are complementary, having several functions in common. Each is used according to your need.

Decoration by attributes (you called Data Annotations, which is actually the name of namespace of attributes) is more succinct and oriented to presentation, while the Fluent API works very well for global database configurations, being more prolific and less readable as to entity configuration and entity properties.

In this case, the Fluent API also uses the namespace System.ComponentModel.DataAnnotations.

Meanwhile, the Fluent API has more features than attribute decoration. I talk about them here.

It is possible to merge the two?

Yes, it is possible, and even normal, since some features are exclusive of one or the other.

I really liked the Fluent Mapping because I can play the validations for a separate class keeping my class clean, but the Data Annotations has the [Display(Name"")] that I use a lot.

What is the advantage of "keeping class clean"? The user of your system does not see the class. Empathy never should be a criterion to use a framework to the detriment of another.

[Display] is not used by Fluent API because it does not state a database behavior: it states a presentation behavior.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.