Given the following code:
public class Product
{
public Guid Id {get;set;}
}
public static class Products
{
public static Guid ProductWithSpecialProcessing = new Guid ("12345...");
}
...
public void ProcessOrder(IEnumerable<Product> products)
{
foreach(var product in product)
{
if(product.Id == Products.ProductWithSpecialProcessing)
DoSpecialProcessing();
DoSomeStuff(product);
}
}
Is there a better way to identify the special product without having to hard code the ID in code, an app.config file or by having some normalized database structure that identifies “special products” (there are more than 1 “special processing rules” and they are different for different products), for example:
CREATE TABLE SpecialProcessing (ProductWithSpecialProcessing uniqueidentifier)
I would go the inheritance route.
The first step would be to create your
SpecialProductancestors ofProduct, with some way of linking them to your database records. Possibilities include a mapping table in the database or the class could define its own mapping.Then your data-access layer would have a factory that would return a set of
Productscreated by the mapping you defined above.The rest of your application works with these
Productswithout actually caring when they are “special” or not.Something like this…