I have a question about building a strategy pattern with CSLA. Here is my problem.
I have 3 different types of products that I need to model. So following the standard practice of Abstracting the things that are similar and implementing the things that are different I have come up with a Strategy pattern to model it.
Here are the classes involved.
AbstractProduct : ReadOnlyBase
ConcreteProductA:AbstractProduct
ConcreteProductB:AbstractProduct
ConcreteProductC:AbstractProduct
With this design you really cant use a standard static FactoryMethod because Static methods in a Abstract base class can not be overriden. So I am wondering if anyone here has done this and can give me a quick over view of how to handle object creation. As well as object initialization.
Thanks,
PD
{
switch(type)
{
case DealType.TypeA:
return DealA.NewDeal();
case DealType.TypeB:
return DealB.NewDeal();
case DealType.TypeC:
return DealC.NewDeal();
default:
throw new NotSupportedException("Invalid deal Source. Cannot get Deal from the given type");
}
}
Thanks Pradeep,
Looks like we both did the same thing :-)... I think it is a good solution. In fact some OOP folks may argue that this is the best way. That perhaps object creation is not a role that a business object should worry about.
Anyone care to chime in with other possible ways?
PD
This is one reason I give strong preference to using factory methods over calling a constructor directly - you get this kind of flexibility.
I have just hit a very similar situation but can't quite seem to get it to work like this.
I have a class based on BusinessListBase defined as follows
public abstract class Notification<T, C> : BusinessListBase<T, C>I then have other classes defined as follows
public
class NotificationTypeA : Notification<NotificationTypeA, NotificationTypeAChild>Then I have a method defined in Notification like this
public static Notification<T, C> GetNotifications(NotificationTypeEnum notificationType)And then an example of the actual GetNotifications implementation is
public static NotificationTypeA GetNotifications()This won't compile and generates the exception
Error 1 Cannot implicitly convert type 'NotificationTypeA' to 'Notification<T,C>'
What am I doing wrong here? I am still getting to grips with generics and can't figure it out.
TIA.
John.
Copyright (c) Marimer LLC