Best method for designing this class

  • Thread starter Thread starter Navaneeth.K.N
  • Start date Start date
N

Navaneeth.K.N

Hi All,

I have to design a "Product" class. This class will contain a product
details. I have a "ProductFactory" which contain some static methods which
returns "Product" instance. I have a "IsNew" boolean property in "Product"
class which indicates the instance is a new one. This flag will be "True"
when some one creates new instance of "Product" (Product p = new Product()).
if factory is used to get a product instance, this flag will be false,
something like Product p = ProductFactory.FromId().

I have to implement save functionality for this product. How do I go about
it ? If the product is new, I need to create a new entry in the DB, if it's
an old one, I need to update the DB. Which will be the appropriate location
for putting Save method ? Is it in the "Product" class itself or will it be
better to create another class like "ProductService" and put a Save method
there which accepts "Product" instance.

Any help for this would be appreciated.

Thanks
 
Navaneeth.K.N said:
Hi All,

I have to design a "Product" class. This class will contain a product
details. I have a "ProductFactory" which contain some static methods which
returns "Product" instance. I have a "IsNew" boolean property in "Product"
class which indicates the instance is a new one. This flag will be "True"
when some one creates new instance of "Product" (Product p = new Product()).
if factory is used to get a product instance, this flag will be false,
something like Product p = ProductFactory.FromId().

I have to implement save functionality for this product. How do I go about
it ? If the product is new, I need to create a new entry in the DB, if it's
an old one, I need to update the DB. Which will be the appropriate location
for putting Save method ? Is it in the "Product" class itself or will it be
better to create another class like "ProductService" and put a Save method
there which accepts "Product" instance.

Any help for this would be appreciated.

Thanks

I assume that you are going to save the data in the same place where the
ProductFactory gets it, so you should put the Save method in some class
in the same assembly as the ProductFactory class.

If the data layer is not separated from the business layer, i.e. you
have the Product class in the same assembly as the ProductFactory class,
you can put the Save method in the Product class.
 
Thank you guffa

Göran Andersson said:
I assume that you are going to save the data in the same place where the
ProductFactory gets it, so you should put the Save method in some class
in the same assembly as the ProductFactory class.

If the data layer is not separated from the business layer, i.e. you
have the Product class in the same assembly as the ProductFactory class,
you can put the Save method in the Product class.
 
I prefer a

ProductController
or
ProductManager
class.


ProductController.AddNew(Product p)
{
if(!p.IsNew)
{
throw new MyException("You are trying to add an existing Product");
}

// ProductData pd = new ProductData(); // call DAL here

}


ProductController.UpdateExisting(Product p)
{
if(p.IsNew)
{
throw new MyException("You are trying to update an existing Product");
}
///call dal here
}



I do NOT like mixing in the .Save methods on the Product class itself.
"Seperation of Concerns" would be the catch-phrase.

Take a look here:
http://sholliday.spaces.live.com/Blog/cns!A68482B9628A842A!139.entry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top