Factory Pattern

I had to use Factory pattern for one my mutual fund house project.The investment strategies includes buying funds as Lumpsum,SIP etc..and the funds which has be bought have options like Redeem,Purchase,Switch.This might not sound familiar if you are not from banking and finance domain,you can have a read over here.

Intention :

There are various Investment plans like SIP,LumpSum etc..and all investment plans has the following option (Purchase,Redeem,Switch).So Its clear that i would have an interface and classes below


public interface IFundOption

{

void Purchase();

void Redeem();

void Switch();

}

//Investment Plan

class Luupsum: IFundOption

{

public void Purchase()

{

//Do Purchase

}

public void Redeem()

{

//Do Redeem

}

public void Switch()

{

//Do Switch

}

}

class SIP : IFundOption

{

public void Purchase()

{

//Do Purchase

}

public void Redeem()

{

//Do Redeem

}

public void Switch()

{

//Do Switch

}

}

 

Problem  :

Given an investment plan as argument,i want to get the instance of investment plan and perform the various options like redeem,purchase etc..Typically i don’t want to write a switch case where i make a comparison based on investment plan and then initiate the class(against open closed principle). The goal is to write a method which accepts investment plan name and returns the instance of investment plan.


public investmentplaninstance   GetinstanceofInvesmentPlan(string investementplanname)

{

return investmentplaninstance ;// goal

}

This is where Factory Pattern comes in,You create a interface for creating object and let sub classes decide which class to initiate.

Factory Pattern :

1.Lets create a interface which is responsible for returning investmentplaninstance.


public interface IGetInvestmentPlanInstance

{

IFundOption CreateInvestmentPlanInstance(); //the instance should be used to create investment plan.

}

2. Lets create Managers (sub classes) which creates the instance of investment plan.


class SIPManager : IGetInvestmentPlanInstance

{

public IFundOption CreateInvestmentPlanInstance()

{

var sipinstance = new SIP(); //sip class implements IFundOption  interface.

return sipinstance;

}

}

class LumpsumManager : IGetInvestmentPlanInstance

{

public IFundOption CreateInvestmentPlanInstance()

{

var lumsuminstance = new Lumpsum(); //Lumpsum class implements IFundOption  interface.

return lumsuminstance ;

}

}

 

3. We have the managers who are ready to give the investment plan instance based on investment plan name.Typically we would put all manager class in settings file/config,and based on input,we create the manager instance based on reflection .


static IGetInvestmentPlanInstance GetManagerClass(string investmetpan)

{

var getmanager = _dictionary.Key(investmentplan);

return Assembly.GetExecutingAssembly.CreateInstance(getmanager) as IGetInvestmentPlanInstance ;

}

[/csharp]

we would make use of this manager class and get the instance of investment plan as below,



var getmanager = GetManagerClass("SIPManager ");

IFundOption options = getmanager.CreateInvestmentPlanInstance();

options.Purchase();

options.Redeem();

 

 

 

#Redis Cache in Azure-MVC

What is Redis Cache and Why do we need to move cache into separate server?

1.If you have a enterprise web application which is responsible for serving several thousand of request,it is advisable that you maintain a separate server for maintaining the cache,so that your cache does not eats up your server memory.(Azure)

2.Traditional Cache can operate only on Key/Value Pair,where as Redis Cache you have built-in data types like string,hash,list.It is more of a useful library which has several custom function which can be used over cache.

Creating an Sample Redis Cache Service in Azure.

1.Setting up Redis Service in Azure:

This is pretty easy one. You would need the Primary Connection String,make sure you add it to the web config of your file.

Capture

 

2.Setting up connection for your project.

1.Copy the Connection String from the Portal and add it to your web config

2.Make use of ConnectionMultiplexer class provided by .Net for connecting to the redis cache.


public class CacheInstance
{
private static ConnectionMultiplexer _connection;

public CacheInstance()
{
var constring = ConfigurationManager.ConnectionStrings["RedisCache"].ConnectionString;
_connection = ConnectionMultiplexer.Connect(constring);
}

public void PutItem(String Key,String Value)
{
IDatabase cache = _connection.GetDatabase();
cache.StringSet(Key,Value);
}

public String GetItem(String Key)
{
IDatabase cache = _connection.GetDatabase();
string redisValue = cache.StringGet(Key);
return redisValue;
}
}

In the above sample,i have created two sample methods Put and GetItem which would add and retrieve items from cache.

3.Using  Redis Cache in your Application.

This is Sample controller which makes use of redis cache in Azure,this application will cache the time when it first runs and will never update the time from then.


public class HomeController : Controller
{
public ActionResult Index()
{
var cache = new CacheInstance();

var res = cache.GetItem("Datetime");
if (String.IsNullOrEmpty(res))
{
cache.PutItem("Datetime",DateTime.Now.ToString());
}
return Content(res);
}
}

In the web page you can see that time is cached and never updated.

Capture

and here is the proof that we are hitting the azure redis cache,

Capture.JPG