P
Peter Richardson
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.LoadCustomerByID(100) where this becomes:
public Customer LoadCustomerByID(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<Customer>?
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.
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.LoadCustomerByID(100) where this becomes:
public Customer LoadCustomerByID(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<Customer>?
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.