pipelineconsulting
asked on
C# Generic Messaging Pattern
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:
I would like the IAddRelatedItemRequest interface to be generic so it can be used for any addition request, similar to:
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
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.
public class ArticleService {
public IResponse AddArticleToCategory(IAddRelatedItemRequest<Category, Article> request) {
// do stuff with the request
}
}
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; }
}
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; }
}
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.