473,569 Members | 2,593 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Object design question

Hi,

I'm wondering if someone can help me with some design questions I have. I'm
trying to create a class in C# to represent my customers. I know how to
create teh Customer class and all, but my problem comes with some conceptual
issues I have:

Let's say I have a business layer and a data layer. I use a seperate
assembly to host the business objects that I pass from one to another. That
is, I have for example:

public class Customer {

private string Name;
private string Surname;
.... public properties
}

Now, here are my questions:

1. Should this class call methods that in turn call the data layer to load
the values up by passing itself as reference? That is, should I have a
method for example Load that does something like:

DAL.Load(Self);

and DAL (Data access layer) accepts as parameter a Customer class that it
loads up?

Or should I in turn have another "wrapper", for example a CustomerLogic
class that creates a customer object, creates a DAL object and passes one
from the other and in turn my UI for example calls methods on this new
class? As in:

CustomerLogic.L oadCustomerByID (100) where this becomes:

public Customer LoadCustomerByI D(int ID) {

DAL dal = new CustomerDAL();
Customer customer = new Customer();

customer = dal.Load(ID);
return customer;
}

Which is better? From one point of view, I think the first solution is
better since it's encapsulating the customer object and doing all the work
from there. However, with this solution I face the following dilema:

What happens if I want to load for example 20 customers? Should I make a
static method of Customer class that returns a Collection<Cust omer>?

With the second approach I could just create a method of CustomerLogic that
does that.

2. My second question is related to the first. Let's say now that a Customer
has several email addresses. I then make a property that points to another
class. However, it would be ideal to load this property in lazy mode, i.e.
when first read, that way consuming little memory. So that's not a problem
with the first approach since again, Customer is all encapsulated and I read
the emails from within the Customer class.

However, with the second approach, I'm now left ot creating a new method in
CustomerLogic that then loads the email addresses.

Alternatively I could read it all in the Load method.

Lastly, I assume that reading related objects should be done at the DAL
layer and not at the BL correct? Because if not, when I change my backend, I
would probably need to change my BL also, unless of course I'm using
interfaces to define behaviour. In other words, if I have two tables that I
want to map to an object, do I do this mapping in the DAL or do I read the
tables invididually by making seperate calls to the DAL from the BL?

Sorry for the long post, but it's really causing me roadblocks.
Nov 22 '06 #1
6 1422
Hi Peter,

Here's how I'd go about it, and this is by no means the "correct" or
"only" answer; but I'd go with model, view, controller. So, a user
would press a button on the form, say it was labelled "Load all
Customers". That form would have a controller and that controller would
would trap that event and call LoadCustomers() on the
ApplicationCont roller. The ApplicationCont roller would maintain a
collection of Customers. If the collection of customers was empty (or
some time out had expired) the ApplicationCont roller would call the
data layer to fetch the customers, something like
DAL.LoadAllCust omers(); The ApplicationCont roller would then populate
its collection of Customer objects and return it to the FormController
which would populate a grid for example.

Thereafter, if a particular Customer was required, say because of a
double click in the grid, then that event would be trapped by the
FormController and the ApplicationCont roller would be asked for the
Customer, like so, ApplicationCont roller.GetCusto merById(100). The
ApplicationCont roller would then search the collection of customers for
the specific customer with id = 100 and return it to the
FormController, which would, in turn, update the form.

Hope this helps.
--
Cheers,
Gary
http://www.garyshort.org/
Peter Richardson wrote:
Hi,

I'm wondering if someone can help me with some design questions I have. I'm
trying to create a class in C# to represent my customers. I know how to
create teh Customer class and all, but my problem comes with some conceptual
issues I have:

Let's say I have a business layer and a data layer. I use a seperate
assembly to host the business objects that I pass from one to another. That
is, I have for example:

public class Customer {

private string Name;
private string Surname;
.... public properties
}

Now, here are my questions:

