What is the best/good way to apply custom behavior to a derived entity in the manager model?
Say ProductAEntity and ProductBEntity both derive from abstract Product. ProductAEntity and ProductBEntity have different discontinued behaviors.
public abstract class ProductEntity
{
// generic Product data
}
public class ProductAEntity : ProductEntity
{
// ProductAEntity data
}
public class ProductBEntity : ProductEntity
{
// ProductBEntity data
}
With a domain model I could just make an abstract discontinue method on Product and force ProductA and ProductB to implement it and everything would be fine. However, I am confused on how to best do this in a manager model.
The best solution I have come up with so far is to have the ProductManager supply a discontinue method and take in a Product. Then determine what type of product it is and call an appropriate object or method.
public class ProductManager
{
public void DiscontinueProduct(ProductEntity product)
{
IDiscontinueStrategy strategy;
// what is the best way to model behavior for different entities ?????
if (product.GetType() == typeof(ProductAEntity))
{
strategy = new DefaultDiscontinueStrategy();
}
else if (product.GetType() == typeof(ProductBEntity))
{
strategy = new CustomDiscontinueStrategy();
}
else
{
throw new ApplicationException("Unknown Product type, cannot create discontinue strategy!");
}
strategy.Discontinue(product);
}
}
public interface IDiscontinueStrategy
{
void Discontinue(ProductEntity product);
}
public class DefaultDiscontinueStrategy : IDiscontinueStrategy
{
#region IDiscontinueStrategy Members
public void Discontinue(ProductEntity product)
{
throw new Exception("The method or operation is not implemented.");
}
#endregion
}
public class CustomDiscontinueStrategy : IDiscontinueStrategy
{
#region IDiscontinueStrategy Members
public void Discontinue(ProductEntity product)
{
throw new Exception("The method or operation is not implemented.");
}
#endregion
}
What I don’t like about this is the if-else statements to determine the product type, but I may just have to live with it.
How do you handle these situations?
Thanks,
Joe