471,350 Members | 2,028 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,350 software developers and data experts.

Best method for designing this class

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
Jun 27 '08 #1
3 1062
Navaneeth.K.N wrote:
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.

--
Göran Andersson
_____
http://www.guffa.com
Jun 27 '08 #2
Thank you guffa

"Göran Andersson" wrote:
Navaneeth.K.N wrote:
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.

--
Göran Andersson
_____
http://www.guffa.com
Jun 27 '08 #3

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



"Navaneeth.K.N" <Na*********@discussions.microsoft.comwrote in message
news:AE**********************************@microsof t.com...
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

Jun 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Joe Bloggs | last post: by
reply views Thread by Anonieko Ramos | last post: by
14 posts views Thread by Bert Vandenberghe | last post: by
4 posts views Thread by ddtl | last post: by
9 posts views Thread by John Salerno | last post: by
7 posts views Thread by Steve | last post: by
11 posts views Thread by Phil Latio | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.