1. Should this class call methods that in turn call the data layer to load
the values up by passing itself as reference? That is, should I have a
method for example Load that does something like:

DAL.Load(Self);

and DAL (Data access layer) accepts as parameter a Customer class that it
loads up?

Or should I in turn have another "wrapper", for example a CustomerLogic
class that creates a customer object, creates a DAL object and passes one
from the other and in turn my UI for example calls methods on this new
class? As in:

CustomerLogic.L oadCustomerByID (100) where this becomes:

public Customer LoadCustomerByI D(int ID) {

DAL dal = new CustomerDAL();
Customer customer = new Customer();

customer = dal.Load(ID);
return customer;
}

Which is better? From one point of view, I think the first solution is
better since it's encapsulating the customer object and doing all the work
from there. However, with this solution I face the following dilema:

What happens if I want to load for example 20 customers? Should I make a
static method of Customer class that returns a Collection<Cust omer>?

With the second approach I could just create a method of CustomerLogic that
does that.

2. My second question is related to the first. Let's say now that a Customer
has several email addresses. I then make a property that points to another
class. However, it would be ideal to load this property in lazy mode, i.e.
when first read, that way consuming little memory. So that's not a problem
with the first approach since again, Customer is all encapsulated and I read
the emails from within the Customer class.

However, with the second approach, I'm now left ot creating a new method in
CustomerLogic that then loads the email addresses.

Alternatively I could read it all in the Load method.

Lastly, I assume that reading related objects should be done at the DAL
layer and not at the BL correct? Because if not, when I change my backend, I
would probably need to change my BL also, unless of course I'm using
interfaces to define behaviour. In other words, if I have two tables that I
want to map to an object, do I do this mapping in the DAL or do I read the
tables invididually by making seperate calls to the DAL from the BL?

Sorry for the long post, but it's really causing me roadblocks.
Nov 22 '06 #2
Hi Gary,

Thanks for the reply. So what you're basically saying is use the second
approach where you have another object that handles the customers? And what
about for example the email addresses, this ApplicationCont roller would take
care of loading them?

Wouldn't that also make the ApplicationCont roller a master of all trades?
Having to deal with all types of entities?

Btw, what's the difference between MVC that you mention and MVP?
<ga**@garyshort .orgwrote in message
news:11******** ************@m7 3g2000cwd.googl egroups.com...
Hi Peter,

Here's how I'd go about it, and this is by no means the "correct" or
"only" answer; but I'd go with model, view, controller. So, a user
would press a button on the form, say it was labelled "Load all
Customers". That form would have a controller and that controller would
would trap that event and call LoadCustomers() on the
ApplicationCont roller. The ApplicationCont roller would maintain a
collection of Customers. If the collection of customers was empty (or
some time out had expired) the ApplicationCont roller would call the
data layer to fetch the customers, something like
DAL.LoadAllCust omers(); The ApplicationCont roller would then populate
its collection of Customer objects and return it to the FormController
which would populate a grid for example.

Thereafter, if a particular Customer was required, say because of a
double click in the grid, then that event would be trapped by the
FormController and the ApplicationCont roller would be asked for the
Customer, like so, ApplicationCont roller.GetCusto merById(100). The
ApplicationCont roller would then search the collection of customers for
the specific customer with id = 100 and return it to the
FormController, which would, in turn, update the form.

Hope this helps.
--
Cheers,
Gary
http://www.garyshort.org/
Peter Richardson wrote:
>Hi,

I'm wondering if someone can help me with some design questions I have.
I'm
trying to create a class in C# to represent my customers. I know how to
create teh Customer class and all, but my problem comes with some
conceptual
issues I have:

Let's say I have a business layer and a data layer. I use a seperate
assembly to host the business objects that I pass from one to another.
That
is, I have for example:

public class Customer {

private string Name;
private string Surname;
.... public properties
}

Now, here are my questions:

1. Should this class call methods that in turn call the data layer to
load
the values up by passing itself as reference? That is, should I have a
method for example Load that does something like:

DAL.Load(Self);

and DAL (Data access layer) accepts as parameter a Customer class that it
loads up?

Or should I in turn have another "wrapper", for example a CustomerLogic
class that creates a customer object, creates a DAL object and passes one
from the other and in turn my UI for example calls methods on this new
class? As in:

CustomerLogic. LoadCustomerByI D(100) where this becomes:

public Customer LoadCustomerByI D(int ID) {

DAL dal = new CustomerDAL();
Customer customer = new Customer();

customer = dal.Load(ID);
return customer;
}

Which is better? From one point of view, I think the first solution is
better since it's encapsulating the customer object and doing all the
work
from there. However, with this solution I face the following dilema:

What happens if I want to load for example 20 customers? Should I make a
static method of Customer class that returns a Collection<Cust omer>?

With the second approach I could just create a method of CustomerLogic
that
does that.

2. My second question is related to the first. Let's say now that a
Customer
has several email addresses. I then make a property that points to
another
class. However, it would be ideal to load this property in lazy mode,
i.e.
when first read, that way consuming little memory. So that's not a
problem
with the first approach since again, Customer is all encapsulated and I
read
the emails from within the Customer class.

However, with the second approach, I'm now left ot creating a new method
in
CustomerLogi c that then loads the email addresses.

Alternativel y I could read it all in the Load method.

Lastly, I assume that reading related objects should be done at the DAL
layer and not at the BL correct? Because if not, when I change my
backend, I
would probably need to change my BL also, unless of course I'm using
interfaces to define behaviour. In other words, if I have two tables that
I
want to map to an object, do I do this mapping in the DAL or do I read
the
tables invididually by making seperate calls to the DAL from the BL?

Sorry for the long post, but it's really causing me roadblocks.

Nov 22 '06 #3
Hello Peter,
Thanks for the reply. So what you're basically saying is use the second
approach where you have another object that handles the customers? And what
about for example the email addresses, this ApplicationCont roller would take
care of loading them?
No, the ApplicationCont roller would load the Customers only. Let's say
the Customer also has related Orders. I would load them in lazily. So
let's imagine that you had selected a specific Customer from a list and
had opened a view of that customer and then you had clicked to fill a
list with this Customer's Orders. The CustomerFormCon troller would trap
the click event and would ask the Customer object for it's list of
Orders. As this is beging done lazily, the Customer object knows how to
get a list of it's Orders from the data layer, something like
DAL.GetOrdersFo rCustomerId(12) . The Customer then returns that list to
the CustomerFormCon troller who updates the view.

As an aside, email addresses are not a good candidate for objects as
they have no behaviour of their own, they only have knowledge.
Wouldn't that also make the ApplicationCont roller a master of all trades?
Having to deal with all types of entities?
Um sort of, the ApplicationCont roller just responds to events that are
raised at the application level, and acts as a repository for
application wide objects, all Customers, for example.
Btw, what's the difference between MVC that you mention and MVP?
Practically none at all. :)

--
Cheers,
Gary
http://www.garyshort.org/

Nov 22 '06 #4
had opened a view of that customer and then you had clicked to fill a
list with this Customer's Orders. The CustomerFormCon troller would trap
the click event and would ask the Customer object for it's list of
Abstracting the UI for a minute, Controller can be the CustomerLogic I was
referring to previously right? A wrapper class that handles my business
entities?
In the case of using it in a WinForms app for example (or ASP.Net or
whatever), this would be named "Controller "...

get a list of it's Orders from the data layer, something like
DAL.GetOrdersFo rCustomerId(12) . The Customer then returns that list to
the CustomerFormCon troller who updates the view.
This answers I think my other question. The Customer object is actually
making explicit calls (in for example the set acceser for the property) to
load the information from the DAL.
However, this brings out another issue. My Customer object has to be aware
of how the information is persisted. If tomorrow I change the database and
instead of storying the information in two independent tables, I use one
(for the sake of argument), then I would not only need to modify my DAL, but
also make BL (where customer object resides). If not, I would be making
unnecessary calls to the DAL.

