C# Generic Messaging Pattern

Posted on 2011-09-08
Medium Priority
Last Modified: 2012-05-12
I'm not sure if I can do this or not, but I am building the service layer of an application which needs to add one type of entity to another. An example might be adding an article to a category. It does this through a service class, for example:
public class ArticleService { 
     public IResponse AddArticleToCategory(IAddRelatedItemRequest<Category, Article> request) { 
     // do stuff with the request 

Open in new window

I would like the IAddRelatedItemRequest interface to be generic so it can be used for any addition request, similar to:

public interface IAddRelatedItemRequest<T, U> 
where T : class 
where U : class { 
    Object Key { get;set; } 
    List<Object> RelatedKey { get;set; } 

Open in new window

What is happening is that the request requires the primary key of the item (e.g. the category) and a list of the primary keys of the related items (e.g. the articles). The AddCommentToArticle class in the concrete ArticleService then retrieves the item by its key and then adds the related key items to it.   Note that the primary keys of the entities are not necessarily integers, they could be uniqueidentifiers.

I want to strongly type the request somehow, so instead of an object and a list of objects being supplied, the interface takes the types of the primary keys of the two entities.

The intereface would then look something like

public interface IAddRelatedItemRequest<T, U> 
where T : class 
where U : class { 
    TPrimaryKeyType Key { get;set; } 
    List<UPrimaryKeyType> RelatedKey { get;set; } 

Open in new window

Obviously this code is not correct but illustrates the end result.

Ideally I would like to somehow extract the type of the entity's Id from the generic type and include that in the interface. Is this possible?

What I don't want to happen is supply the actual entity and a list of related entities - it needs to be done through primitive primary keys.
I don't particularly want to supply the two types through Generics as it makes for less readable code and the keys of the objects could potentially change type.
Question by:pipelineconsulting
1 Comment
LVL 16

Accepted Solution

SriVaddadi earned 2000 total points
ID: 36539443
Define an interface  
interface IPrimaryKey
    T Key {get;set;}
    List<T> RelatedKey {get;set;}

public interface IAddRelatedItemRequest<T, U>
where T : class
where U : IPrimaryKey

Means that your Article class has to implement IPrimaryKey interface and could specify the type


Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

The PowerShell Core 6.0 of .NET release is just the beginning. The upcoming PowerShell Core 6.1 would have artificial intelligence and internet of things capabilities. So many things to look forward to in the upcoming release.
This article describes and provides a custom-made tool I wrote to give businesses a means of identifying commercial music content, without having to expend too much effort. Business recordings are easily identified from possibly illegal music files …
SQL Database Recovery Software repairs the MDF & NDF Files, corrupted due to hardware related issues or software related errors. Provides preview of recovered database objects and allows saving in either MSSQL, CSV, HTML or XLS format. Ensures recov…
Stellar Phoenix SQL Database Repair software easily fixes the suspect mode issue of SQL Server database. It is a simple process to bring the database from suspect mode to normal mode. Check out the video and fix the SQL database suspect mode problem.

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question