Tuesday, January 10, 2012

Software Basic Architecture

In this post I would like to explain some basic software architecture. If you going to build your new application from scratch and you decide to build application that will be right OO designed, following architecture can helps you in.



Doesn't matter, you going to build Winform, WPF or Web application the basic architecture still to be same.

Before you going to read this post, I suggest you to be familiar with Dependency Injection (see my previous posts) and MVC and / or MVVM pattern.

1. Represents Client View - it's may be HTML, WPF or Winform application. This part of application not so 'interesting' for us and not an issue in this post. After some interaction accured with user and request has been sent to the server (in case of Web application) we suppose to come to Controller / CommandHandler or some else object that responsible to get response from client and decide what going on with. Let's take example of currency calculator. In this case we got some data model from client, like: 

Pseudo Code:

class CurrencyModel:
{
     string currencyCodeFrom = 'USD', 
     string currencyCodeTo = 'EUR'
     decimal quantityToConvert = 100.0
     decimal quantityAfterConversion = 0.0
}

OK, we have our first model, that arrived to  our first layer (number 3 on pic.1)

First of all we would like to save current state of our main model, before we going to change it or may be not, any way. 
We need to create DataModelManager that will holds all Models that inherit from  IMainCurrencyModel that inherit from IDataModel interface all application session or until we will delete it from. 

MainCurrencyModel has method Update(CurrencyModel currencyModel):

class MainCurrencyModel
{
     public CurrencyModel _currencyModel {get; set;}

     Update(CurrencyModel currencyModel)
     {
           _currencyModel = currencyModel;
     }
}

So, now we have data to going on to service. Now we need to get service provider in order to refer to service and get back quantityAfterConversation. Suppose,  service wait from us currencyCodeFrom, currencyCodeTo and  quantityToConvert and after perform all works for us return us in response quantityAfterConversion.

OK, let's assume that we have two service providers (or maybe more) of currency conversions rates. In this case we don't want create service provider with 'new' and we will user service manager build for us list of all service providers we have define in our application scope and let to Business Logic to decide which one to use.
While creating service provider object we must inherit from IServiceProvider. While application starts ServiceManager running and imports [imports many] all objects inherits from IServiceProvider (using MEF dependancy injection framework).

Now we need get from a list of providers one we need to use in our case, let's say we will use 'ChippestConversionServiceProvider':

pseudo code:

//let's save our current DataModel
var mainDataModel = DataModelManager<MainDataModel>();
mainDataModel.Update(CurrencyModel);

//get our serviceProvider we need according our Business Logic 
var serviceProviders = ServiceManager.GetServiceProvider<ChippestConversionServiceProvider>();
serviceProvider.GetQuantityAfterConversion(mainDataModel );

In case you need to use another serviceProvider you can take from all list of serverProviders:

var serviceProviders = ServiceManager.GetServiceProvider();

provider you need to use according to specific logic:

Example:

if(currencyCodeFrom  == 'USD')
   var serviceProvider = serviceProvider.Where(x=>x.GetType == 'ChippestConversionServiceProvider')
else
  var serviceProvider = serviceProvider.Where(x=>x.GetType == 'RapidConversionServiceProvider')

Of course all providers must realize GetQuantityAfterConversion() method.

OK, we finished explanation of 3,4,9 parts of pic. 1

To be continued :-)