And there is my dilema. The idea situation would be for the customer to get
loaded in one call in the DAL. However, this prevents lazy loading.
As an aside, email addresses are not a good candidate for objects as
they have no behaviour of their own, they only have knowledge.
Yes, sorry, just wanted to "simplify" it.

Nov 22 '06 #5
Peter Richardson wrote:
Abstracting the UI for a minute, Controller can be the CustomerLogic I was
referring to previously right? A wrapper class that handles my business
entities?
In the case of using it in a WinForms app for example (or ASP.Net or
whatever), this would be named "Controller "...
Think of the Controller as being the "glue" between your object model
and the view of a particular model. Google MVC for more information.
This answers I think my other question. The Customer object is actually
making explicit calls (in for example the set acceser for the property) to
load the information from the DAL.
However, this brings out another issue. My Customer object has to be aware
of how the information is persisted. If tomorrow I change the database and
instead of storying the information in two independent tables, I use one
(for the sake of argument), then I would not only need to modify my DAL, but
also make BL (where customer object resides). If not, I would be making
unnecessary calls to the DAL.
No the Customer object has no knowledge of the persistence mechanism
other that it knows there is a DAL. For example, let's say today your
persistence mechanism is SQL Server. If a Customer wanted to lazily
load it's related Orders the code would be something like the
following:-

if(null == ListOfOrders)
{
DAL.GetOrdersFo rCustomersById( this.id);
}
return ListOfOrders;

Now, imagine that tomorrow you changed your persistence mechanism to be
MySQL then the above code would remain the same, any changes would be
in the data layer.

--
Cheers,
Gary
http://www.garyshort.org/

Nov 22 '06 #6
You can find a complete example at:

http://sholliday.spaces.live.com/blog/

5/24/2006
Custom Objects/Collections and Tiered Development

The businessobject shouldn't have any knowledge of what a database is. the
business object is a container for properties/methods and sometimes events.

this is the worst thing you can do

public class Employee
{

public Employee (int empid )
{
//in the constructor, talk to a specific database to load up values
}

}

But I see it done all the time in online examples and among other
developers.

...

