Tuesday, December 16, 2008

Business delegate pattern

Step 1

Create the following directories in your web/windows application (within App_Code directory).
1. business
2. common
3. services
4. delegates

Step 2

Let’s presume that you are working on a Project named “sample” and you have decided to use Business-Delegate model for it. To test the feasibility of the pattern, let’s take Invoice module. This module can have variety of sub-modules, like add, get, set, list, update, delete, save etc. For now let’s implement “Save” method

Step 3

Create a file IInvoice.cs in common folder

namespace Sample.common
{
public interface IInvoice
{
int Save();
}
}

Step 4

Create a file Invoice.cs in service folder with the following contents

Using Sample.common;
namespace Sample.services
{
public class Invoice : IInvoice
{
public Invoice()
{
}
public int Save()
{
return 0;
}
}
}

Step 5

Save this file as ServiceLocator.cs in delegate directory

Using Sample.common;
Using Sample.service;

namespace Sample.delegates
{
public class ServiceLocator
{
public ServiceLocator()
{
}
public IInvoice getInvoice()
{
return new Invoice();
}
}
}

Step 6

Save the following contents as BusinessDelegate.cs in the directory business

Using Sample.delegate;

namespace Sample.business
{
public class BusinessDelegate
{
private ServiceLocator sLocator;
public BusinessDelegate()
{
sLocator = new ServiceLocator();
}
//you can have a cutom name for this function
public int Save()
{
return sLocator.getInvoice().Save();
}
}
}

Step 7

You are all set to use this from your view

Using sample.business;
Public button_save_click(object sender, EventArgs e)
{
BusinessDelegate bDelegate = new BusinessDelegate();
bDelegate.Save();
}

If you wish to add additional functionalities like add, list…then you add them to you IInvoice interface, and Invoice class. Create a new method for invocation in BusinessDelegate and ServiceLocator class

No comments: