Thursday, November 13, 2008

ASP.NET - Using OOPs in real life

How to use OOPs
OOPs concepts are effectively using by high level coders...But college students & ordinary developers (like me) well know the OOPs concepts (inheritance, polymorphism, etc etc) , but the problem is where to apply these learned concepts in real life ?

OOPs in ASP.NET
Let me share you the ideas of where to use OOPs in ASP.NET, if you have enough knowlege & not sure where & how to use.

Hope all ASP.NET e-commerce developers would have worked with Payment Processors. (Ex. Paypal / Authorize.net /Google Checkout etc). And also you will have experiences such as your customer may frequently change his payment processors depending on their commission/sales charges. Also you may need to re-use this same payment code for other projects as well & need some touch-up works to work with others.

If you are doing the development works as above, lets follow how OOPs can save your time by creating our own oop components.


Planning your Requirements for all Projects
Before creating a component, plan the properties you have needed. In our example, if we are going to create a Payment Processor Component & some common properties we might needed are
1. MerchantId
2. First Name
3. Last Name
4. Contact Address..
5. Billing Name
6. Billing Address etc
7. Credit Card Number
8. Exp.Date
etc...etc.

Creating OOPs components
Base Class:
Create a Base Class, say named "Mustinherit Class PaymentProcessor" & have all the properties, you think that are essential/common inputs from Customer to charge.

Add MustOverride functions which you think common for all. Ex. ProcessPayment( )

Add Events like Success(transactionId as string) , Failed() etc

Derived Class:
Create a extension class, say "PaypalProcessor" inherits "PaymentProcessor"
Override ProcessPayment function & add the code you needed for Paypal workings. The function use the properties defined in base PaymentProcessor class. Depending on the transaction status, raise the related events.

Similarly you can develop any number of extension classes which inherits base class for all your Payment Processors.

GetInstanceClass:
In your PaymentProcessor component, add a class to provide the right instance required by application layer.
Shared Function GetProcessorInstance(ProcessorName as String) as PaymentProcessor
Select Case ProcessorName:
Case "Paypal"
return New PaypalProcessor()
Case "Authorize.Net"
return New AuthorizeProcessor()
End Function


Application Layer:
Later in Application Layer, depending on the cart customer payment mode selection, get the needed processor instance.

Ex:
Dim pProcessor as PaypalProcessor
pProcessor=GetInstance.GetProcessorInstance("Paypal")
pProcessor.FirstName=txtCustomerFirstName.text
pProcessor.LastName=txtCustomerLastName.text
....
...
....
pProcessor. ProcessPayment()

So now our application layer code is not bind or designed for any particular payment system. We can add/change the payment processors easily & can also reuse the PaymentProcessor code in all other projects too with out any change.

Conclusion
This is just to show a small light on OOPs usage way. You can apply your all known OOPs skills for areas where you regularly copy/paste from other projects + need of dynamic module include without changing Application Code.

Happy OOPs Coding!!!

No comments: