KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I know I'm not doing this right, but I also know there is a way to do this. I'm trying to be as generic and abstract as possible, otherwise my code is going to get real messy. So I'm using strategy pattern here as well, which is the GetAggregateClient() method. I want to have an abstract class called AbstractAggregate<T> , so that it uses generics. The generic type will be a series of data classes ( BlogItem , ResourceItem , and AskItem ), which all inherit from ListItem . So that's the background info. The problem here is that I want GetAbstractAggregate() to return an instance of one of the client classes that implements AbstractAggregate , with the type of item specified depending on the enum passed in. However, I cannot return an AbstractAggregate<T> . The compiler won't let me, and that makes sense since, since the AbstractAggregateFactory class is not a generic. Does anyone know the best way to do this? Thanks a lot. public static class AggregateHelper { public enum AggregateTypes { TankTruckBlog, AskTankTruck, Resources } } public static class AbstractAggregateFactory { public static AbstractAggregate<T> GetAggregateClient(AggregateHelper.AggregateTypes type) { switch (type) { case AggregateHelper.AggregateTypes.AskTankTruck: return new AskTankTruckAggregate<AskItem>(); case AggregateHelper.AggregateTypes.TankTruckBlog: return new TankTruckBlogAggregate<BlogItem>(); case AggregateHelper.AggregateTypes.Resources: return new ResourcesAggregate<ResourceItem>(); default: throw new AggregateDoesNotExistException(); } } } public abstract class AbstractAggregate<T> { public abstract List<T
Tags (comma-separated)
Save Edits
Cancel