Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

C# Generic Messaging Pattern

Posted on 2011-09-08
1
Medium Priority
?
294 Views
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?

Note:
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.
0
Comment
Question by:pipelineconsulting
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 16

Accepted Solution

by:
SriVaddadi earned 2000 total points
ID: 36539443
Define an interface  
like
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

HTH
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

670 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