Model

Object oriented programming

Introduction

Object-oriented programming (OOP) is a programming style using objects.
Objects are data structures consisting of data fields and methods together with their interactions.

A class is a construct that is used as a blueprint to create instances of itself – referred to as class instances, class objects, instance objects or simply objects. A class defines constituent members which enable these class instances to have state and behavior. Data field members (member variables or instance variables) enable a class object to maintain state. Other kinds of members, especially methods, enable a class object's behavior.

Class
  -- members 
     -- data field members
         -- properties
         -- other (internal) variables
     -- method members

Most known features are data abstraction, encapsulation, messaging, modularity, polymorphism and inheritance.

3-tier architecture

Introduction

Every program is divided into 3 layers (3-tier architecture):
- Presentation Layer / GUI (website or winforms) - shows the information on the screen
- Business Logic Layer (BLL) - contains all business related routines
- Data Access Layer (DAL) - contains all the access to the database

Theoretical point of view

From the presentation layer it should be possible to transparantly call the BLL and the DAL.
Example:
DAL
Customer from Linq {int id, string name, datetime birthdate}
Customer.Set(string name, string birthdate);
Customer.Insert(string name, datetime birthdate);
BLL
Customer.Age(); // Calculate the age of the customer
GUI
mCustomer = new Customer();
mCustomer.Set(txtName.Text, txtBirthdate.Text);
mCustomer.Insert();
txtAge.Text = mCustomer.Age();

Possible ways of working

Static

Linq:
  Public Partial Class Customer
DAL:
  Public Static Class CustomerDL
  {
    Public Static bool Set(ref Customer mCustomer, string name, string birthdate) {}
    Public Static bool Insert(ref Customer mCustomer) {}
  }
BLL:
  Public Static Class CustomerBL
  {
    Public Static int Age(Customer mCustomer) {}
  }
GUI:
  mCustomer = new Customer();
  mCustomer = CustomerDL.Set(ref mCustomer, txtName.Text, txtBirthdate.Text);
  CustomerDL.Insert(ref mCustomer);
  txtAge.Text = CustomerBL.Age(mCustomer).ToString();

Works perfect, but is far from what we want

Static + Partial

Linq:
  Public Partial Class Customer
DAL:
  Public Static Partial Class Customer
  {
    Public Static bool Set(ref Customer mCustomer, string name, string birthdate) {}
    Public Static bool Insert(ref Customer mCustomer) {}
  }
BLL:
  Public Static Class CustomerBL
  {
    Public Static int Age(Customer mCustomer) {}
  }
GUI:
  mCustomer = new Customer();
  mCustomer = Customer.Set(ref mCustomer, txtName.Text, txtBirthdate.Text);
  Customer.Insert(ref mCustomer);
  txtAge.Text = CustomerBL.Age(mCustomer).ToString();

Works perfect, but you still have to pass mCustomer as a parameter every time.
It also isn't transparant whether you call the DAL or the BLL.
Maybe also inherit the CustomerBL from DAL.Customer and name it Customer. In the GUI only refer to BLL.
So: Public Static Class Customer : DAL.Customer.

Partial

Linq:
  Public Partial Class Customer
DAL:
  Public Partial Class Customer
  {
    Public bool Set(string name, string birthdate) {}
    Public bool Insert() {this. ...}
  }
BLL:
  Public Static Class CustomerBL
  {
    Public Static int Age(Customer mCustomer) {}
  }
GUI:
  mCustomer = new Customer();
  mCustomer.Set(txtName.Text, txtBirthdate.Text);
  mCustomer.Insert();
  txtAge.Text = CustomerBL.Age(mCustomer).ToString();

Looks almost like what we want, but the problem is that in DAL Insert it is impossible to fill in the result of the insert in the class.
Maybe we should have a look at this again.
Same remark for inheritance of the BLL as above.

Links

http://planetmoss.blogspot.com/2007/08/linq-to-sql-n-tier-architecture.html
http://www.codeproject.com/KB/architecture/EnterpriseApplicationArch.aspx
http://weblogs.asp.net/scottgu/
http://channel9.msdn.com/forums/Coffeehouse/417482-LINQ-The-end-of-n-tier/
http://msdn.microsoft.com/en-us/library/bb882661.aspx
http://randolphcabral.wordpress.com/2008/05/21/exploring-n-tier-architecture-with-linq-to-sql-part-4-of-n/
http://social.msdn.microsoft.com/forums/en-US/linqprojectgeneral/thread/65199d51-5d19-4d60-9dae-c35d26684fba/
http://blogs.vertigo.com/personal/keithc/Blog/archive/2007/06/28/linq-to-sql-and-the-quote-request-scoped-datacontext-quote-pattern.aspx

Varia

Generically fill object from another object (DynamicFillerLib)

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License