|
|
|
|
|
|
|
Page Views :
|
1446
|
|
Downloads :
|
0
|
|
Rating :
|
Rate it
|
|
Level :
|
Intermediate
|
|
Moving from procedural programming languages such as ASP and Visual Basic to .NET object-oriented languages enables programmers to build systems that are highly extensible and flexible. In the first part I will demonstrate how to isolate business rules to a particular implementation class.
The Scenario
I have built an application that tracks bills for a company. I host the application for the company and perform modifications for a fee. A second company likes the product but adds new business rules in calculating the bill. The second company has requested a modification to the application to meet their business need.
Version 1 - VB Com Object
Public Class Bill Public Function calculateBill(ByVal CustomerName As String) As Double If CustomerName = "CompanyA" Then calculateBill = totalprice ElseIf CustomerName = "CompanyB" Then calculateBill = totalprice + comission End If End Function End Class
The Problem
As the sales person convinces new clients our billing system is the best, and these new clients will inevitably request more changes. The result is that the application will be filled with control logic to perform the special business logic for each customer.
A Possible solution
In this article I will show how to use a design pattern accompanied with inheritance to enable an application to become more flexible. A design pattern is a way to solve common problems from various applications with a standard implementation style. I am implementing a variation of the Factory Pattern. The Factory Pattern allows a requestor to get an object of a base type, but it does not specify which implementation object to create. Therefore the requestor is shielded from the creation of the object.
Version 2 - C# Objects
In our example above, the requestor was some user interface that will display the bill. The Bill class provides the base of implementation. In this example, I will create an intermediate class that the user interface will use to request the bill object. Based on which type of Company the user represents they will get an implementation class that is appropriate.
Sample Code:
using System; using myBusinessObjects; namespace ConsoleSample { class MySample { [STAThread] static void Main(string[] args) { // test harness for company B string currentCompany = "CompanyB"; //address new company's need CompanyBBill companyBBill = (CompanyBBill)BillFactory.getBill("CompanyB"); companyBBill.Commission = 9.2; Console.WriteLine(companyBBill.calculateBill().ToString ()); /* // test harness for company Astring currentCompany = "CompanyA"; //regular implementation Bill companyABill = BillFactory.getBill("CompanyA"); Console.WriteLine(companyABill.calculateBill().ToString()); */ } } } namespace myBusinessObjects { class BillFactory { public static Bill getBill(String CompanyName) { Bill myBill; if (CompanyName == "CompanyB") myBill = new CompanyBBill(); else myBill = new Bill(); return myBill; } } class Bill { protected double CostofProduct = 10.25; protected int NumberOfProducts = 3; public virtual double calculateBill() { return CostofProduct * NumberOfProducts; } public virtual double getDiscountPrice() { return CostofProduct * 0.8; } } //each derived special case class could be generalized and compiled //as needed then cached. will show in future article class CompanyBBill : Bill { //the extended data items could be generaicized, //I will show this in a future article public double Commission; public override double calculateBill() { // calls Bill.calculateBill and then adds commission return base.calculateBill() + Commission; //or you can provide other custom implementation //return NumberOfProducts * CostofProduct + Commission * NumberOfProducts; } } }
Explanation
The Bill class provides the basic design of the system. Most customers should want the functionality of the base Bill class. New customers who want additional functionality and class members can request objects that are derived from the base Bill class. In the sample above, the base class Bill calculates the bill by (# of products * price of Products). When a customer wants to modify this functionality, I create a new implementation class CompanyBBill that inherits all of the functionality of the Bill class. I inherited from Bill instead of creating a new class so that CompanyBBill will maintain all of the functionality of the original Bill. Thus, I do not have to rewrite getDiscountPrice().
The BillFactory class provides an intermediate between the client and the implementation class. If a customer requests a change you could make a new implementation class and alter the factory for the base class. If this technique was applied across all business objects, a customer could request functionality that changed in only one or multiple business objects.
Applying to the Real World
In future articles I will demonstrate how to expand this concept.
1) Building a user interface of dynamically loaded Web User Controls from a factory. The same patterns could be applied so that the user interface adapts to each customer needs and changes.
2) Have each Business Object that is changeable derive from a base class. This base class will have a collection of generic data objects driven by a database. Coding specific implementations into class files. The class files can be compiled at first run and cached on the server for future use. The class files will add functionality by overriding functionality in a base class and potentially accessing the generic data objects above. Once this framework is established you can do the following:
a) Release customer functionality with zero regression testing for other customers.
b) Publish your application and limited object model to customers and have their programmers alter functionality without having access to your base code. You can even charge for object models with different levels of detail!! c) Publish new versions of the application without affecting the base build.
d) Remove functionality of old customers with zero impact.
|
|
Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post
Here.
|
|
|
|
|
Login
to add your contents and source code to this article
|
|
|
|
|
|
|
|
|
|
|
|
wdenton
Wade Denton is a senior developer. Hobbies include watching the Cleveland Browns lose and Kentucky Wildcat's choke.
|
|
|
|
|
|
|
|
|
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional
consulting company, our consultants are well-known experts in .NET and many of them
are MVPs, authors, and trainers. We specialize in Microsoft .NET development and
utilize Agile Development and Extreme Programming practices to provide fast pace
quick turnaround results. Our software development model is a mix of Agile Development,
traditional SDLC, and Waterfall models.
|
|
Click here to learn more about C# Consulting. |
|
|
|
|
|
|
|
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon.
Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees.
As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
|
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
|
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
|
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
|
|
|
|
|
|
|
|
|
|
|
|
|