views:

52

answers:

1

In my line of business we have Products. These products can be modified by a user by adding Modifications to them. Modifications can do things such as alter the price and alter properties of the Product. This, to me, seems to fit the Decorator pattern perfectly.

Now, envision a database in which Products exist in one table and Modifications exist in another table and the database is hooked up to my app through the Entity Framework. How would I go about getting the Product objects and the Modification objects to implement the same interface so that I could use them interchangeably?

For instance, the kind of things I would like to be able to do: Given a Modification object, call .GetNumThings(), which would then return the number of things in the original object, plus or minus the number of things added by the modification.

This question may be stemming from a pretty serious lack of exposure to the nitty-gritty of EF (all of my experience so far has been pretty straight-forward LOB Silverlight apps), and if that's the case, please feel free to tell me to RTFM.

Thanks in advance!

Edit: It would also be nice if, given a third table, linking a Products to Modifications (one-to-many) it could reconstruct the decorated object (I realize that this is likely way out of bound for the EF to do automatically). How would you recommend going about this, and where would that code reside? Would it be part of the EF classes or would every entity I received from the DB need to be passed through some sort of "builder" to construct a decorated object from a Product and its list of Modifications?

A: 

I am not entirely sure if I understood your question correctly, but here goes: You can create partial classes to those defined in your EF model. You could define a common interface and use the partial classes to implement that interface.

For example:

public interface IProduct{
   public int GetNumThings();
}

public partial class Product : IProduct{

   public int GetNumThings()
   {
   ...
   }
}

public partial class Modification: IProduct{
   public int GetNumThings()
   {
   ...
   }
}
Adrian Grigore
This answers the problem of needing the two to implement the same interface. Is there anything that would stop the IProduct interface from containing a property of type IProduct?
Anthony Compton
@Anthony Compton: No, you could just as well use the references contained in the database. After all it appears that would need to store the reference between product and decorator anyway.
Adrian Grigore