Gunnar Peipman's ASP.NET blog

ASP.NET, C#, SharePoint, SQL Server and general software development topics.

Sponsors

News

 
 
 
 
 
Programming Blogs - Blog Catalog Blog Directory
 
 
 

Links

Social

Entity Framework 4.0: On the way to Composite Pattern

In my last posting about Entity Framework 4.0, Entity Framework 4.0: POCOs and table-per-type inheritance mapping, I made first generalization to my photo gallery model. I introduced GalleryItem class that is base class for all that can be added to gallery: new albums, photos and maybe videos too. Although everything works as expected my model needs some modifications because it is not very foolproof.

Analyzing current model

Let’s take a look at current model. Currently album is self-referencing class. It was okay because albums may have sub-albums. Also albums my contain photos and we had also association from Album to Photo.

But there are some nasty side effects:

  • Album contains direct association to Photos. When we add new class for videos then we must add new association to Album class. All queries for Album children go more complex because we have to query ChildAlbums, Photos and Videos. Think about gallery items view where nine items from current album are shown ordered by date.
  • Adding new classes to model that extend GalleryItem mean that we have two database relations per table: one from GalleryItem to new table and one from Album to new table.

Somehow we have to protect Album class so it doesn’t grow as new descendant is added for GalleryItem.

Solution

To solve our problem we need some modifications. We will move ParentAlbum property from Album class to GalleryItem class. This guarantees that every item in gallery may have parent album. If we have new class called Video that extends GalleryItem then Video gets parent album automatically.

As you can see from new model diagram then we don’t need association from Album to Photo anymore. We can easily add new classes that extend GalleryItem and we don’t have to change other classes. Also all classed based on GalleryItem have ParentAlbum property.

On Entity Framework 4.0 everything works fine. I changed my classes and my mappings a little bit and everything works now as expected.

Code

So, here is the code of classes and it is exactly what I wanted.


public abstract class GalleryItem
{
    public virtual int Id { get; set; }
    public virtual string Description { get; set; }
    public virtual GalleryItem ParentAlbum { get; set; }
    public virtual string Title { get; set; }
    public virtual bool Visible { get; set; }
}
 
public class Album : GalleryItem
{
    public virtual ICollection<GalleryItem> ChildItems { get; set; }
}
 
public class Photo : GalleryItem
{
    public virtual string FileName { get; set; }
    public virtual bool IsGalleryThumb { get; set; }
}

As you can see then Album class contains now only one automatic property. Although we should be more careful here (everybody can assign new value to ChildItems collection) I don’t have problem with it right now. When I discover the problem then it is time to find a solution.

Why not composite pattern?

Reader who knows patterns can see here Composite Pattern. At least our associations are made this way but GalleryItem misses some methods. I know that and I don’t even consider using Composite Pattern here. Why? Because I really don’t need it right now.

Okay, many i-know-everything gurus, idealists and other maniacs will scream now divine revelations of dark doom of my model and my application. It is blasphemy in this point not to use Composite Pattern. If these guys knew where I live then they were considering next crusade. But don’t care about them.

This far I have used my common sense to go on step by step. I’m not swimming in safe waters and most of my work here is experimenting. That’s why I don’t put any efforts to ideal architecture and packaging right now. Also there is no point to implement any pattern just because something almost looks like following this pattern.

Of course, I don’t abandon Composite Pattern completely. I made a note to my private development Wiki that there is something very similar to Composite Pattern. That’s enough for now. As a first thing I try to use the new architecture in my code.


kick it on DotNetKicks.com pimp it Progg it Shout it

Comments

DotNetShoutout said:

Thank you for submitting this cool story - Trackback from DotNetShoutout

# July 17, 2009 12:54 PM

DotNetBurner - Patterns said:

DotNetBurner - burning hot .net content

# July 17, 2009 12:56 PM

PimpThisBlog.com said:

Thank you for submitting this cool story - Trackback from PimpThisBlog.com

# July 17, 2009 12:57 PM

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# July 17, 2009 12:59 PM

progg.ru said:

Thank you for submitting this cool story - Trackback from progg.ru

# July 17, 2009 1:00 PM

Gunnar Peipman's ASP.NET blog said:

Here are my postings about Visual Studio 2010 and .Net Framework 4.0 that may be interesting to my readers

# July 17, 2009 1:04 PM

Entity Framework 4.0: On the way to Composite Pattern | ASP Scribe said:

Pingback from  Entity Framework 4.0: On the way to Composite Pattern | ASP Scribe

# July 17, 2009 1:34 PM

Entity Framework 4.0: On the way to Composite Pattern | Adobe Tutorials said:

Pingback from  Entity Framework 4.0: On the way to Composite Pattern | Adobe Tutorials

# July 17, 2009 4:07 PM

Al said:

Why is the ChildItems collection in the Album? Would it not make sense to put it in the GalleryItem base?

# July 19, 2009 11:35 AM

DigiMortal said:

Thanks for your questions, Al! :)

Album is the only class that can contain gallery items. Photo cannot have children. Neither does Video that I plan to add. When I move ChildItems to GalleryItem then I open up a possibility to Photo and Video to have child items. And this is something I don't want.

# July 19, 2009 1:54 PM

Sanjeev Agarwal said:

Daily tech links for .net and related technologies - July 18-21, 2009 Web Development A (less) simple

# July 20, 2009 5:11 AM

Gunnar Peipman's ASP.NET blog said:

Entity Framework 4.0 is able to generate database schema based on model. If you built your model first

# July 23, 2009 12:44 PM

Entity Framework 4.0: Generating SQL script from model | rapid-DEV.net said:

Pingback from  Entity Framework 4.0: Generating SQL script from model | rapid-DEV.net

# July 23, 2009 1:40 PM

Entity Framework 4.0: Generating SQL script from model | I love .NET! said:

Pingback from  Entity Framework 4.0: Generating SQL script from model | I love .NET!

# July 23, 2009 2:33 PM

Gunnar Peipman's ASP.NET blog said:

MyGallery2010 is my simple photo gallery system built on Visual Studio 2010 and .Net Framework 4.0 technologies

# July 24, 2009 11:01 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)