I am starting a new project and I always and looking for a better way
to skin a cat. Anyway, I am interested in seeing how different people
have implemented an N-Tier type architecture (using business objects)
for their applications...
The mains things I am interested in seeing is how you have setup each
tier to talk to each other, how the OR mapping stuff happens,
realtionships between objects (LazyLoad?), etc.
For simplicity please just show how using your design you would Insert,
Update, Delete, Get a CustomerObject and also how you would...
Example #1:
Customer Object:
public class Customer
{
public Customer() {}
private int m_id;
private string m_firstName;
private string m_lastName;
private string m_address;
[DataField("iCustomerID")]
public int ID
{
get{ return m_id; }
set{ m_id = value; }
}
[DataField("vchFirstName")]
public string FirstName
{
get { return m_firstName; }
set { m_firstName = value; }
}
[DataField("vchLastName")]
public string LastName
{
get { return m_lastName; }
set { m_lastName = value; }
}
[DataField("vchAddress")]
public string Address
{
get { return m_address; }
set { m_address = value; }
}
}
Manager Class:
usage:
CustomerManager manager = new CustomerManager();
//Get from DB
Customer customer = manager.GetByID(666);
//Update
customer.FirstName = "New Name";
manager.Update(customer);
//Insert
Customer newCustomer = new Customer();
newCustomer.FirstName = "Hello";
newCustomer.LastName = "Dolly";
newCustomer.Address = "1234 Somewhere Place";
implementation:
public class CustomerManager
{
public Customer GetByID(int customerID)
{
DALCustomer customer = new DALCustomer();
DataTable dtCustomer = customer.GetByID(customerID);
Customer customer = new Customer();
//The mapper class uses refelection to map the datatable columns to
//business object properties use the DataField attribute
DataMapper.Map(customer, dtCustomer);
return customer;
}
public void Update(Customer customer)
{
DALCustomer dalCustomer = new DALCustomer();
dalCustomer.Update(customer.ID, customer.FirstName,
customer.LastName, Customer.Address);
}
public void Insert(Customer customer)
{
DALCustomer dalCustomer = new DALCustomer();
int customerID = dalCustomer.Insert(customer.FirstName,
customer.LastName, Customer.Address)
customer.ID = customerID;
}
}
DataMapper:
public class DataMapper
{
public void Map(object entity, DataTable data)
{
//each property in the business object has an "Attribute" that
defines the OR Mapping
}
}
DataLayer:
- For each table there is a DataLayer object.
- Each object has the basic CRUD methods calling StoredProcs
implementation:
public class DALCustomer
{
public static DataTable GetByID(int customerID)
{
// Create Connection
// Create Command using Stored Procedure
// Return Dataset
}
public static int Insert(string firstName, sting lastName, string
address)
{
// Create Connection
// Create Command using Stored Procedure
// Add output paramter for new customerID
return customerID;
}
public static void Update(int customerID, string firstName, sting
lastName, string address)
{
// Create Connection
// Create Command using Stored Procedure
}
}