Implementing Composite Pattern with Entity Framework Code First – Part 2

17 במרץ 2011

In my previous blog post I described the way you can implement Composite pattern using Entity Framework Code First. The sample uses EFCodeFirst CTP 5 which suffers from a minor bug related to the composite relationship. Microsoft just released EF 4.1 RC which fixes the issue. Now, you can implement your composite pattern in a clean fashion. You can find the fixed composite pattern sample here.  

Using jQuery Grid with ASP.NET MVC

11 במרץ 2011

Many newcomers to ASP.NET MVC are asking about a replacement for the classical ASP.NET WebForms GridView server control.  As always (almost) there are 3 possible solutions: Do it your self - Write an HTML helper that renders some TABLE, TR and TD elements. For sure this is the hardest solution but it gives you absolute control over grid behavior and there is no need to learn new API because you are the owner of every bit and byte. ...
7 comments

Implementing Composite Pattern with Entity Framework Code First

2 במרץ 2011

The full sample code can be downloaded here Entity Framework allows us to map object relationships into a database. One of the useful relationship is the Composite Pattern. Suppose we have implemented the following class diagram: As you can see this is a classical implementation of the Composite design pattern, where Group holds a collection of polymorphic items through the property Items of type BookItem. We would like to save this model into a database using Entity Framework Code First.According to ECCodeFirst coding convention guidelines we should define the following classes:public abstract class BookItem {     public int ID { get; set; }     public virtual Group Parent { get; set; }     public abstract void Accept(IBookVisitor visitor); } public class Group : BookItem {     public string Name { get; set; }     public virtual ICollection<BookItem> Items { get; set; }     public Group()     {         this.Items = new List<BookItem>();     }     public override void Accept(IBookVisitor visitor) {...} } public class Person : BookItem {     public string FirstName { get; set; }     public string LastName { get; set; }     public virtual ICollection<Address> Addresses { get; set; }     public Person()     {         this.Addresses = new List<Address>();     }     public override void Accept(IBookVisitor visitor) {...} } public class Address {     public int ID { get; set; }     public string Value { get; set; }     public string Description { get; set; }     public virtual Person Parent { get; set; }     public int PersonID { get; set; }     public void Accept(IBookVisitor visitor) {...} } public class BookContext : DbContext {     public DbSet<BookItem> Items { get; set; }     public BookContext()         : base("AddressBook")     { }     protected override void OnModelCreating(ModelBuilder model)     {         model.Entity<BookItem>().ToTable("BookItems");         model.Entity<Person>().ToTable("Persons");         model.Entity<Group>().ToTable("Groups");         model.Entity<Address>().HasRequired(a => a.Parent) ...