473,761 Members | 5,839 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

OO class design question (logic placement)...

Here is a simple OO design question...

I have a Contract class. The user can either save an existing contract or
they start off fresh with a blank contract, fill in the data and then save a
"new" contract.

I have a method in my contract class called "Save" which is called like
this...

dim oContract as new Contract
objContract.Sav e(me.txtContrac tNo)

when the user clicks the 'Save" icon on the contract form.

As you can see from the above code, the contract number is a paramter of the
"Save" method.

My question is this... Where should the code be placed that actually
determines whether we are saving an existing contract or creating a new
one?? WOuld this be placed in the Contract class that is handling the save
operation or should it be left to the programmer to add logic to do this on
the form's save button?

I am thinking that it would be best to place this code in the Contract
class. Basically the logic is that if the contract number being passed to
the Save method is empty then we need to get the next available contract
number and save the contract using that #. If the contract number being
passed in has a value, then we will simply save the exisiting contract.

I think that it makes the most sense to place that logic within the class -
then the programmer does not need to do any checking - as it will always be
done by the class "Save" method...

Does this makes sense??

Thanks Brad
Jul 11 '07 #1
29 2227
"Brad Pears" <br***@truenort hloghomes.comwr ote in
news:u1******** ******@TK2MSFTN GP04.phx.gbl:
My question is this... Where should the code be placed that actually
determines whether we are saving an existing contract or creating a
new one?? WOuld this be placed in the Contract class that is handling
the save operation or should it be left to the programmer to add logic
to do this on the form's save button?
I would place the data inside the contact object. The contact object should
be aware of it's present state (i.e. new vs. update).

Take a look at LLBLGen Pro - it's DAL framework does this sort of thing.
Jul 11 '07 #2
>
I think that it makes the most sense to place that logic within the
class - then the programmer does not need to do any checking - as it will
always be done by the class "Save" method...

Does this makes sense??
The object should be able to set its state upon instantiate.

Upon the instantiation of the object, the object state is I am NEW, and I am
not DIRTY. I am not DIRTY, because no change has taken place within me.

If a change has taken place within me (the object), then I am marked DIRTY.

I the object will be OLD, if data from the database has populated me. I will
be DIRTY, because the database logic has populated me. I will be marked not
DIRTY.

I have another state called MARKED_FOR_DELE TION too.

1) Upon the instantiation of me (the object), my state will be NEW. I am
NEW no data from the database has populated me, so when I am saved, my data
will be added/inserted into the database.

2) Upon instantiation I (the object), I am NEW and not DIRTY, but data has
populated me so I am going to be marked as OLD and not DIRTY. When I am
saved, I am going to update the database with my data.
Oh, my Save method has been invoked.

What is my state me (the object)?

1) I am DIRTY some change as taken place with in me. If I am not DIRTY, then
don't do anything with me and ignore me.

2) I am NEW and DIRTY, so add my data to the database.

3) I am OLD and DIRTY, so update my data in the database.

Hey, I am none of that. I am MARKED_FOR_DELE TION.

1) If I am NEW and MARKED_FOR_DELE TION, don't do anything with me (the
object) as I have no data that needs to be deleted from the database.

2) If I am OLD and MARKED_FOR_DELE TION, I have data in the database so
delete my data.

Hey, me (the object) those could possibly be my states and how my Save
method might work.

Jul 12 '07 #3
Brad,

I'd suggest it's not really an OO question as much as general forms decision
making.

One approach is to vear from the other answers you have (although not to
contradict anything that's been said) and suggets that you split up the
concept of the Contract object and the Contract form.

The object understands how to load itself, save itself, and you could even
(for example) have a static method on the contract obect that checks whether
an instance of this object already exists.

The form simply gets data from the user and closes down once the form has
been "OK'd". From here, your code would query properties on the Contract
form object, passing them to a contract object. The final step is to call
save on the contract object, which decides whether it needs to create a new
object, update/overwrite to an existing object...

The File object works this way. There's the open dialog, save dialog, and
the File object.... When you open a file you first query what file is to be
opened using the file open dialog, and then use the file object's filename
property to open that file, read from it etc.
The second approach would be to simply use the data controls to build your
form, and use the view/edit/save funcationality that comes with the
gridview, databindernavig ator and so on...
It all depends the context of the application...
ASP.Net: http://www.ondotnet.com/pub/a/dotnet...spdatactl.html
Windows: http://www.codeproject.com/csharp/Bi...ndingNavCS.asp
"Brad Pears" <br***@truenort hloghomes.comwr ote in message
news:u1******** ******@TK2MSFTN GP04.phx.gbl...
Here is a simple OO design question...

I have a Contract class. The user can either save an existing contract or
they start off fresh with a blank contract, fill in the data and then save
a "new" contract.

I have a method in my contract class called "Save" which is called like
this...

dim oContract as new Contract
objContract.Sav e(me.txtContrac tNo)

when the user clicks the 'Save" icon on the contract form.

As you can see from the above code, the contract number is a paramter of
the "Save" method.

My question is this... Where should the code be placed that actually
determines whether we are saving an existing contract or creating a new
one?? WOuld this be placed in the Contract class that is handling the save
operation or should it be left to the programmer to add logic to do this
on the form's save button?

I am thinking that it would be best to place this code in the Contract
class. Basically the logic is that if the contract number being passed to
the Save method is empty then we need to get the next available contract
number and save the contract using that #. If the contract number being
passed in has a value, then we will simply save the exisiting contract.

I think that it makes the most sense to place that logic within the
class - then the programmer does not need to do any checking - as it will
always be done by the class "Save" method...

Does this makes sense??

Thanks Brad

Jul 12 '07 #4
Very well written. Thanks for that... Make some sense now...

Thanks, Brad

"Mr. Arnold" <MR. Ar****@Arnold.c omwrote in message
news:uD******** ******@TK2MSFTN GP02.phx.gbl...
>
>>
I think that it makes the most sense to place that logic within the
class - then the programmer does not need to do any checking - as it will
always be done by the class "Save" method...

Does this makes sense??

The object should be able to set its state upon instantiate.

Upon the instantiation of the object, the object state is I am NEW, and I
am not DIRTY. I am not DIRTY, because no change has taken place within me.

If a change has taken place within me (the object), then I am marked
DIRTY.

I the object will be OLD, if data from the database has populated me. I
will be DIRTY, because the database logic has populated me. I will be
marked not DIRTY.

I have another state called MARKED_FOR_DELE TION too.

1) Upon the instantiation of me (the object), my state will be NEW. I am
NEW no data from the database has populated me, so when I am saved, my
data will be added/inserted into the database.

2) Upon instantiation I (the object), I am NEW and not DIRTY, but data
has populated me so I am going to be marked as OLD and not DIRTY. When I
am saved, I am going to update the database with my data.
Oh, my Save method has been invoked.

What is my state me (the object)?

1) I am DIRTY some change as taken place with in me. If I am not DIRTY,
then don't do anything with me and ignore me.

2) I am NEW and DIRTY, so add my data to the database.

3) I am OLD and DIRTY, so update my data in the database.

Hey, I am none of that. I am MARKED_FOR_DELE TION.

1) If I am NEW and MARKED_FOR_DELE TION, don't do anything with me (the
object) as I have no data that needs to be deleted from the database.

2) If I am OLD and MARKED_FOR_DELE TION, I have data in the database so
delete my data.

Hey, me (the object) those could possibly be my states and how my Save
method might work.

Jul 12 '07 #5
Hmmm sounds intertesting enough...

I do have another question though...

You mentioned about having a static method on the contract object that
checks whether an instance of the object already exisits or not...
How would you code such a method?? How could you call this method if in
fact the object does not exist? ( I am assuming this is where the static
part comes in - I just don;t understand what you mean) Since I am very new
to OO design, I do not quite understand that one...

I do like the idea of having a property on the object once instantiated that
is the object's "state" which would indicate once the save button is hit
what should be done with the object ... i.e. save new, save existing,
delete me, do nothing etc.. as meniotned by the fellow who wrote the 2nd
response...

Thanks, Brad

"Daniel Bass" <da***********@ blueCAPSbottle. comFIRSTwrote in message
news:ej******** ******@TK2MSFTN GP05.phx.gbl...
Brad,

I'd suggest it's not really an OO question as much as general forms
decision making.

One approach is to vear from the other answers you have (although not to
contradict anything that's been said) and suggets that you split up the
concept of the Contract object and the Contract form.

The object understands how to load itself, save itself, and you could even
(for example) have a static method on the contract obect that checks
whether an instance of this object already exists.

The form simply gets data from the user and closes down once the form has
been "OK'd". From here, your code would query properties on the Contract
form object, passing them to a contract object. The final step is to call
save on the contract object, which decides whether it needs to create a
new object, update/overwrite to an existing object...

The File object works this way. There's the open dialog, save dialog, and
the File object.... When you open a file you first query what file is to
be opened using the file open dialog, and then use the file object's
filename property to open that file, read from it etc.
The second approach would be to simply use the data controls to build your
form, and use the view/edit/save funcationality that comes with the
gridview, databindernavig ator and so on...
It all depends the context of the application...
ASP.Net: http://www.ondotnet.com/pub/a/dotnet...spdatactl.html
Windows: http://www.codeproject.com/csharp/Bi...ndingNavCS.asp
"Brad Pears" <br***@truenort hloghomes.comwr ote in message
news:u1******** ******@TK2MSFTN GP04.phx.gbl...
>Here is a simple OO design question...

I have a Contract class. The user can either save an existing contract or
they start off fresh with a blank contract, fill in the data and then
save a "new" contract.

I have a method in my contract class called "Save" which is called like
this...

dim oContract as new Contract
objContract.Sa ve(me.txtContra ctNo)

when the user clicks the 'Save" icon on the contract form.

As you can see from the above code, the contract number is a paramter of
the "Save" method.

My question is this... Where should the code be placed that actually
determines whether we are saving an existing contract or creating a new
one?? WOuld this be placed in the Contract class that is handling the
save operation or should it be left to the programmer to add logic to do
this on the form's save button?

I am thinking that it would be best to place this code in the Contract
class. Basically the logic is that if the contract number being passed to
the Save method is empty then we need to get the next available contract
number and save the contract using that #. If the contract number being
passed in has a value, then we will simply save the exisiting contract.

I think that it makes the most sense to place that logic within the
class - then the programmer does not need to do any checking - as it will
always be done by the class "Save" method...

Does this makes sense??

Thanks Brad


Jul 12 '07 #6
Where can I take a look at what you mentioned there - the LLBLGen Pro?

Thanks, Brad
"Spam Catcher" <sp**********@r ogers.comwrote in message
news:Xn******** *************** ***********@127 .0.0.1...
"Brad Pears" <br***@truenort hloghomes.comwr ote in
news:u1******** ******@TK2MSFTN GP04.phx.gbl:
>My question is this... Where should the code be placed that actually
determines whether we are saving an existing contract or creating a
new one?? WOuld this be placed in the Contract class that is handling
the save operation or should it be left to the programmer to add logic
to do this on the form's save button?

I would place the data inside the contact object. The contact object
should
be aware of it's present state (i.e. new vs. update).

Take a look at LLBLGen Pro - it's DAL framework does this sort of thing.

Jul 12 '07 #7
You mentioned about having a static method on the contract object that
checks whether an instance of the object already exisits or not...
How would you code such a method?? How could you call this method if
in
fact the object does not exist? ( I am assuming this is where the
static
part comes in - I just don;t understand what you mean)
It sounds like you are talking about the singleton pattern

In this case Static means Shared (Static is the c# keyword)

Thus:
-----------------------------------------------------------
Private Shared Singleton as SomeClass
Public Shared Function GetSingleton() as SomeClass
If Singleton is nothing then
Singleton = New SomeClass
Endif
return Singleton
End Function
-----------------------------------------------------------
--
Rory
Jul 12 '07 #8
Where can I take a look at what you mentioned there - the LLBLGen Pro?
>
www.llblgen.com

--
Rory
Jul 12 '07 #9
That was very well written. I had never thought of having a property 'State'
that is set for the object but I think it makes sense...

Basically what I have happeneing now is that the "Save" method on the
business class "contract" object calls an "update" method on the contract
"data class" object. ( I split the object so that I have a business object
"contract" and a data object "contract_data" ). This update method in turn
runs a stored procedure to do the job. The stored procedure itself
determines whether or not to "update" the data or "insert" the data. There
is a completely separate method to "delete" a contract which in turn runs a
stored procedure to specifically do just that...

So implementing a state property really doesn't buy me anything in this
instance BUT I do like the idea of having a state on every object and I
think it makes sense. Is this a common thing to have?? I have not seen a lot
of this in reading OO material or viewing OO design techniques/coding
techniques...

Thanks, Brad

"Mr. Arnold" <MR. Ar****@Arnold.c omwrote in message
news:uD******** ******@TK2MSFTN GP02.phx.gbl...
>
>>
I think that it makes the most sense to place that logic within the
class - then the programmer does not need to do any checking - as it will
always be done by the class "Save" method...

Does this makes sense??

The object should be able to set its state upon instantiate.

Upon the instantiation of the object, the object state is I am NEW, and I
am not DIRTY. I am not DIRTY, because no change has taken place within me.

If a change has taken place within me (the object), then I am marked
DIRTY.

I the object will be OLD, if data from the database has populated me. I
will be DIRTY, because the database logic has populated me. I will be
marked not DIRTY.

I have another state called MARKED_FOR_DELE TION too.

1) Upon the instantiation of me (the object), my state will be NEW. I am
NEW no data from the database has populated me, so when I am saved, my
data will be added/inserted into the database.

2) Upon instantiation I (the object), I am NEW and not DIRTY, but data
has populated me so I am going to be marked as OLD and not DIRTY. When I
am saved, I am going to update the database with my data.
Oh, my Save method has been invoked.

What is my state me (the object)?

1) I am DIRTY some change as taken place with in me. If I am not DIRTY,
then don't do anything with me and ignore me.

2) I am NEW and DIRTY, so add my data to the database.

3) I am OLD and DIRTY, so update my data in the database.

Hey, I am none of that. I am MARKED_FOR_DELE TION.

1) If I am NEW and MARKED_FOR_DELE TION, don't do anything with me (the
object) as I have no data that needs to be deleted from the database.

2) If I am OLD and MARKED_FOR_DELE TION, I have data in the database so
delete my data.

Hey, me (the object) those could possibly be my states and how my Save
method might work.

Jul 12 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1459
by: Richard Stride | last post by:
I am fairly new to PHP and have written an application for managing spam in a quarantine like environment. Now the problem has arisen that Branding will eventually come into the picture as well as localisation. I prefer to embed php code into the HTML segments of the page, the dynamic PHP components having been populated by middleware classes that can take care of all the business logic.
0
1352
by: radha | last post by:
Hai all, My problem not just releated to sql. My problem is releated to good logic. I should not this type of questions hear. But just i am trying. If any body gave suggestions, or help i am the lockest person that time. I just started working with sql server 3 Months back.
22
2137
by: Nunaya | last post by:
We have developed two objects called "customer" and "salesrep". For simplicity, assume that these two objects will talk directly to a database. The database has three tables: customers, customersalesreps (allows multiple salesreps to be associated with a customer), and salesreps. The customer object has business rules that allow manipulation of customer information (add,update,delete,select,etc). The salesrep object has business rules...
3
1573
by: Jack | last post by:
Dear all, I'm using a commercial library that I cannot modify. There's this one class in the library which is almost 90% usable. But unfortunately, I have to reimplement 3 member functions. The problem is the original class is not designed very well, because it is assumed to be the final class. Because I have to modify some private members in the reimplemented functions. What should I do? Design a new class or doing something else?
9
3349
by: Richard Brown | last post by:
Can anyone give me a good argument one way or another? I have an 'address' set of fields that are used in various situations (a client has an address, a destination has an address, etc). These fields are all normalized into a seperate database table from the client and destination tables. My question is, which is better to create, a User Control, or a generic class that will add controls to a form directly, applying its common event...
9
1861
by: MariusI | last post by:
Consider the following class layout public class Order { public ProductOrder AddProductOrder(/* variables required to create a product order */) { /* Check if the product order can be added to the order */ }
7
1180
by: Ole Nielsby | last post by:
I want to create a class with a variably-sized array inlined. The array size will be known at each instantiation but not at compile time. Once created, arrays won't grow or shrink and their contents won't change. The array length will be accessible via a smart pointer located before the array. On destruction, it must be fetched before the smartpointer containing it is destroyed. The arrays will typically hold smart pointers, so proper
2
18377
by: kodart | last post by:
Introduction Performance is the main concern to most server application developers. That’s why many of them anticipate using .NET platform to develop high performance server application regardless of the security features it provides. Microsoft Windows provides a high performance model that uses I/O completion port (IOCP) to process network events. IOCP provides best performance, but difficult to use due to lack of good code samples and...
55
3987
by: tonytech08 | last post by:
How valuable is it that class objects behave like built-in types? I appears that the whole "constructor doesn't return a value because they are called by the compiler" thing is to enable built-in-like behavior for objects of class type. That leads to the "necessity" for the exception machinery so that errors from constructors can be handled. Is all that complexity worth it just to get built-in-like behavior from class objects? Maybe a...
0
9554
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9377
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10136
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9925
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 most users, this new feature is actually very convenient. If you want to control the update process,...
1
7358
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5266
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3913
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3509
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.