Reputation: 1577
I'd like to hear some suggestions and opinions about the following:
Let's say you have some entities: NewsPiece, BlogPost, Article
In general the are quite similar to each other, so I've made a generic class
public abstract class InformationItem<TParent, TChild, TLike>
where TParent : InformationItem<TParent, TChild, TLike>
where TChild : InformationItemChild<TParent, TChild, TLike>
where TLike : InformationItemLike<TParent, TChild, TLike>
{
[Key]
public int Id { get; set; }
protected ICollection<TChild> childItems;
public virtual ICollection<TChild> ChildItems
{
get { return childItems ?? (childItems = new List<TChild>()); }
protected set { childItems = value; }
}
//and so on...
}
Similar definitions are on the Child and Like classes, so i was able to define BaseInformationService and use it as basic implementation for all operations with my information items (Create, edit, post comment, get recent, etc...).
But recently I've faced a problem - such implementation enforces the structure of the derived classes and makes classes that don't need comments (for example) to have unnecessary functionality. So I've decided to make this implementation a little bit more flexible - i want to have something like InformationItemServiceBuilder that will have methods such as .WithComments or .WithLikes which will be able to construct information item service with needed functionality only.
Which approach or design pattern would you suggest? Should i try decorator, or something else will fit better? Thank you in advance for you replies.
Upvotes: 0
Views: 91
Reputation: 42246
Have you considered using the a dynamic
object? The DLR allows you to create properties and methods dynamically. There are a few open source projects that provide custom implementations which would let you easily add properties and methods dynamically, as well as dynamically implement known interfaces. The dynamic interface implementations rely on dynamic proxies and you can mix-in interfaces (e.g. ISupportComments
) to add your partitioned functionality.
I would recommend taking a look at the following two projects:
In addition, Jeremy Miller has a good article on implementing persistent extension properties that seem to be along the lines of what you are trying to build. It might give you some ideas to give it a read at: http://codebetter.com/jeremymiller/2010/02/16/our-extension-properties-story/
Upvotes: 1