473,654 Members | 3,104 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

3-Tier Development - A little confused..

Developing a new app and am trying to make this my first truly
OOP/3-Tier app. I understand the principles of the presentation,
business, and data layers. I do, however, have some questions on where
certain functionality should be placed and how some things should be
implemented.

Let's use a simple example such as an application to manage customer
records (customer_id, first_name, last_name). I'd have a Customer
business object with ID, FirstName, and LastName properties. What I
don't understand is the following:

1. How do I obtain a Customer business object pre-loaded with data from
the database (customer_id 3 for example). I could create a constructor
in the Customer business class with the customer_id as a parameter and
have the constructor call the data layer to return a dataset containing
the customer's data and then set the properties accordingly. Is this
how this is normally accomplished? I could also have a method in the
Customer data layer that accepts a customer_id as a parameter and
returns a Customer business object. My only problem with this is that I
have it in my head that the presentation layer should never communicate
with the data layer. Some help here would be great.

2. How do I obtain a collection or ArrayList of Customer objects in the
case where I need to return more than one customer object? Such an
example would be a form that listed all customers. Calling a method in
the Customer data layer to return an ArrayList of Customer business
objects would work here as well, but this is breaking the same rule of
having the presentation layer working with the data layer. The other
option is to put a method in the Customer business object to return an
ArrayList of Customer business objects, but then my code has to
instantiate the Customer object simply to return more Customer objects.
This is unless I make the method in the Customer business object
shared.

3. Data layer methods to insert, update, delete database records -
Should the insert and update methods accept business objects as
parameters or should they accept a long parameter list that contains a
seperate parameter for each of the business object's properties?

Any help anyone can provide here is more than welcome. These issues
have been keeping me from developing an OOP/3-tier solution for quite
some time as I just can't seem to force myself to develop a system
without truly knowing how these issues should be tackled the proper
way.

Thanks in advance for your help!

Shawn Berg

Mar 6 '06 #1
11 1699
Hi Shawn,

Good questions! OOP is all about thinking first, and coding later.

I see a couple of holes in your understanding of these principles. The first
is understanding which layer knows what about which other layer. Whether
you're talking about a 3-tier or more-tier app, the principle is failry
simple: Each tier is a client of the tier below it. That means that no tier
knows anthing about the tier above it, and only knows about the tier
immediately below it. Example:

Presentation Tier
Knows about Business Tier (1 below)
Does NOT know about Data Tier (2 below)
Business Tier
Knows about Data Tier (1 below)
Does NOT know about Presentation Tier (above)
Data Tier
Does NOT know about Presentation Tier (above)
Does NOT know about Business Tier (above)

How does the Business tier communicate with the Presentation tier? It used
Properties which the Presentation Tier can read, and Events which the
Presentation Tier can subscribe to.

What this means with regards to Question number 1 ("How do I obtain a
Customer business object pre-loaded with data from the database"), the
answer should be obvious after understanding this principle. The Customer
class is a business class. It may know about the Data Layer. So, yes, you
could create a Contructor that takes a Customer ID and populates the
instance from the Data Tier. Or you could create a Customer ID property in
the Customer class that, in the setter method, fetches the data from the
Data Tier, and populates the instance with it. The second is the better
method. The first method can also be used, by overloading the Constructor,
and in the variation that takes a Customer ID, use the setter method of the
Customer ID property to populate the class. Very Simple Example:

public class Customer
{
private string _ConnectionStri ng = "Your Default Connection String";
public string ConnectionStrin g
{
get { return _ConnectionStri ng; }
set { _ConnectionStri ng = value; }
}

private string _FirstName;
public string FirstName
{
get { return _FirstName; }
set { _FirstName = value; }
}

private int _CustomerID;
public int CustomerID
{
get { return _CustomerID; }
set
{
// Set the private member
_CustomerID = value;

// (Imaginary Data Tier method)
// Note that the Data Tier only knows what it is told
// about the data it's working with.
// The business tier knows how to use the Data Tier.
DataTable dt = DataTier.GetDat aTable(
"TblCustome r", "CustomerID = " + _CustomerID,
_ConnectionStri ng);

// Populate the instance
_FirstName = (int)dt.Rows[0]["FirstName"];
}
}

public Customer() {}

public Customer(string connectionStrin g)
{
_ConnectionStri ng = value;
}

public Customer(string connectionStrin g, int customerID)
{
// Changes the Default Connection String
_ConnectionStri ng = connectionStrin g;

// Note the use of the setter method here
CustomerID = customerID;
}

public Customer(int customerID)
{
// Note the use of the setter method here.
// This overload uses the Default Connection String
CustomerID = customerID;
}

}

The second thing you seem to be missing is that a class and an object are
not the same thing. There is only one copy of a class. There can be many
copies of objects. An object is an instance (or copy) of a class.

So, when you ask

"How do I obtain a collection or ArrayList of Customer objects in the case
where I need to return more than one customer object?"

and you say

"The other option is to put a method in the Customer business object to
return an ArrayList of Customer business objects"

it seems that you're mixing up classes and objects. The solution is to
create a Collection class that can be used to store multiple Customer
instances. The best thing, rather than using an ArrayList, is to create a
strongly-typed Collection that always works with Customer instances.

Question 3 is answered in my opening remarks about what layer knows what
about the other layers.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer

Presuming that God is "only an idea" -
Ideas exist.
Therefore, God exists.

"iTISTIC" <sh***@itistic. com> wrote in message
news:11******** **************@ v46g2000cwv.goo glegroups.com.. .
Developing a new app and am trying to make this my first truly
OOP/3-Tier app. I understand the principles of the presentation,
business, and data layers. I do, however, have some questions on where
certain functionality should be placed and how some things should be
implemented.

Let's use a simple example such as an application to manage customer
records (customer_id, first_name, last_name). I'd have a Customer
business object with ID, FirstName, and LastName properties. What I
don't understand is the following:

1. How do I obtain a Customer business object pre-loaded with data from
the database (customer_id 3 for example). I could create a constructor
in the Customer business class with the customer_id as a parameter and
have the constructor call the data layer to return a dataset containing
the customer's data and then set the properties accordingly. Is this
how this is normally accomplished? I could also have a method in the
Customer data layer that accepts a customer_id as a parameter and
returns a Customer business object. My only problem with this is that I
have it in my head that the presentation layer should never communicate
with the data layer. Some help here would be great.

2. How do I obtain a collection or ArrayList of Customer objects in the
case where I need to return more than one customer object? Such an
example would be a form that listed all customers. Calling a method in
the Customer data layer to return an ArrayList of Customer business
objects would work here as well, but this is breaking the same rule of
having the presentation layer working with the data layer. The other
option is to put a method in the Customer business object to return an
ArrayList of Customer business objects, but then my code has to
instantiate the Customer object simply to return more Customer objects.
This is unless I make the method in the Customer business object
shared.

3. Data layer methods to insert, update, delete database records -
Should the insert and update methods accept business objects as
parameters or should they accept a long parameter list that contains a
seperate parameter for each of the business object's properties?

Any help anyone can provide here is more than welcome. These issues
have been keeping me from developing an OOP/3-tier solution for quite
some time as I just can't seem to force myself to develop a system
without truly knowing how these issues should be tackled the proper
way.

Thanks in advance for your help!

Shawn Berg

Mar 6 '06 #2
Kevin,

You've been very helpful thus far. I appreciate your time. Would you be
able to show me an example of a brief CustomerCollect ion class and how
it is implemented? This makes sense, but I'm not sure how to implement
it.

Another question that has been raised now is why the Business tier
deals with connection strings which are specific to the data tier?
Would it be OK to move this functionality to the data tier and have the
data tier set the connection string by default from a web.config
<connectionStri ngs> variable? I'm thinking I could have a base class
that all the other data classes inherit from entitled "DataBase" which
has this functionality built in, as well as the properties for changing
the connection string if necessary. Is this ok?

Thanks again,

Shawn

Mar 6 '06 #3
Think I've found a good article on this. Is this what you were
referring to for the CustomerCollect ion class?

And then in this class I could have methods such as "LoadByCity(ByV al
city As String)", etc. which would populate the collection?

Shawn

Mar 6 '06 #4
Guess it would help if I included the url ;)

http://builder.com.com/5100-6373-1050004.html

Mar 6 '06 #5
I don't know if it is or not, Shawn. It would seem that you omitted a link
to the article!

But from the sound of it, it seems right.

A lot depends on which version of the .Net Framework you're using. .Net 2.0
includes some nice support for Generics, which are classes that are
strongly-typed at compile-time. An example of this would be:

using System.Collecti ons.ObjectModel ;

// Collection<T> is a generic class which can be a Collection of any type.
// You pass the type to it to create a strongly-typed Collection.
// By inheriting the Strongly-typed Collection, you now have your own
// strongly-typed and extensible Collection.
// It already includes all the properties of the inherited Collection
// (which is what inheritance does), and you can add any other
// properties, methods, etc., that you want to it.
public class CustomerCollect ion : Collection<Cust omer>
{

public void LoadByCity(stri ng cityName)
{
DataTable dt = DataTier.GetDat aTable("Custome r",
"City = '" + cityName + "'");
foreach (DataRow row in dt.Rows)
{
Customer c = new Customer();
c.CityName = (string)(row[CityName]);
c.FirstName = (string) (row[FirstName]);
// etc.
Items.Add(c);
}
}

public CustomerCollect ion() : base()
{
}

public CustomerCollect ion(string cityName) : base()
{
LoadByCity(city Name);
}
}

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer

Presuming that God is "only an idea" -
Ideas exist.
Therefore, God exists.

"iTISTIC" <sh***@itistic. com> wrote in message
news:11******** **************@ u72g2000cwu.goo glegroups.com.. .
Think I've found a good article on this. Is this what you were
referring to for the CustomerCollect ion class?

And then in this class I could have methods such as "LoadByCity(ByV al
city As String)", etc. which would populate the collection?

Shawn

Mar 6 '06 #6
Yes, that's about right, if you're using .Net 1.1.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer

Presuming that God is "only an idea" -
Ideas exist.
Therefore, God exists.

"iTISTIC" <sh***@itistic. com> wrote in message
news:11******** *************@z 34g2000cwc.goog legroups.com...
Guess it would help if I included the url ;)

http://builder.com.com/5100-6373-1050004.html

Mar 6 '06 #7
I'm using .NET 2.0 .. What would be different?

Mar 6 '06 #8
See my example in a previous reply. I showed you how to create a Collection
derived from the Generic Collection class. In case you have trouble finding
it, here it is again:

using System.Collecti ons.ObjectModel ;

// Collection<T> is a generic class which can be a Collection of any type.
// You pass the type to it to create a strongly-typed Collection.
// By inheriting the Strongly-typed Collection, you now have your own
// strongly-typed and extensible Collection.
// It already includes all the properties of the inherited Collection
// (which is what inheritance does), and you can add any other
// properties, methods, etc., that you want to it.
public class CustomerCollect ion : Collection<Cust omer>
{

public void LoadByCity(stri ng cityName)
{
DataTable dt = DataTier.GetDat aTable("Custome r",
"City = '" + cityName + "'");
foreach (DataRow row in dt.Rows)
{
Customer c = new Customer();
c.CityName = (string)(row[CityName]);
c.FirstName = (string) (row[FirstName]);
// etc.
Items.Add(c);
}
}

public CustomerCollect ion() : base()
{
}

public CustomerCollect ion(string cityName) : base()
{
LoadByCity(city Name);
}
}

The differences are numerous:

1. Inheriting CollectionBase, which works with the base Object data type,
requires casting, which is somewhat costly in terms of performance. Not a
lot, but a little.

2. When you inherit from a Generic Collection that has been assigned a type,
there's usually no need to override anything, as everything is already
strongly-typed to the type you have set the Generic Collection to. All you
need to do is add any implementation-specific functionality you want to add
and you're done. This means that you have less code to write.

3. In some cases, you don't even need to inherit
System.Collecti ons.ObjectModel .Collection<T>. When you add one to your code
and assign a type to it, it is compiled as that type. You only need to
inherit it if you want to add any implementation-specific functionality
and/or properties. For example, if you didn't need anything but standard
Collection behavior for your Customer Collection, you could simply use:

private Collection<Cust omer> Customers = new Collection<Cust omer>();

Generics arae very powerful tools, perhaps the single most useful addition
to the .Net 2.0 platform.

"iTISTIC" <sh***@itistic. com> wrote in message
news:11******** **************@ j33g2000cwa.goo glegroups.com.. .
I'm using .NET 2.0 .. What would be different?

Mar 6 '06 #9
Makes a lot of sense, and definitely less code. Going to have to see
how I can implement that in VB.NET versus C#. Shouldn't be too
different I wouldn't assume.

Thanks for all your help Kevin.

Shawn

Mar 6 '06 #10

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

Similar topics

4
1203
by: Amy Snyder | last post by:
I am trying to discern the difference between creating a web service or using IIS Virtual Directory Management for SQL Server Utitly if I want to provide xml data from a database. I have created some xml templates that will allow a user to access the data in xml format when entering the http:// address. Its pretty quick and easy to do once the virtual directory is configured through sql. Is creating a web service to provide xml data...
6
1544
by: sparks | last post by:
extracalc = Switch(Me.Parent.Race_Black = -1 And Me.Parent.Sex = "Female", 1.952, Me.Parent.Race_Black = -1, 1.21, Me.Parent.Sex = "Female", 0.742, 1) I look at this and say ok if race = black and sex = female then extracalc = 1.952 if race = black then extracalc = 1.21 if sex = female then extracalc = .742 else
6
1694
by: manochavishal | last post by:
Hi , Ihave this code to show binary of an integer. #include<stdio.h> #include<stdlib.h> typedef struct binary* binaryptr; typedef struct binary { int data;
5
1636
by: Bit byte | last post by:
I have the following methods: static void Foo::setBar(const Bar*) ; //store a copy of Bar static const Bar* Foo::getBar(void) const ; //return an UNMODIFIABLE ptr to our internal copy In another part of my code , I retrieved and used Bar as follows: .... const Bar* temp = NULL ;
6
1373
by: porky008 | last post by:
We are still going over pseudo code and we are working on arrays right now. I am having some difficulty with the bubble sort algorithm. Can some one give me a better example than this one? I do understand that each pass the larger values are to drop to the bottom but the algorithm is still throwing me off. While the array A is not sorted For K = 1 Step 1 To N - 1 If A A Then Interchange A and A End If
0
8294
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
8816
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...
0
8709
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8596
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6162
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
5627
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4297
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1924
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1597
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.