Also, at the end of my blog, there is a MS article url ("read all of this
top to bottom") ...

You should read that, its a bird's eye view.

You should search the MS article for "timestamp" , and you'll find some ways
to handle the object instance vs the data in the database issue.


"Peter Richardson" <no****@nospam. comwrote in message
news:O6******** ******@TK2MSFTN GP06.phx.gbl...
Hi,

I'm wondering if someone can help me with some design questions I have.
I'm
trying to create a class in C# to represent my customers. I know how to
create teh Customer class and all, but my problem comes with some
conceptual
issues I have:

Let's say I have a business layer and a data layer. I use a seperate
assembly to host the business objects that I pass from one to another.
That
is, I have for example:

public class Customer {

private string Name;
private string Surname;
.... public properties
}

Now, here are my questions:

1. Should this class call methods that in turn call the data layer to load
the values up by passing itself as reference? That is, should I have a
method for example Load that does something like:

DAL.Load(Self);

and DAL (Data access layer) accepts as parameter a Customer class that it
loads up?

Or should I in turn have another "wrapper", for example a CustomerLogic
class that creates a customer object, creates a DAL object and passes one
from the other and in turn my UI for example calls methods on this new
class? As in:

CustomerLogic.L oadCustomerByID (100) where this becomes:

public Customer LoadCustomerByI D(int ID) {

DAL dal = new CustomerDAL();
Customer customer = new Customer();

customer = dal.Load(ID);
return customer;
}

Which is better? From one point of view, I think the first solution is
better since it's encapsulating the customer object and doing all the work
from there. However, with this solution I face the following dilema:

What happens if I want to load for example 20 customers? Should I make a
static method of Customer class that returns a Collection<Cust omer>?

With the second approach I could just create a method of CustomerLogic
that
does that.

2. My second question is related to the first. Let's say now that a
Customer
has several email addresses. I then make a property that points to another
class. However, it would be ideal to load this property in lazy mode, i.e.
when first read, that way consuming little memory. So that's not a problem
with the first approach since again, Customer is all encapsulated and I
read
the emails from within the Customer class.

However, with the second approach, I'm now left ot creating a new method
in
CustomerLogic that then loads the email addresses.

Alternatively I could read it all in the Load method.

Lastly, I assume that reading related objects should be done at the DAL
layer and not at the BL correct? Because if not, when I change my backend,
I
would probably need to change my BL also, unless of course I'm using
interfaces to define behaviour. In other words, if I have two tables that
I
want to map to an object, do I do this mapping in the DAL or do I read the
tables invididually by making seperate calls to the DAL from the BL?

Sorry for the long post, but it's really causing me roadblocks.


Nov 22 '06 #7

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

Similar topics

2
3457
by: ggg | last post by:
I'm looking for a complete project/application done with heavy use of of object-oriented programming & design. Preferably something well documented and/or commented so that I can pick it apart and learn how/why they designed it they way they did. Any suggestions?
3
1480
by: andy2O | last post by:
Hello comp.lang.py, Can you help me with ideas for the following (somewhat newbie) OO design question in Python? Note, I'm using psuedo-code, not actual Python for the examples! Background: ----------- I need to represent a small variety of mathematical constructs symbolically using Python classes.
6
4103
by: blueblueblue2005 | last post by:
here is a friend function of Class Array, which has two private data member: int size, int *ptr // Array's public member function to return size int getSize() const { return size; } friend istream &operator>>(istream &in, Array &a) { for(int i=0; i<a.size; i++) // do something
22
2725
by: ypjofficial | last post by:
Is there any possibility of invoking the member functions of a class without creating an object (or even a pointer to ) of that class. eg. #include <iostream.h> class test { public: void fun() {
4
1789
by: Carl J. Van Arsdall | last post by:
It seems the more I come to learn about Python as a langauge and the way its used I've come across several discussions where people discuss how to do things using an OO model and then how to design software in a more "Pythonic" way. My question is, should we as python developers be trying to write code that follows more of a python standard...
17
2580
by: Divick | last post by:
Hi, I am designing an API and the problem that I have is more of a design issue. In my API say I have a class A and B, as shown below class A{ public: void doSomethingWithB( B * b) { //do something with b //possibly store in a list
11
1346
by: John A Grandy | last post by:
I'm in a vigorous debate at my work regarding objects assuming knowledge of the type their containing object. This debate pertains specifically to ASP.NET, but I have decided to post in the C# forum because this is where most of the OO gurus hang out, and I view this as a fundamental issue of OO design. In ASP.NET, objects of type WebForm...
0
1139
by: =?Utf-8?B?SmVhbi1GcmFuY29pcyBCcmV0b24=?= | last post by:
"siddharthkhare@hotmail.com" wrote: The context is important in this kind of design concern : I assume there's a lot of user and that application will evolve to add richer functionality. My first concern would be to separate the business logic code from data contener : the new code would look like that :...
3
157
by: H. S. Lahman | last post by:
Responding to siddharthkhare... Ignore Topmind and frebe. They are anti-OO P/R guys. Let's not confuse things with specific 3GL syntax. At the OOA/D level the model looks like: | 1
7
1688
by: joproulx | last post by:
Hi, I was wondering if there was a way with Reflection to find dynamically if an object was referencing indirectly another object. A simple example would be: Object1 | --Object2 |
0
8125
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
7676
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
7974
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
6284
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 project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5513
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
5219
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
3653
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...
1
2114
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
0
938
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.