View RSS Feed

jmurrayhead

Building Applications with a Business Object Architecture

Rating: 2 votes, 3.00 average.
by on November 19th, 2008 at 09:44 PM (1675 Views)
Business Objects are objects in an object-oriented computer application or program that represent the entities within a business domain. A business object will often encapsulate all of the data and business logic associated with the entity in which it represents. Furthermore, business objects provide flexibility and adaptability, scalability and reusability.

Let's take a look at how a business object may look within an ASP.NET web application. Let's say you want to design an application that serves as a contact book. The contact book will have the following entities:
  • Contact
  • Address
  • PhoneNumber
The Contact class might look like this:
Code:
Public Class Contact
#Region "Private Variables"
        Private _ID As Integer = 0
        Private _firstName As String = String.Empty
        Private _middleName As String = String.Empty
        Private _lastName As String = String.Empty
        Private _suffix As String = String.Empty
#End Region

#Region "Properties"
        Public Property ID() As Integer
            Get
                Return _ID
            End Get
            Set(ByVal value As Integer)
                _ID = value
            End Set
        End Property

        Public Property FirstName() As String
            Get
                Return _firstName
            End Get
            Set(ByVal value As String)
                _firstName = value
            End Set
        End Property

        Public Property MiddleName() As String
            Get
                Return _middleName
            End Get
            Set(ByVal value As String)
                _middleName = value
            End Set
        End Property

        Public Property LastName() As String
            Get
                Return _lastName
            End Get
            Set(ByVal value As String)
                _lastName = value
            End Set
        End Property

        Public Property Suffix() As String
            Get
                Return _suffix
            End Get
            Set(ByVal value As String)
                _suffix = value
            End Set
        End Property
#End Region

#Region "Constructors"
        Public Sub New()

        End Sub

        Public Sub New(ByVal id As Integer, lastName As String, ByVal firstName As String)
            Me.ID = id
            Me.LastName = lastName
            Me.FirstName = firstName
        End Sub

        Public Sub New(ByVal id As Integer, lastName As String, ByVal firstName As String, _
        ByVal middleName As String, ByVal suffix As string)
            Me.ID = id
            Me.LastName = lastName
            Me.FirstName = firstName
            Me.MiddleName = middleName
            Me.Suffix = suffix
        End Sub
#End Region
End Class
The Address class might look like this:
Code:
Public Class Address
#Region "Private Variables"
        Private _ID As Integer = 0
        Private _contactID As Integer = 0
        Private _street1 As String = String.Empty
        Private _street2 As String = String.Empty
        Private _city As String = String.Empty
        Private _Country As String = String.Empty
#End Region

#Region "Properties"
        Public Property ID() As Integer
            Get
                Return _ID
            End Get
            Set(ByVal value As Integer)
                _ID = value
            End Set
        End Property

        Public Property ContactID() As Integer
            Get
                Return _contactID
            End Get
            Set(ByVal value As Integer)
                _contactID = value
            End Set
        End Property

        Public Property Street1() As String
            Get
                Return _street1
            End Get
            Set(ByVal value As String)
                _street1 = value
            End Set
        End Property

        Public Property Street2() As String
            Get
                Return _street2
            End Get
            Set(ByVal value As String)
                _street2 = value
            End Set
        End Property

        Public Property City() As String
            Get
                Return _city
            End Get
            Set(ByVal value As String)
                _city = value
            End Set
        End Property

        Public Property Country() As String
            Get
                Return _country
            End Get
            Set(ByVal value As String)
                _country = value
            End Set
        End Property
#End Region

#Region "Constructors"
        Public Sub New()

        End Sub

        Public Sub New(ByVal id As Integer, city As String, ByVal country As String)
            Me.ID = id
            Me.City = city
            Me.Country = country
        End Sub

        Public Sub New(ByVal id As Integer, street1 As String, ByVal street2 As String, _
        ByVal city As String, ByVal country As string)
            Me.ID = id
            Me.Street1 = street1
            Me.Street2 = street2
            Me.City = city
            Me.Country = country
        End Sub
#End Region
End Class
and the PhoneNumber class might look like this:

Code:
Public Class PhoneNumber
#Region "Private Variables"
        Private _ID As Integer = 0
        Private _contactID As Integer = 0
        Private _phoneNumber As String = String.Empty
        Private _phoneType As String = String.Empty
#End Region

#Region "Properties"
        Public Property ID() As Integer
            Get
                Return _ID
            End Get
            Set(ByVal value As Integer)
                _ID = value
            End Set
        End Property

        Public Property ContactID() As Integer
            Get
                Return _contactID
            End Get
            Set(ByVal value As Integer)
                _contactID = value
            End Set
        End Property

        Public Property PhoneNumber() As String
            Get
                Return _phoneNumber
            End Get
            Set(ByVal value As String)
                _phoneNUmber = value
            End Set
        End Property

        Public Property PhoneType() As String
            Get
                Return _phoneType
            End Get
            Set(ByVal value As String)
                _phoneType = value
            End Set
        End Property
#End Region

#Region "Constructors"
        Public Sub New()

        End Sub

        Public Sub New(ByVal id As Integer, phoneNUmber As String, ByVal phoneType As String)
            Me.ID = id
            Me.PhoneNumber = phoneNumber
            Me.PhoneType = phoneType
        End Sub
#End Region
End Class
I've divided each of these classes into three regions: Private Variables, Properties, and Constructors. Properties are what you use to set and get values for your objects. The Private Variables are used to hold the data for these properties. The Constructors allow you to quickly and easily assign values to your object's properties. For example, look at the second constructor in the Contact class:

Code:
Public Sub New(ByVal id As Integer, lastName As String, ByVal firstName As String)
    Me.ID = id
    Me.LastName = lastName
    Me.FirstName = firstName
End Sub
This allows you to populate the Contact object with the ID, LastName and FirstName in one easy piece of code:

Code:
Dim myContact As New Contact(dtr("ID"), dtr("LastName"), dtr("FirstName"))
As opposed to this way, without constructors:

Code:
Dim myContact As New Contact
With myContact
    .ID = dtr("ID")
    .LastName = dtr("LastName")
    .FirstName = dtr("FirstName")
End With
You would then be able to use your business object like this:
Code:
txtID.Text = myContact.ID
txtLastName.Text = myContact.LastName
txtFirstName.Text = myContact.FirstName
You can quickly see power behind using business objects in your applications. You can do much more with business objects, but that is out of the scope of this entry. If you've noticed my other entries and posts within this forum, you will know that I am a huge supporter of the layered approach to designing applications, specifically the one used by the BeerHouse CMS (TheBeerHouse: CMS & e-commerce StarterKit - Home). As time allows, I will break down this project in easier to understand sections, including how business objects are used and what else you can do with them to make managing your applications easier.

Submit "Building Applications with a Business Object Architecture" to Digg Submit "Building Applications with a Business Object Architecture" to del.icio.us Submit "Building Applications with a Business Object Architecture" to StumbleUpon Submit "Building Applications with a Business Object Architecture" to Google

Updated May 9th, 2011 at 11:32 AM by jmurrayhead

Categories
Programming & Scripting , ASP.NET

Comments


SEO by vBSEO