Are fluent interfaces more flexible than attributes and why?

In a EF 4.1 Code First tutorial the following code is given:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public class Department
{
public int DepartmentId { get; set; }
[Required]
public string Name { get; set; }
public virtual ICollection<Collaborator> Collaborators { get; set; }
}
</code>
<code>public class Department { public int DepartmentId { get; set; } [Required] public string Name { get; set; } public virtual ICollection<Collaborator> Collaborators { get; set; } } </code>
public class Department
{
    public int DepartmentId { get; set; }
    [Required]
    public string Name { get; set; }
    public virtual ICollection<Collaborator> Collaborators { get; set; }
}

Then it is explained that the fluent interface is more flexible:

Data Annotations are definitely easy to use but it is preferable to
use a programmatic approach that provides much more flexibility.

The example of using the fluent interface is then given:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Department>().Property(dp => dp.Name).IsRequired();
modelBuilder.Entity<Manager>().HasKey(ma => ma.ManagerCode);
modelBuilder.Entity<Manager>().Property(ma => ma.Name)
.IsConcurrencyToken(true)
.IsVariableLength()
.HasMaxLength(20);
}
</code>
<code>protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<Department>().Property(dp => dp.Name).IsRequired(); modelBuilder.Entity<Manager>().HasKey(ma => ma.ManagerCode); modelBuilder.Entity<Manager>().Property(ma => ma.Name) .IsConcurrencyToken(true) .IsVariableLength() .HasMaxLength(20); } </code>
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Department>().Property(dp => dp.Name).IsRequired();
    modelBuilder.Entity<Manager>().HasKey(ma => ma.ManagerCode);
    modelBuilder.Entity<Manager>().Property(ma => ma.Name)
        .IsConcurrencyToken(true)
        .IsVariableLength()
        .HasMaxLength(20);
}

I can’t understand why the fluent interface is supposedly better. Is it really? From my perspective it looks like the data annotations are more clear, and have more of a clean semantic feel to it.

My question is why would a fluent interface be a better option than using attributes, especially in this case?

(Note: I’m quite new to the whole concept of fluent interfaces, so please expect no prior knowledge on this.)

Reference: http://codefirst.codeplex.com/

2

Data annotations are static, for instance this method declaration cannot change at runtime:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>[MinLength(5)]
[MaxLength(20,ErrorMessage="Le nom ne peut pas avoir plus de 20 caractères")]
public new string Name { get; set; }
</code>
<code>[MinLength(5)] [MaxLength(20,ErrorMessage="Le nom ne peut pas avoir plus de 20 caractères")] public new string Name { get; set; } </code>
[MinLength(5)]
[MaxLength(20,ErrorMessage="Le nom ne peut pas avoir plus de 20 caractères")]
public new string Name { get; set; }

The fluent interface can be dynamic:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>if (longNamesEnabled)
{
modelBuilder.Entity<Manager>().Property(ma => ma.Name)
.HasMaxLength(100);
}
else
{
modelBuilder.Entity<Manager>().Property(ma => ma.Name)
.HasMaxLength(20);
}
</code>
<code>if (longNamesEnabled) { modelBuilder.Entity<Manager>().Property(ma => ma.Name) .HasMaxLength(100); } else { modelBuilder.Entity<Manager>().Property(ma => ma.Name) .HasMaxLength(20); } </code>
if (longNamesEnabled)
{
    modelBuilder.Entity<Manager>().Property(ma => ma.Name)
    .HasMaxLength(100);
}
else
{
    modelBuilder.Entity<Manager>().Property(ma => ma.Name)
    .HasMaxLength(20);
}

not to mention the code can be reused between properties.

7

I don’t think that statement should be broadly applied; it is very specific to Code First. In Code First, data annotations include only a subset of the functionality that is available in the fluent API. In other words, there are certain model configurations that can only be done using the fluent API.

For example, here are some of the things that can’t be specified using the annotations:

  • The precision of a DateTime property
  • The precision and scale of numeric properties
  • A String or Binary property as fixed-length
  • A String property as non-unicode
  • The on-delete behavior of relationships
  • Advanced mapping strategies

Personally, I tend to use the validation-related data annotations whenever possible since other technologies like MVC can also take advantage of these. For everything else, I prefer the fluent API.

4

Answer to your question is provided in the link.

Then you define your constraints applicable to your domain within this method programmatically.

Basically, it is more or less preference to use Attributes vs programmatic approach, where programmatic approach has more control over the entity. However, there a custom way of adding Attributes to decorate your model that you may look as well.

When using this approach you may even describe relations between tables and columns.
Bottom line, if you are willing to have more fine-grain control over your domain you may use this new approach that comes with EF4.1.

However, for common scenarios of validation applying Attributes should work fine because it is robust to cover most cases; and in addition it might save you time.

5

My thought is that they recommend the fluent API for code first implementations because you explicitly describe how the relationships are created in the database. If you use data annotations the database created by Entity Framework may not be what you expect. Your initial example is very simple so, like you, I would just use the data annotation method.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật