[Reference] Business Objects for codesmith

Source: Internet
Author: User
Tags coding standards
Document directory
  • Business Object:
  • Business Object collection:
  • Business Object Data:
  • Base Object classes:
  • Base Object Data class
  • Data Layer generator:

Http://www.codeproject.com/cs/design/busnessobjectgenerator.asp

  • Download source files-77.9 KB

Introduction

One of the biggest issues a developer faces when developing a large application, is maintaining design standards and methods. design Documents, coding standards, and code review were the only tools a developer had to work with in the golden days of coding. with the birth of code generators such as codesmith, development teams are now armed with a powerful tool to expedite the development process and maintain a standard throughout the application. when developing applications, templates can save you a lot of time by using common design patterns for generating different business objects. by definition, templates are repeatable patterns, which can be derived from any design methods. the benefit of using templates in code generation is the ability to enforce design methods and standards while saving development costs for any application. this article will present a basic design pattern and the templates to generate code for the design pattern.

Business Object Design Pattern

Business Objects contain properties that reflect data; you will find objects named invoice, invoice detail, etc. commonly, they include embedded relationships -- for example, an invoice object may have a property that retrieves the details for that invoice instance.

Based on this simple observation, we can develop a design standard for our business objects:

  • Business Objects contain properties
  • Business Objects Require collections
  • Business Objects Require access data
Business Object:

The business object class template (Classgenerator. CST) Is used to create a business object's properties and methods. a standard set of methods for accessing the object's data will be generated for each object defined in an XML document. the following methods will be created for each business object defined:

Public static methods
  • CreateNewObject: Creates a newObject.
  • DeleteObject: DeletesObject.
Public instance Constructors
  • Object-Overloaded. initializes a new instance ofObjectClass.
Public instance methods
  • Copy: CopiesObject.
  • Delete: DeletesObject.
  • Fetch: FetchesObject.
  • Save: SavesObject.
  • Update: UpdatesObject.
Business Object collection:

The Business Object collections template (Collectiongenerator. CST) Is used to create a collection class for each business object. The collection object uses'CollectionBaseAs the base class. The following methods will be created for each business object defined:

Public instance Constructors
  • ObjectCollection: Constructor initializes a new instance ofObjectCollectionClass.
Public instance Properties
  • Count: Gets the number of elements contained inCollectionBaseInstance.
  • Item: Gets or sets the value associated with the specified key.
Public instance methods
  • Add: Adds an item to the object collection.
  • Contains: Determines whether the object collection contains a specificObject.
  • IndexOf: Determines the index of a specific item in the object collection.
  • Insert: Inserts an item to the object collection at the specified position.
  • Remove: Removes the first occurrenceObjectIn the object collection.
Protected instance methods
  • OnInsert: Validates before inserting a newObjectInto the object collection.
  • OnRemove: Validates before removing a newObjectFrom the object collection.
  • OnSet: Validates before settingObjectIn the object collection.
  • OnValidate: Performs additional custom processes when validatingObject.
Business Object Data:

The Business Object Data class template (Dataclassgenerator. CST) Is used to define the data access layer for each business object. The following methods will be created for each business object defined:

Public instance Constructors
  • ObjectData: Constructor initializes a new instance ofObjectDataClass.
Public instance methods
  • CreateNewObject: Creates a newObjectIn the database.
  • DeleteObject: DeletesObjectFrom the database.
  • FetchObject: Gets a singleObjectFrom the database.
  • UpdateObject: UpdatesObjectIn the database.
Base Object classes:

The Business Object base class template (Baseobjectclass. CST) Is used to create the base class for each business object. The following are the properties used for each business object:

Public instance Properties
  • Id: ID of business object.
  • Name: Name of business logic object.
Internal
  • GenerateObjectId: Generates an ID for the given object.
Base Object Data class

The Business Object base data class template (Basedataclass. CST) Is used to create the base class for each business object. The following are the properties used for each business object:

Public instance Constructors
  • BaseDataClass: Constructor initializes a new instance ofBaseDataClassClass.
Public instance Properties
  • ConnectionString: Gets connection string configuration from a config file.
Public instance methods
  • AddParamToSQLCmd: Adds a parameter toSqlCommand.
  • ExecuteCollectionReader: Returns an object collection from a SQL data reader.
  • ExecuteObjectReader: Returns an object from a SQL data reader.
  • ExecuteReaderCmd: Generates object collection from SQL data reader.
  • ExecuteScalarCmd: Executes a query.
  • SetCommandType: SetsCommandTextProperty to the name of the stored procedure.
Data Layer generator:

The data layer template (Datagenerator. CST) Is used to create the required database objects needed to support the business objects.

Procedures
  • Object_Create: Creates a newObjectIn the database.
  • Object_Delete: DeletesObjectFrom the database.
  • Object_Fetch: Gets a singleObjectFrom the database.
  • Object_Update: UpdatesObjectIn the database.
Using the generator

Based on a definition provided by an XML document, the generator will produce code for your business object, business object collection, Business Object Data Access, Business Object tables, and stored procedures.

Business Objects attributes

Attribute

Maps

Remarks

class

Table

Name will represent the table name

property

Column

Name will represent the column name

Class attributes

Attribute

Remarks

name

Represents the table name

Property attributes

Attribute

Remarks

name

Represents the column name

type

Represents a system type or a business object collection

maxSize

 
Supported types

C # type

Maps to SQL type

string

nvarchar

int

int

DateTime

smalldatetime

bool

bit

decimal

money

Defining the business objects:

Let's create a simple Invoice Business Object. The invoice object will contain in a customer name, invoice date, invoice paid status, and line items. The following XML document defines the objects:

Collapse
<?xml version="1.0" encoding="utf-8" ?><BusinessObjects xmlns="http://www.austinconnergroup.com"><class name="Invoice"><properties><property name="Id" type="String" maxSize="36" /><property name="Customer" type="String" maxSize="96" /><property name="InvoiceDate" type="DateTime" maxSize="" /><property name="InvoicePaid" type="bool" maxSize="" /><property name="InvoiceDetails" type="InvoiceDetailCollection" maxSize="" /></properties></class><class name="InvoiceDetail"><properties><property name="Id" type="String" maxSize="36" /><property name="InvoiceId" type="String" maxSize="36" /><property name="LineItem" type="Int" maxSize="" /><property name="ItemDescription" type="String" maxSize="96" /><property name="ItemCostPerUnit" type="Single" maxSize="" /><property name="ItemQuanity" type="Int" maxSize="" /><property name="ItemUnit" type="String" maxSize="8" /></properties></class></BusinessObjects>

In our definition document, we define two objects,InvoiceAndInvoiceDetail. You will notice thatInvoiceObject contains a propertyInvoiceDetailsWhich is of the typeInvoiceDetailCollection. This defines a relationshipInvoiceDetailAndInvoice, And will produce the required methods and SQL statements for this relationship.

Running the generator

Once we have our model, we are ready to generate our code.Buildtemplate. CSTIs the main entry point for generating the code for all the business objects, and has the following properties:

  • XmlFile-XML document that the classes will be generated from.
  • OutputDirectory-The base directory for code output.
  • GenerateBaseClasses-SettrueTo generate base classes.
  • RootNamespace-The root namespace generated classes will be a member.
  • ObjectNamespace-The object namespace generated classes will be a member.
  • BusinessLogicNamespace-The business object namespace generated classes will be a member.
  • DataAccessNamespace-The data object namespace generated classes will be a member.
  • DeveloperName-Developer's name.
  • CompanyName-Company name.
Points of interest

The article here presents the prototype that I developed for proof concept. after writing the generator and supporting templates, I was so amazed at the results, I immediately wrote the templates for our team and deployed them into our production environment. one of the biggest benefits was not the cost savings in time, but the fact that all the generated code was self documenting. this fact alone was worth the efforts in creating these templates. the biggest battle I faced everyday was the lack of your ented code, now it's a snap.

History
  • 11/23/2004-initial article.


About Jr hull

Software Engineering ECT with 20 + + years of software design and development experience with a strong hold in object-oriented software engineering using UML with design patterns. specified Ted and developed several industrial software packages and passed through full software development life-cycle. currently managing a small group of developers, developing management software for the agriculture industry.

Click here to view Jr hull's online profile.

Other popular design and architecture articles:
  • The razor framework: Part 1: plugins/extensibility

    An extensible dependency based plugin framework for. NET applications.
  • Design Patterns implementation in a storage explorer applicationa design patterns approach for designing and explaining a storage Explorer application. The application is used to need e file composition in a computer storage.
  • Leveraging. NET components and IDE integration: Ui AOP in an MVC use casean in-depth isolation of the features and the power. NET Component Model Architecture, its integration with the ide at design-time and the possiblities it opens through extensions at run-time.
  • The razor framework: Part 2: configuration/optionsan extensible dependency based plugin framework for. NET applications.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.