473,574 Members | 3,218 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1119
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

ProductControll er
or
ProductManager
class.
ProductControll er.AddNew(Produ ct p)
{
if(!p.IsNew)
{
throw new MyException("Yo u are trying to add an existing Product");
}

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

}
ProductControll er.UpdateExisti ng(Product p)
{
if(p.IsNew)
{
throw new MyException("Yo u 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.space s.live.com/Blog/cns!A68482B9628 A842A!139.entry



"Navaneeth. K.N" <Na*********@di scussions.micro soft.comwrote in message
news:AE******** *************** ***********@mic rosoft.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
9230
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 C++. I find my self sometimes, trying Object app = Object(); Object *app = Object(); Object app = new Object();
2
3649
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 parameter, connects to the database , executes the string (in this case SQL) in the form of a SqlDataReader and then returns the SqlDataReader. Here's...
0
4211
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 Applications and owner of Access Microsystems. Doug can be reached at doug@accessmicrosystems.com....
14
2603
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 to use web services, but we have like hundreds of different methods you can call and what I wanted to do, is add a couple of generic webmethods...
4
1715
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
1732
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.. say we have a static settings class, and it has some OnChange event. The object I am writing will add a method to the settings OnChange event in its...
9
1326
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 the best way to get all this information into the object to just have a really long __init__ method that takes each argument? Does a question like...
7
3573
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 - FirstName - LastName - CompanyId (many-to-one )
11
1633
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 10 or more include statements. I am wonering if there is a better way? It's bit annoying you can't build packages like Java. Cheers Phil
0
8101
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8271
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7858
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8137
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6511
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5654
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5335
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3793
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1099
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.