473,320 Members | 1,831 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 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 1114
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: DrUg13 | last post by:
In java, this seems so easy. You need a new object Object test = new Object() gives me exactly what I want. could someone please help me understand the different ways to do the same thing in...
2
by: Joe Bloggs | last post by:
I have a general question on best practice regarding data access. I have the code below, a static method defined in a class that I use in a data layer dll. The method takes a string as its...
0
by: Anonieko Ramos | last post by:
ASP.NET Forms Authentication Best Practices Dr. Dobb's Journal February 2004 Protecting user information is critical By Douglas Reilly Douglas is the author of Designing Microsoft ASP.NET...
14
by: Bert Vandenberghe | last post by:
Hi, I was wondering if there are any best practices on the creation of webmethods? I'll try to explain this a little more: My problem is that we are changing an existing (large) DCOM application...
4
by: ddtl | last post by:
Hello everybody. Consider the following code: class A(object): def met(self): print 'A.met' class B(A): def met(self):
1
by: CB | last post by:
When designing an object, is there a best practice for how to allow the user of the object to tell the object to remove itself from any delegates / events it has registered with? For example.....
9
by: John Salerno | last post by:
Let's pretend I'm creating an Employee class, which I will later subclass for more specific jobs. Each instance will have stuff like a name, title, degrees held, etc. etc. So I'm wondering, is...
7
by: Steve | last post by:
I am building an object library for tables in a database. What is the best practice for creating objects like this? For example, say I have the following tables in my database: User: - Id -...
11
by: Phil Latio | last post by:
I'm designing a small framework with a lot of classes (1 file per class) and want to know the best method of calling each class? Obviously I could I call each file that is used but that could be...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.