OO business class vs data class..

B

Brad Pears

Hi guys!!! Thanks for all your input on previous OO posts. I know it will
be all the same people responding again and I really appreciate your insight
etc.. as you all appear to know what you are doing - each having your own
flare but that is what makes us individuals right?? Anyway, here is a quick
question... (or maybe not so quick)

In my business "Contract" class I have many properties.
When I set these "contract" properties in the GUI layer front end (i.e
populating the Contract business class) when the 'Save' button is clicked, I
want to pass the values to the "data" class for update/insert etc...

Do I create the same "properties" on the data class as I have on the
business class and populate the data class from the properties of the
business class? OR should I pass all the variables as a parameter list
from teh business class to the method I am invoking on the data class to do
the update/insert etc .. ?

I guess the biggest question is should data classes use the same
properties - as in the associated business class(s)? What is the best way to
get the data from teh business class to the data class?

I have been doing the following... (used only 2 properties for demonstration
sake)

The front end would set and call the update method on the business class
"clsContract" as follows

public sub btmSave_click (bunch of aprameters)
' Save the Contract data
dim oCB as new clsContract
' Set class properites
oCB.ContractNo = me.txtContractNo.text
oCB.CustID = me.txtCustID.text
' Call update method to update/insert the data
oCB.Update(oCB.ContractNo)
' clean up
oCB = nothing
end sub

public class clsContract
private sContractNo as string
private iCustID as integer
public property ContractNo as string
get
return sContractNo
end get
set (byval value as string)
sContractNo = value
end set

public property CustID as integer
get
return iCustID
end get
set (byval value as integer)
iCustID = value
end set

public sub new
mybase.new
...some code
end sub

Public sub Update(byval sContractNo as string)
' Call dataclass update method
dim oCD as new clsContract_Data
' Set data class property then update
oCD.CustID = me.CustID
oCD.update(sContractNo)
' Clean up
oCD = nothing
end sub

public class clsContract_Data
private sContractNo as string
private iCustID as integer
public property ContractNo as string
get
return sContractNo
end get
set (byval value as string)
sContractNo = value
end set

public property CustID as integer
get
return iCustID
end get
set (byval value as integer)
iCustID = value
end set

public sub new(sContractNo as string)
mybase.new
me.ContractNo = sContractNo
end sub

Public sub Update(sContractNo as string)
' Call stored procedure to do updatemethod
dim bRet as boolean = false
' Call stored procedure with appropriate parameters
bRet is RunSP("spname", me.ContractNo, me.CustID)
if not bret then
msgbox("Error")
endif
return bret
end sub




Thanks, Brad
 
M

Mr. Arnold

Do I create the same "properties" on the data class as I have on the
business class and populate the data class from the properties of the
business class? OR should I pass all the variables as a parameter list
from teh business class to the method I am invoking on the data class to
do the update/insert etc .. ?

Here is where OO comes into play.

You have busContact and it should have all of its data accessor properties.

At the UI, you have busContract.Save (note: I don't know why you're trying
to pass the ContractNo in the Save, as it should have been already populated
in the
busContract (the object).

Then at busContract.Save(), you would do this.

Dim daoc as new daoContract

daoc.Save(busContract)

Then at the daoc.Save() will actually be this within daoContract

Public sub Save(byval obj as busContract)

Then you have reference to obj.ContractNo or the properties in the
busContract (the object) you have passed to the DAL object so that DAL
Object can save the data from the Business Object.

You have reference to obj.ContractNo and all other properties in the
busContract, because the (object) was passed.

I hope you understand this and you need to read some books and see the
examples or code execution if you run a sample project from a book that was
mentioned. :)

You should throw everything out the door that you have learned previously
and start fresh.

I have been programming professionally since 1980 and started on the MS
platform in 1996. Something's I kept over the years and a lot of things I
tossed and started fresh, a blank sheet OO or OOp(s).
 
B

Brad Pears

Thanks for that... Here are a couple questions regarding your response...

1) Ok, I was assuming a brand new contact - one that has not yet been
populated from the db because it does not yet exist there - that's why I was
passing the contractno. I guess I don't really need to do though so would it
have been better to omit that and just set the ContractNo property and then
issue the update like this... (using no parameters)

' Set the busContract object properties
busContract.ContractNo = me.txtContractNo.text
' include code to set other properties the same way if they havent; already
been set elsewhere
' issue the update
busContract.update

2) Ok YES I UNDERSTAND!!! The light bulb went on!! Makes perfect sense,
pass the entire object and presto I can then use all the properties on that
business class object to do my updates!!

3) OK, so that means that you do not need to duplicate the properties of the
business class on the data class at all. Really the associated data class
will have very few properties... Only ones specific to it...

4) In looking at other OO code, I have noticed where many programmers have
simply used public variables in their classes instead of declaring them as
private and exposing public properties which get and set the private
variables... I have been using properties and private variables... What is
your take on that?? Is therte a general rule of thumb that should be
followed or can this simply be left up to the developer?? I would think it
not wise to have a pile of public variables exposed and you would lose the
ability to do consistency cheecking within the get and set blocks etc...

5) I think I am going to order that book (Expert VB 2005 Business Objects) !
I downloaded the sample chapter 6 and it looks interesting for sure. The
only way to learn this stuff though is to program from the ground up. Using
a pre-built framework application would be handy once I understand it all..
but for now I need to dive in with both feet and read about it and learn by
doing etc... Does this book deal with grassroots stuff or does it mainly
focus on the whole CSLA.net thing becasue I really need a book to do
grassroots OO stuff...


Thanks, Brad
 
R

Rory Becker

4) In looking at other OO code, I have noticed where many programmers
have simply used public variables in their classes instead of
declaring them as private and exposing public properties which get and
set the private variables... I have been using properties and private
variables... What is your take on that?? Is therte a general rule of
thumb that should be followed or can this simply be left up to the
developer?? I would think it not wise to have a pile of public
variables exposed and you would lose the ability to do consistency
cheecking within the get and set blocks etc...

If you can afford the time, I would say that...
....a class should not expose anything that it does not need to.
....a class should not allow any uncontrolled access to it's internal state.
....following on from above, ideally a property should be used everywhere
instead of public fields.

Note: A productivity tool like Coderush and RefactorPro (http://www.devexpress.com/Coderush)
may help to elliviate the typing associated with writing so many properties
rather than Public fields

Also CodeGeneration Tools, (CodeSmith http://www.codesmith.com, MyGeneration
http://www.mygenerationsoftware.com) can help here.
5) I think I am going to order that book (Expert VB 2005 Business
Objects) ! I downloaded the sample chapter 6 and it looks interesting
for sure. The only way to learn this stuff though is to program from
the ground up. Using a pre-built framework application would be handy
once I understand it all.. but for now I need to dive in with both
feet and read about it and learn by doing etc... Does this book deal
with grassroots stuff or does it mainly focus on the whole CSLA.net
thing becasue I really need a book to do grassroots OO stuff...

I have not read this book but I have heard if it and have heard the author
interviewed a few times on DotNetRocks (http://www.dotnetrocks.com - well
worth listening to). I understand that the book's purpose is to walk you
through the use of CSLA.net and show you how it works internally.
 
B

Brad Pears

Thanks for your help Rory...

So it sounds like maybe this book isn't really for me right now? Maybe Mr.
Arnold can comment on that better..?

I really need a good book for someone jsut beginning down the OO trail...

Thanks, Brad
 
R

Rory Becker

Thanks for your help Rory...

No problem whatsoever.

It's fun to be of help.

...and although there is rarely a "right way", it's still good that you're
looking for one.

Note: I've read ahead and I know I'm about to waffle. Just a little warning
:)

You will find many opinions and you will have to judge how valid/acurate
the information contained within then is for yourself.

My advice is "Ask why". Don't take anything at face value unless it is obvious
to you the reason behind something. Every good desicion has good reasons
behind it.

For example: My point before about using properties to mask the internal
data...
The basic reason for this is to avoid the hell caused by global variables.

Access to global variables is not restricted any one part of a program, so
when you find that the Global variable somehow contains bad data which is
infuencing your code, where do you look to find the source of this problem?

Properties give you a location to set a break point which will fire when
the property is read or written to.

A well designed class allows external access to only those parts of itself
that it needs to in order to function well according to it's purpose. The
class maintains control over itself and if something goes wrong it should
be easy to track the fault to it's source if it's responsibility is well
defined.

Another good suggestion in my opinion is that "A class, like a method should
try to do one thing and do it well".

If you're serious about OOP, then you should lookup "Inheritance", "Encupsulation"
and "Polymorphism". The 2 terms that I mentioned in my last post.

Once you understand these, you should look into interfaces.

When starting down the OOP route, you're likely to descide that inheritance
is the Bees Knees. this is good initially as it is true to a point. Many
people go a little nuts with inheritance.

Later on as you read about interfaces, more lights will go on. This OOP stuff
can be quite deep.

Patterns (Factory, Singleton, Adapter...) might follow this, and frankly,
the learning will never stop. I personally consider that a good thing.

Have fun. Keep asking questions. We're all learners here :)

One last piece of advice...
....Ignore everything that Rory bloke has to say.... He hasn't a clue what
he's talking about :p
 
B

Brad Pears

Rory, I really appreciate the fact you have taken the time to respond in
this way. Your advice seems quite rock solid to me. I definately like the
idea of "doing one thing - and doing it well" even if that means many more
classes may need to be used. I am all for the smaller easier component
system. It seems to make things much more robust and easier to code and
debug AND maintain down the road!! (imho)

I will do as you suggest. I do uinderstand some of the basics of
polymorphism, inheritance, encapsulation etc... so a lot about interfaces
just yet... This project I am working on will be an endless maintenacne job
I suspect as I learn new and better ways to do things!!!

You haven;t gotten rid of me yet! I still ahve lots of question to be asked
!!! :)

PS.. Ok, I'll ignore everything that Rory says!! :)

Thanks, Brad
 
M

Mr. Arnold

Brad Pears said:
Thanks for that... Here are a couple questions regarding your response...

1) Ok, I was assuming a brand new contact - one that has not yet been
populated from the db because it does not yet exist there - that's why I
was passing the contractno. I guess I don't really need to do though so
would it have been better to omit that and just set the ContractNo
property and then issue the update like this... (using no parameters)

' Set the busContract object properties
busContract.ContractNo = me.txtContractNo.text
' include code to set other properties the same way if they havent;
already been set elsewhere
' issue the update
busContract.update

I would assume that the ContractNo is a primary-key on a table with an auto
increment.

In addition to this, the Private variables the object's Public accessor
properties use will be set to some default state like 0 for ConatctNo,
NullString for ContractName, etc, etc when the object is instantiated.

Now, the ContractNO is going to be already set when data is returned.

If you're saving the busContract and it's new, in your Stored Procedure, you
could return the primary-key for the record added as an output parameter and
set the busContract.ContractNo to the retuned key. That way, the object has
its key, which you can then use from the busContract and set the paraent key
for child objects that are linked to the paraent being saved.

It's always busContract.Save, and in the Save method, you make the decision
as to what method in the DALObject to use of DALObject.Insert(object),
DALObject.Update(object) or DALObject.Delete(object).
2) Ok YES I UNDERSTAND!!! The light bulb went on!! Makes perfect sense,
pass the entire object and presto I can then use all the properties on
that business class object to do my updates!!
:)

3) OK, so that means that you do not need to duplicate the properties of
the business class on the data class at all. Really the associated data
class will have very few properties... Only ones specific to it...

You just set reference to the Bus.dll in the DAL Project, just like you
would set reference to the DAL.dll in the BUS Project.

4) In looking at other OO code, I have noticed where many programmers have
simply used public variables in their classes instead of declaring them as
private and exposing public properties which get and set the private
variables... I have been using properties and private variables... What
is your take on that??

Private variables and methods should be addressed by Public properties and
methods.
Is therte a general rule of thumb that should be followed or can this
simply be left up to the developer??

Those are the standards I have been taught and follow (up above there about
Private as opposed to Public).

I would think it not wise to have a pile of public variables exposed and
you would lose the ability to do consistency cheecking within the get and
set blocks etc...

You can look at it that way. I look at it being more of protecting the
object, particularly if the Object was being used in a WEB solution. But no
matter what environment the object is being used in, the object should be
protected with no direct access of anything Private.
5) I think I am going to order that book (Expert VB 2005 Business Objects)
! I downloaded the sample chapter 6 and it looks interesting for sure. The
only way to learn this stuff though is to program from the ground up.
Using a pre-built framework application would be handy once I understand
it all.. but for now I need to dive in with both feet and read about it
and learn by doing etc... Does this book deal with grassroots stuff or
does it mainly focus on the whole CSLA.net thing becasue I really need a
book to do grassroots OO stuff...

The CSLA book and the framework will give the grass roots of OO and show the
basic objects that you'll need to develop a OO solution.

The CSLA framework is a library of base objects that a NET developer derives
his or her objects from to develop a solution with some consistency and
other things.

But forget about that part of it, what you want to know is what are those
base objects and how are they being used.

Then when you put the Project Tracker project together to use the CSLA
Framework, you start looking at how is the code in the project using the
base objects in the framework.

You will then step back, and the light bulb will start going off. You'll
say to yourself, "what out of that framework can I just rip out of the
framework and what can I just rip out Tracker project to start making my
objects from scratch and use them.

I have stepped into some client sites, and when I could use the CSLA
concepts, they were being used. I just downloaded the CSLA Framework and
started pulling out the pieces I needed to make my own objects. It's not
that I was pulling things out per say, but it was I knew what to go get and
how it worked when I put the pieces together for the project. And I have
used every last object type that was being explained.

Some of the features in the base objects you may never use, and the only
way you can use them is through the CSLA Framework. But it may come a point
that you may be able to use one of those features in a project and you'll
know the concepts.

I remember I stepped into one client's site. It was a WEB solution that they
needed done. It was a project no one wanted to take it on, and the VP of IT
was asking could it be done.

It wound up in my lap. I was able to put a demo together for the client in
about 4 weeks using their company framework and the stuff I knew in making
those objects on the fly from CSLA, and what was needed.

The client was happy, I gave them the base of what was needed to be done and
turned it over to some other developers. The Director of .Net Technology
became my buddy while I was there and we talked turkey about OO concepts. He
showed me a few things, and I was able to show him a thing two.

That CSLA book will give you what you need, and you'll walk away think this
..NET stuff is fabulous from what I have seen out of that book, which I have
used the VB and C# CSLA concepts.
 
B

Brad Pears

The contract number is a primary key but not autogenerated. It has the year
and then a "-" and a seven digit incrementing number i.e. a contract for
this year would be "07-0000010"

I actually have a db table with one record in it that contains the next
contract number to be used among other system default values. I have a small
class
specifically set up to go get default values, increment the next number
etc... So on a new contract, I use this class to get the next contract
number and then I set it on the bus object...


Thanks for all your help. I will definately get the book then. I really
appreciate the time you have spent in replying to me to help me understand
and make better decisions. I am excited about learning this new stuff - I am
just trying not to get bogged down in it and want to keep moving forward -
and to try to do things somewhat right the first time and not get too far
off track. Not always going to happen but maybe I can get somewhat close...

Much appreciated! Rest assured there will be more questions!!! :)

Thanks, Brad
 
G

Guest

I actually have a db table with one record in it that contains the
next contract number to be used among other system default values. I
have a small class
specifically set up to go get default values, increment the next
number etc... So on a new contract, I use this class to get the next
contract number and then I set it on the bus object...

Here's a question - how do you ensure that you won't have any concurrency
issues.

Just make sure that the code you're using to update the contract record is
wrapped in some sort of transaction. Or ensure that the your call is using
the proper synchronization mechanism - i.e. Mutexes to ensure multiple
threads won't be incrementing the same value under laod.
 
B

Brad Pears

Yes, in the stored procedure that updates this single system 'row' I have it
inside a transaction which either commits or does a rollback. I then check
for the return value in my application and
display an error message if unsuccessful (among other things). In this
particular application, the
chances of multiple salespeople asking for the next contract number at the
exact same time hence causing a potential consistency issue are quite slim
(not that many contracts are really created) - but none the less possible.

Thanks,
Brad
 
M

Mr. Arnold

Brad Pears said:
Yes, in the stored procedure that updates this single system 'row' I have
it
inside a transaction which either commits or does a rollback. I then check
for the return value in my application and
display an error message if unsuccessful (among other things). In this
particular application, the
chances of multiple salespeople asking for the next contract number at the
exact same time hence causing a potential consistency issue are quite slim
(not that many contracts are really created) - but none the less possible.

Oracle works the same way in auto increment number to be used on a record as
a primary key. It's not a table with a field, but it's along the same
principles, where the next sequential number is gotten and applied to a
table's primary key.

I thought it was strange at first because of how MS SQL Server does it
normally with it's auto generate number for a table's record key, but it
works.
 
P

Pritcham

Hi

Don't know whether you're still looking for an OO intro type book but
I have heard very good reviews of a book called "Doing Objects in
Visual Basic 2005" - by Deborah Kurata - might be worth a look for you

Cheers
Martin
 

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

Top