Building Application Framework Objects


Application framework objects are memory structures that closely resemble parts of the database schema. They temporarily hold information that is passed between the database and the user interface. The two foundation entities are business objects and business object collections.

Working with Business Objects

Within the application framework, you implement business objects within a public class that inherits from the BusinessObject class. They have properties that match fields within a corresponding database table. Business objects are more functional than simple DataRow objects in that they may also provide object-specific functionality, such as data validation. Listing 2-2 presents the BusinessObject base class.

Listing 2-2: The Abstract Base Class for All Business Objects
start example
 public abstract class BusinessObject {     private DateTime _RowModified;     public BusinessObject()     {         return;     }     public bool Select()     {         return false;     }     public bool Insert()     {         return false;     }     public bool Update()     {         return false;     }     public bool Delete()     {         return false;     }     public bool Validate()     {         return false;     }     public DateTime RowModified     {         set         {              _RowModified = value;         }         get         {              return _RowModified;         }     } } 
end example
 

The BusinessObject class is abstract and cannot be instantiated itself. Application business objects, such as Issue and User, must inherit from this to participate within the application framework. The BusinessObject class defines the methods that must be implemented in the inherited class. Listing 2-3 outlines the implementation of the Issue business object.

Listing 2-3: The Issue Business Object Definition
start example
 public class Issue : BusinessObject {     private int _IssueID;     private int _TypeID;     private int _UserID;     private int _StatusID;     private int _PriorityID;     private string _Summary;     private string _Description;     private DateTime _EntryDate;     //property accessor implementation     public int IssueID     {         get { return _IssueID; }         set { _IssueID = value; }     }     public int TypeID     {         get { return _TypeID; }         set { _TypeID = value; }     }     public int UserID     {         get { return _UserID; }         set { _UserID = value; }     }     public DateTime EntryDate     {         get { return _EntryDate; }         set { _EntryDate = value; }     }     public int StatusID     {         get { return _StatusID; }         set { _StatusID = value; }     }     public String Summary     {         get { return _Summary; }         set { _Summary = value; }     }     public String Description     {         get { return _Description; }         set { _Description = value; }     }     public int Priority     {         get { return _PriorityID; }         set { _PriorityID = value; }     } } 
end example
 

The Issue business object begins by inheriting from the BusinessObject base class. The object properties are defined as they appear in the database table definition, and the resulting stored procedure should comply with a specific naming convention. For example, the object name Issue should match its relative data table name of Dat_Issue .

Most traditional classes have private data members and public access methods. Although public get and set methods are easy to create, the C# language preference is to define property accessors for each data element. This way, an application can set the value of a data member on the right side of an equals operator rather than as a parameter to a set method. Given the object definition for the Issue class, an application can quickly and easily set data members without the need for public access methods:

 Issue issue = new Issue(); issue.Summary = "Unable to print."; issue.Description = "Everytime I print, an error message appears."; issue.Priority = 1; 

When implementing the property accessors, it is important that the public accessor name matches its relative column name in the database table. For example, the property name of IssueID should match the column name of Dat_Issue.IssueID . The private data member name, however, can be just about anything. The User and Company entities require that additional business objects be created.

Working with Business Object Collections

Enumerators are another C# utility that help provide structure to application code. Rather than managing an array of Issue objects and then iterating through the list, the user interface logic can use the foreach statement to cycle through a list of Issue objects. First, define the Issue class to be Enumerable. Next , create an enumerator class that owns the responsibility of cycling through the Issue objects within an array. Implementing a custom collection class has advantages over using simple DataRow objects because you can create custom validation code and interobject mappings. Listing 2-4 shows the abstract base class for all framework business object collections, and Listing 2-5 shows how that class is inherited to implement the IssueCollection.

Listing 2-4: The Abstract Base Class for All Business Object Collections
start example
 public abstract class BusinessObjectCollection {     //manage business objects within an ArrayList with a default size of 25     ArrayList _Objects = new ArrayList(25);     //force derived classes to implement the New method     public abstract BusinessObject New();     //add a new business object to the collection     public void Add(BusinessObject argObject )     {         _Objects.Add( argObject );     }     //return the number of contained business objects     public int Count     {         get         {             return( _Objects.Count );         }     }     //internally validate if an array index is valid     private void CheckIndex(int argIndex) {         if( argIndex >= _Objects.Count )             throw new ArgumentOutOfRangeException( "Index out of range" );     }     //return a business object by array index reference     public BusinessObject this[int argIndex]     {         get         {              CheckIndex(argIndex);              return (BusinessObject)_Objects[argIndex];         }         set         {              CheckIndex(argIndex);              _Objects[argIndex] = value;         }     }     //return the associated enumerator object     public IEnumerator GetEnumerator()     {         return( new BusinessObjectCollectionEnumerator(this) );     } } class BusinessObjectCollectionEnumerator: IEnumerator {     BusinessObjectCollection _Collection;     int _Index;     //initialize the enumerator with the collection     internal BusinessObjectCollectionEnumerator(         BusinessObjectCollection argCollection)     {          _Collection = argCollection;          Reset();     }     //advance the current position to the next element     public bool MoveNext()     {         _Index++;         if( _Index >= _Collection.Count )            return( false );         else            return( true );     }     //return the current business object     public object Current     {         get         {              return( _Collection[ _Index ]);         }     }     //reset the enumerator     public void Reset()     {         _Index = -1;     } } 
end example
 
Listing 2-5: The IssueCollection Business Object Collection
start example
 public class IssueCollection : BusinessObjectCollection {     public IssueCollection()     {         return;     }     //create a new instance of the Issue object     public override BusinessObject New()     {         return new Issue();     } } 
end example
 

The BusinessObjectCollection class is an abstract base class from which framework business object collections should be derived. By inheriting business object collections, such as IssueCollection, from this class, they will behave consistently and benefit from the filling capability of the business object manager. Inherited objects will have the same base properties, such as Count, and the same base methods, such as New. Listing 2-6 demonstrates a variation of the IssueCollection that includes a method that can be applied to a group of Issue objects.

Listing 2-6: IssueCollection Implementation with an Optional Group Action Method
start example
 public class IssueCollection : BusinessObjectCollection {     public IssueCollection()     {         return;     }     public override BusinessObject New()     {         return new Issue();     }     public void MyCustomGroupAction()     {         return;     } } 
end example
 

The IssueCollection class must implement the abstract base method New. This method simply creates an instance of the object it collects and is largely needed by the business object manager. The MyCustomGroupAction method merely demonstrates the ability to implement a collection-level method, specific to this object type. This derived class will also benefit from an enumerator object that can return individual business objects in a foreach statement. Not only will inherited collections benefit from the batch loading of business objects provided by the business object manager, but they may also implement custom functionality that performs unique validation:

 IssueCollection myIssues = new IssueCollection(); DataAccess.ObjectManager objManager = new DataAccess.ObjectManager(); objManager.SelectAll( myIssues ); foreach( Issue myIssue in myIssues ) {     System.Diagnostics.Debug.WriteLine( myIssue.Summary ); } 



Developing. NET Enterprise Applications
Developing .NET Enterprise Applications
ISBN: 1590590465
EAN: 2147483647
Year: 2005
Pages: 119

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net