Tuesday, December 1, 2009

Design Patterns – Memento Pattern

The Memento Pattern is useful to remember the state of object without keeping copy of the whole object. Copying of the entire object is sometimes inefficient as the copy eventually contains much more information that we need to restore back.



Memento



Memento is the object that can remember the internal state of another object.


// Memento. Uses deep copy and serialization
// to save/restore the state of type TState.
public class Memento<TState>
{
MemoryStream stream = new MemoryStream();
BinaryFormatter formatter = new BinaryFormatter();

public Memento<TState> Save(TState state)
{
formatter.Serialize(stream, state);
return this;
}

public TState Restore()
{
stream.Seek(0, SeekOrigin.Begin);
TState state = (TState)formatter.Deserialize(stream);
stream.Close();
return state;
}
}



The Memento in this example uses generics to ensure type consistence and prevent using type conversion.

Originator



Originator is the object which state to remember and restore back if needed. The Originator has two methods for that:
- GetMemento() that returns the state of the object
- and SetMemento() receives back state to restore from


// The Originator has one property that returns
// the complete state of the object.
public interface IOriginator<TState>
{
TState State { get; }
}

// Holds the state of the Originator
[Serializable]
public class ProductState
{
public string Name { get; set; }
public float Price { get; set; }
}

// Originator
// The complete state is moved to one property
// to generalize the memento pattern.
[Serializable]
public class Product : IOriginator<ProductState>
{
public Product()
{
this.State = new ProductState();
}

public Product(string name, float price)
: this()
{
this.State.Name = name;
this.State.Price = price;
}

public ProductState State { get; private set; }

public Memento<ProductState> GetMemento()
{
return new Memento<ProductState>().Save(State);
}

public void SetMemento(Memento<ProductState> memento)
{
State = memento.Restore();
}

public override string ToString()
{
return String.Format(System.Globalization.CultureInfo.InvariantCulture,
"{0} {1:f2}", State.Name, State.Price);
}
}




Caretaker



Caretaker is the object that controls when to create Memento but the Originator will use the stored into the Caretaker state to restore from.


// Usage example and Caretaker as the same time.
static void Main(string[] args)
{
// Caretaker is in this example the class that holds the Main() void
// or the Main() void itself of you want.
Memento<ProductState> memento = new Memento<ProductState>();

Product product = new Product("Name one", 100f);
Console.WriteLine("Initial state: " + product.ToString());

memento = product.GetMemento();
Console.WriteLine("The state was saved to memento");

// Change the state of the Originator
product.State.Name = "Name two";
product.State.Price = 200f;
Console.WriteLine("State changed: " + product.ToString());

// Change the state of the Originator
product.State.Name = "Name three";
product.State.Price = 300f;
Console.WriteLine("State changed: " + product.ToString());

product.SetMemento(memento);
Console.WriteLine("The state restored from memento");

Console.WriteLine("Reverted state: " + product.ToString());

Console.ReadLine();
}




which gives after running the following output:




The Memento is save-state undo command, where the captured is a backup. In this example the Memento object used in-memory storage to keep backup of state, but the Memento object could also even persist state into a database.

Atanas Hristov

kick it on DotNetKicks.com
Shout it

Saturday, October 31, 2009

AJAX publish/subscribe

Calling long running web server process and locking the user for a while in web applications with the typical for the HTTP request/replay way can be critical for the user expirience. Even more, possibly we don't get back the data due to problems with the network connection or browser timeouts.

Even if on top of unreliable protocol like HTTP we can provide some abstraction level to ensure we'll get the result from the web server. We could simulate the publish/subscribe pattern in ajax. Furthermore our communication with the web server is asynchronous and allows our application to respond better to the user interactions.

On the web server we could implement multithreading and open working threads which get the calculations done.

With javascript we send request to the web server and then in some interval of time we again and again ask if the server has prepared the result. When the web server has collected the information we need, we take the data from the web server, display on the page and stop the repetative calls to the web server.

I will implement publish/subscribe with ASP.NET MVC and jQuery.

The CityWeather model



First i'll create model class CityWeather. The only public properties of CityWeather object are "City" and "Temperature".

After CityWeather class created, the constructor calls Measure() in thread. The Measure() function simulates long running process used to collect the weather information for that city.

We store all the requests for measurements in static collection _measurements. The keys of this collection are the city names.

The only public function of the model class is the static function GetCityWeather() which when called will check if in _measurements exists object CityWeather for this city. If not will create one. The function returns back the corresponding CityWeather object from the _measurements collection.



namespace PublishSubscribe.Models
{
// Measurements per city.
// Call class method CityWeather.GetCityWeather(city)
// to get CityWeather object with measurements
// for that city.
// Repead calling CityWeather.GetCityWeather(city)
// until city.Temperature is not null.
public class CityWeather
{
// Holds all requested measurements.
// Used from class method GetCityWeather()
// The keys are city names.
// The values are CityWeather objects.
private static Dictionary<string, CityWeather> _measurements =
new Dictionary<string, CityWeather>();

// public properties of CityWeather object:
public string City { get; private set; }
public int? Temperature { get; private set; }


// The constructor itself starts measurement
// in an asynchronous thread.
private CityWeather(string city)
{
this.City = city;

// after the object is constructed will start
// doing measurement in a thread, which respectively
// takes some time
ThreadStart ts = new ThreadStart(Measure);
Thread th = new Thread(ts);
th.Priority = ThreadPriority.Lowest;
th.Start();
}

// Worker method started in separate thread from
// the constructor
private void Measure()
{
// measurement takes some time - up to a minute
Thread.Sleep(new Random().Next(1,6) * 10000);

// lock the object alowing concurent access
// to the object properties
lock (this)
// degrees Celsius between 10 and 20
Temperature = new Random().Next(10, 20);
}


// The only public method that capsulates all the logic
// of keeping single instances - one per city -
// just as static memory for the purpose of the
// demonstration.
//
// Keep calling CityWeather.GetCityWeather(city) until
// you get back an CityWeather object with
// property Temperature which is not null.
public static CityWeather GetCityWeather(string city)
{
lock (_measurements)
{
if (! _measurements.ContainsKey(city))
{
CityWeather cw = new CityWeather(city);
_measurements.Add(city, cw);
}

return _measurements[city]; // check
}
}
}
}



The usage of this class is pretty simple. We call the static function CityWeather.GetCityWeather() with the name of a city to get weather measurements for that city. We repeat the call again and again until we get back CityWeather object where Temperature property is set - is not null, but contains value.



The Weather controller



Then I'll create the controller class:



namespace PublishSubscribe.Controllers
{
public class WeatherController : Controller
{
public JsonResult Measure(string city)
{
return Json(Models.CityWeather.GetCityWeather(city));
}

public ActionResult Index()
{
return View();
}
}
}



The controller has two methods. Measure gets weather measurements for one given city. We send back JSON result.

The Index method we will use to create page where we will demonstrate how the AJAX publish/subscribe calls will work.

We are ready to try how the Measure JSON handler will respond and to see the how the CityWeather model will work. We run the application and navigate to /weather/measure where we request measurements for a city and repetative hit repoad on the browser until we receive back the Temperature calculated.

Here is how example session with repetative calls looks like:




URL: http://localhost:3284/weather/measure/?city=berlin
Response: {"City":"berlin","Temperature":null}
...
URL: http://localhost:3284/weather/measure/?city=berlin
Response: {"City":"berlin","Temperature":null}
...
URL: http://localhost:3284/weather/measure/?city=berlin
Response: {"City":"berlin","Temperature":12}




AJAX publish/subscribe with jQuery



Now I'm going to implement the web page to demonstrate the idea behind the AJAX publish/subscribe.

The html body tag is very simple.



<body>

<label for="city">City:</label>
<input type="text" id="txtCity" />
<input type="button" value="measure" id="btnMeasure" />

<table id="cites">
<thead>
<tr><td style="width:200px">City</td><td>Temperature</td></tr>
</thead>
<tbody>
</tbody>
</table>

</body>



We write city name into the input box and click the "measure" button. We repeat that several times with other city names. We expect whenever the server has measured the weather for a city we asked for, the result will be added to the table as new row. The order of the cities we ask for measurements is not necessarily the order we'll get back calculated measurements. The time the server will spend to take weather measurements may vary from city to city and is not presumable.

This way we will have asynchronious communication with the server and the user will appreciate the better respond from the application.


Last, I'll put some javascript after the closing body task and will automate the html page with jQuery.

As we don't have multithreading in javascript we simulate it with setInterval and clearInterval. We start repetative calls as we run sendMeasureRequest(city) every second and ask the web server if the measurements for that city are calculated. Once the server gives back calculated results we stop the repetative calls.

The setInterval() function gives us interval ID which we later use at calling clearInterval() to stop repetative tasks. Then we also need to map somewhere interval ID to city name, thereof we create array measurements which keys are city names. The values of the measurements dictionary are data structures wich hold interval ID, city name and the temperature as measured from the web server.

Once the web server respond calculated measurement we stop the corresponding repetative calls to the web server and call the function cityMeasured(). We add new row into the table with the measurements.



<script type="text/javascript">

/* Hash array of all collected measurememts */
var measurements = new Array();

/* Does weather measurement for a city */
function measureCity(city) {
if (measurements[city] == undefined) {
// send request to the server every one second
var intervalID = setInterval(sendMeasureRequest, 1000, city);
measurements[city] = {
IntervalID: intervalID,
City: city,
Temperature: null
};
sendMeasureRequest(city);
}
cityMeasured(city);
}

/* Send GET request to the server to collect weather measurements for a city */
function sendMeasureRequest(city) {
$.ajax({
'url': '<%= Url.Action("Measure") %>',
'type': 'GET',
'dataType': 'json',
'data': { 'city': city },
'success': function(data) {
if (data.Temperature != null) {
clearInterval(measurements[data.City].IntervalID);
measurements[data.City].Temperature = data.Temperature;
cityMeasured(data.City);
}
}
});
}

/* Does weather measurement for a city */
function cityMeasured(city) {
if (measurements[city].Temperature != null) {
if ($('#tr_' + city).length == 0) {
$("#cites > tbody").append('<tr id="tr_' + city + '"><td>'
+ measurements[city].City
+ '</td><td>'
+ measurements[city].Temperature + ' grad'
+ '</td></tr>');
}
}
$('#tr_' + city).fadeOut();
$('#tr_' + city).fadeIn();
}

/* SetUp click handler */
$(document).ready(function() {
$("#btnMeasure").click(function() {
var city = $("#txtCity").val();
measureCity(city);
});
});
</script>



After I ran the application and navigated to /weather/ I asked for weather measurements for the following cities in the exact order: Paris, Boston, Rom, Hamburg

Then I waited few seconds and the weather measurements were displayed to the page one after other:




Well, we may consider as next to be done, once the weather in city from the table has charnged, we refresh the information on the table.

Atanas Hristov

kick it on DotNetKicks.com
Shout it

Tuesday, October 13, 2009

Design Patterns – Abstract Factory Pattern

Factories are objects that encapsulate the logic for creating other objects.

Factory object could create one or another object based on some configuration parameters. Or the factory could decide what kind of concrete object to create based on a parameter to some object creational method. For example if the method received post-code the factory creates City object and when the parameter is an email address the factory creates Customer object. In that way one might create factory to select one or another kind of object from database based on the user input.

Abstract Factory



The Abstract Factory is that encapsulates the way of creating concrete objects that have lot of common.

Often the created from abstract factory objects derive from same base class or share some interface. Abstract factory object for example could be used to create fake objects for the purpose of testing the software, and create regular objects otherwise in production mode. The fake object and the regular object derive from same base class or share same interface. The client code deals only with instances of the base type and has no knowledge of the concrete implementation.

I will give an example of abstract factory created with C#.

Reflection based abstract factory



The central feature of our abstract factory is to create concrete objects based on key/value pairs.

We register the types the factory is able to create into a dictionary. For registration of the types the factory may create we expose method Register().

Our base abstract factory class uses reflection to call the object constructors of the concrete type. That is done in method CreateInstance(). We invoke CreateInstance() with:



  • key - that will be used to create one concrete object or another as it is registered already via Register().


  • typeParams - if the concrete object has parameterized constructor which we want to use, then we specify the parameter types in an array.


  • valParams - if the concrete object has parameterized constructor which we want to use, then we specify the parameter values in an array.





Following is a implementation of reflection based abstract factory:




// T1 is the appropriate key type.
// T2 is the base type from which derive all the types the factory can create.
public abstract class AbstractFactory<T1, T2>
{
// Creates the factory.
// T1 is the appropriate type.
// T2 is the base type from which derive all the types the factory can create.
public static AbstractFactory<T1, T2> CreateFactory()
{
throw new Exception("Override FactoryReflected.CreateFactory()!");
}

// Register for types the factory can create.
protected Dictionary<T1, Type> _registered = new Dictionary<T1, Type>();

// Registers the specified factory types.
protected void Register(T1 key, Type type)
{
_registered.Add(key, type);
}


// Creates new instance of type by enumeration key.
public T2 CreateInstance(T1 key, Type[] typeParams, object[] valParams)
{
Type type = _registered[key];

System.Reflection.ConstructorInfo cInfo =
type.GetConstructor(typeParams);

return (T2)cInfo.Invoke(valParams);
}

}


We also have used generics to make the implementation of reflection based abstract factory more generalized.




Personal name



Let's face the problem and how we are going to find a solution. The different societies have different conventions for personal full name. In Russia the common order is "family-name given-name". In some situations the family-name is capitalized. In west Europe the usual convention is "given-name family-name", etc.

Our problem is - we are going to use the appropriate form of address when we communicate with people from different regions. For simplicity assume we have the family and the given name. We need a way to compose appropriate full name.

First we have the base class Name. The Name class has first and last name properties. We also have an abstract method GetFullName() which should give different result for different society. Furthermore, the concrete implementation will be done in derived classes:



// Base abstract class from which concrete implementations derive
public abstract class Name
{ // see http://en.wikipedia.org/wiki/Full_name#Naming_convention

protected string firstName;
protected string lastName;
protected Name() { }
public Name(string first, string last)
{
this.firstName = first;
this.lastName = last;
}

// Full name can be composed differently
// as the different societies have they different
// naming conventions
public abstract string GetFullName();
}

// composing Western full name
public class WesternName : Name
{
public WesternName(string first, string last) : base(first, last) { }
public override string GetFullName() { return firstName + " " + lastName; }
}

// composing Eastern full name
public class EasternName : Name
{
public EasternName(string first, string last) : base(first, last) { }
public override string GetFullName() { return lastName + " " + firstName; }
}

// another Eastern full name convention
public class EasternOfficialName : Name
{
public EasternOfficialName(string first, string last) : base(first, last) { }
public override string GetFullName() { return lastName.ToUpper() + " " + firstName; }
}





Concrete factory



Having the abstract factory from above we create concrete factory used for creation of objects of classes derived from the Name class. The NameFactory contains the initialization of itself in method CreateFactory(). If we create new concrete class derived from class Name to fit another convention for personal full name then we also have to register that new class in CreateFactory() method.



public class NameFactory : AbstractFactory<string, Name>
{
private NameFactory() { }

public static new NameFactory CreateFactory()
{
NameFactory factory = new NameFactory();

// Register which concrete type to create for which key
factory.Register("WesternOrder", typeof(WesternName));
factory.Register("EasternOrder", typeof(EasternName));
factory.Register("EasternOfficialOrder", typeof(EasternOfficialName));

return factory;
}
}




We will create instances of NameFactory only via the static method CreateFactory() and thereof we have specified the default constructor to be private.






Using the concrete factory



We create nameFactory object and then call one after another GetFullName() of all possible derived from class Name objects. These derived from Name objects we create as we call method CreateInstance() of nameFactory.



class Program
{
static void Main(string[] args)
{
NameFactory nameFactory = NameFactory.CreateFactory();

foreach (string convention in new string[] {
"WesternOrder", "EasternOrder", "EasternOfficialOrder", })
{
Name name = nameFactory.CreateInstance(convention,
new Type[] { typeof(string), typeof(string) },
new object[] { "Atanas", "Hristov" });

Console.WriteLine(name.GetFullName());
}


Console.Read();
}
}



And finally the output to the console:






Atanas Hristov

kick it on DotNetKicks.com
Shout it

Friday, September 25, 2009

Design Patterns – Singleton Pattern

The Singleton pattern gives us a way to restrict the instantiation of objects of given class to certain number, in the common usage to one only.

The Singleton class instantiates itself and works as a global repository for an instance of itself. For example we share a database connection or log writer across all threads in one process.

Implementation of Singleton pattern



Here i'm going to implement Singleton class in C#. The class should hide its constructor. It has also GetInstance() method that gives back the only concrete instance. We embed the concrete instance of the class into the static variable "obj".

For the purpose of the demonstration we have one public method Add() used to add some number to the class and Total() which gives back the total of the added numbers.



public class Singleton
{
private static Singleton obj = new Singleton(); // holds a reference to the only concrete instance

private static int total = 0; // we add numbers up to this via Add()

private Singleton() { } // hiden constructor

public static Singleton GetInstance() { return obj; } // returns the only concrete instance

public void Add(int number)
{ // add up to the total
lock (obj) // synchronize in concurrency
{
Thread.Sleep(new Random().Next(1000)); // randomly wait up to a second
total += number;
Console.WriteLine(String.Format("Add {0} to total", number));
}
}

public int Total()
{ // gets the total
lock (obj)
return total;
}
}




In Add() method we have a timeout up to a second to randomize the time to run the method.

We also locked the access to the class variables for concurrency. We are going to test the Singletion class in multithreaded environment.


Usage of Singleton pattern




First we create three static helper methods which will make calls to Add() with different number from one to three.



class Program
{
// add
static void HelperOne() { Singleton.GetInstance().Add(1); }
static void HelperTwo() { Singleton.GetInstance().Add(2); }
static void HelperThree() { Singleton.GetInstance().Add(3); }
...



Then in Main() we run three threads and run concurrently the three static helper methods.

We wait for the threads to finish and show the total of all additions to the singleton class.




static void Main(string[] args)
{
Thread[] threads = new Thread[]
{
new Thread(new ThreadStart(HelperOne)),
new Thread(new ThreadStart(HelperTwo)),
new Thread(new ThreadStart(HelperThree))
};

foreach (Thread thread in threads) { thread.Start(); }
foreach (Thread thread in threads) { thread.Join(); }

Console.WriteLine(String.Format("The total is {0}",
Singleton.GetInstance().Total()));

Console.Read();

}



Run the example



Running that I've got the following output:




Happy codding!

Atanas Hristov


Shout it

Friday, September 11, 2009

Design Patterns – Chain of Responsibility Pattern

Imagine you have complicated decision logic and you’ve got a big if-then-else like structure in your code which you want to simplify.
The Chain of Responsibility Pattern is a good way for code refactoring in such situation. It will make the code more flexible and easy to support and modify in the future.

You prepare a set of interdependent chain handler classes linked in a chain. Every chain handler class implements part of the decision logic and has a link to next chain handler object. A request data object is passed thru the chain. Eventually one of the handlers on the chain matches some decision criteria, does data processing and ends up the run flow. This would be the handler objects in the chain that takes the responsibility and does data processing having received the request data object.

In this example we will try to programmatically come up with a conclusion based on passed in to the chain two boolean parameters. For every possible combination of those parameters we are going give a conclusion back, so we have four different possible results. Otherwise we could do things using conditional structures like:



If Param1 is true
If Param2 is true
Conclusion1
Else
Conclusion2
Else
If Param2 is true
Conclusion3
Else
Conclusion4


Based on that conditional structure we will create chain of responsibility with four different chain handler classes covering all decision making combinations.

We will use C# as a programming language for the examples.

Chain Handler Interface and Base Class



We start with the definition of the chain handler interface. The chain handler object stores internally a request data object. We have a way to set if there is a handler next on the chain. The handler has Run() void where the decision making and eventually data processing will be done.



// All handler objects implement common interface.
// T: request object- holds data to process
// and the result of the processing
public interface IChainHandler<TRequest>
{
IChainHandler<TRequest> SetNextLink(IChainHandler<TRequest> next); // set link to next handler
void Run(); // does processing
}



As we will have four chain handler classes and they all have a lot in common, we create parent abstract base class for chain handler.



// base class for all concrete chain handler objects
public abstract class ChainHandlerBase : IChainHandler<RequestData>
{
// the request data object
protected RequestData _requestData;

// a reference to next chain handler object
private IChainHandler<RequestData> _nextHandler;

// hide parameterless constructor
private ChainHandlerBase() { }

// expose constructor with parameters
public ChainHandlerBase(RequestData requestData)
{
_requestData = requestData;
}
// set the reference to the next chain handler object
public IChainHandler<RequestData> SetNextLink(IChainHandler<RequestData> nextHandler)
{
_nextHandler = nextHandler;
return _nextHandler;
}

// let the concrete implementation make processing decision
public abstract void Run();

// run next handler in the chain if defined or end up the processing
protected void RunNext()
{
if (_nextHandler != null)
_nextHandler.Run();
}

}



Request Data Object



A step back – we need to define the request data object which we’ll pass thru the chain. This object holds two input boolean parameters and a holder where we write the conclusion message back.



// Data to pass thru the chain.
// Based on this data the chain
public class RequestData
{
// pass in parameters
public bool HaveLotOfMoney { get; set; }
public bool HaveBrilliantIdea { get; set; }

// a message to send back
public string MessageBack { get; set; }
}




Concrete Chain Handler Implementations



Now we are ready to define concrete chain handler classes and cover all possible combinations of the input parameters.



// concrete chain handler object
public class ChainHandlerOne : ChainHandlerBase
{
public ChainHandlerOne(RequestData requestData) : base(requestData) { }
public override void Run()
{
if ((_requestData.HaveLotOfMoney) && (_requestData.HaveBrilliantIdea))
{
_requestData.MessageBack = "I am with you.";
return;
}
RunNext();
}
}

// concrete chain handler object
public class ChainHandlerTwo : ChainHandlerBase
{
public ChainHandlerTwo(RequestData requestData) : base(requestData) { }
public override void Run()
{
if ((_requestData.HaveLotOfMoney) && (!_requestData.HaveBrilliantIdea))
{
_requestData.MessageBack = "Well done. Go on pension.";
return;
}
RunNext();
}
}

// concrete chain handler object
public class ChainHandlerThree : ChainHandlerBase
{
public ChainHandlerThree(RequestData requestData) : base(requestData) { }
public override void Run()
{
if ((!_requestData.HaveLotOfMoney) && (_requestData.HaveBrilliantIdea))
{
_requestData.MessageBack = "What a nice idea.";
return;
}
RunNext();
}
}

// concrete chain handler object
public class ChainHandlerFour : ChainHandlerBase
{
public ChainHandlerFour(RequestData requestData) : base(requestData) { }
public override void Run()
{
if ((!_requestData.HaveLotOfMoney) && (!_requestData.HaveBrilliantIdea))
{
_requestData.MessageBack = "Don't give up.";
return;
}
RunNext();
}
}




Set Up the Chain



We are ready at this point to put in order the chain. We create chain handler objects and link them in a appropriate order. We send to the first handler in the chain a request data object and hit the Run() method to get the result calculated.



// wraps the chain initialization and processing code
static RequestData WhatAboutMe(RequestData requestData)
{
// construct the first chain handler.
ChainHandlerBase firstHandler = new ChainHandlerOne(requestData);

// arrange chain of responsibility as list of chain handker objects
firstHandler
.SetNextLink(new ChainHandlerTwo(requestData))
.SetNextLink(new ChainHandlerThree(requestData))
.SetNextLink(new ChainHandlerFour(requestData));

// start processing of the request data
firstHandler.Run();

// return back the request data with the message created
return requestData;
}



Run the Chain



The win of utilizing chain responsibility pattern is the extensibility of our code in future. If we need additional logic we have to implement new chain handler class. We also may construct the chains in one or another way, we can order the chain handler object based on concrete needs. We absolutely take care of single responsibility principle and we divide a long and complicated to maintain code into well testable small classes.

Finally let’s run our chain of responsibility implementation. We’ll send every possible combination of input to the chain and see what’ll happen.




static void Main(string[] args)
{
Console.WriteLine(WhatAboutMe(new RequestData {
HaveLotOfMoney = true, HaveBrilliantIdea = true }).MessageBack);

Console.WriteLine(WhatAboutMe(new RequestData {
HaveLotOfMoney = true, HaveBrilliantIdea = false }).MessageBack);

Console.WriteLine(WhatAboutMe(new RequestData {
HaveLotOfMoney = false, HaveBrilliantIdea = true }).MessageBack);

Console.WriteLine(WhatAboutMe(new RequestData {
HaveLotOfMoney = false, HaveBrilliantIdea = false }).MessageBack);

Console.Write("Hit Enter to finish.");
Console.ReadLine();
}



Running that we get on the console like:



Atanas Hristov

kick it on DotNetKicks.com
Shout it

Monday, September 7, 2009

Upload file to database with ASP.NET MVC and Castle Active Record

A file upload function to web application raises the question where to store the uploaded data. One possibility is to just store into the file system. This is inexpensive and easy to implement, but you have to think for file backups. If you don’t have huge amount of data you can also upload files directly into a database using binary format for table column to store the content of the file.

In this post I’m going to give a try of the second option and create simple application to store uploaded files directly into the database.

I will use Postgres database to store the file uploads and as ORM I will use Active Record from the Castle project. Then I need a driver for .NET to connect to the database, we will use the Npgsql driver.

With Castle Active Record you can easy switch to another database server; it’s just matter of switching the referenced driver and replace appropriately the configuration into Web.config file.

Assume we have PostgreSQL Server on localhost up and running and we will connect to the database "postgres" as user "postgres".




Create the project and set the configuration



We start with creating new ASP.MVC Application in Visual Studio and give it the name of "UploadToDb".

We then add references to the following assemblies from our project:

NHibernate.dll
Castle.ActiveRecord.dll
Castle.Components.Validator.dll
Castle.Core.dll
Castle.DynamicProxy.dll
Iesi.Collections.dll
Npgsql.dll

We change the Web.config file and add the appropriate settings for Active Record.

In "configSections" we add new section sub element for the Active Record. Next to "configSections" we add "activerecord" element where we specify the connection parameters and the database driver we are going to use.




<configuration>
<configSections>
<section name="activerecord" type="Castle.ActiveRecord.Framework.Config.ActiveRecordSectionHandler, Castle.ActiveRecord" />
….
</configSections>
<activerecord isWeb="false" isDebug="true">
<config>
<add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider"/>
<add key="hibernate.dialect" value="NHibernate.Dialect.PostgreSQL81Dialect"/>
<add key="hibernate.connection.driver_class" value="NHibernate.Driver.NpgsqlDriver"/>
<add key="hibernate.connection.connection_string" value="Server=localhost;Port=5432;Database=postgres;User ID=postgres;Password=;"/>
</config>
</activerecord>







Database table and domain model



First we'll create Active Record class for the domain object "Upload" into Models directory. This is an entity object and has property Id which is a primary key into the database. We’ve got also data property which holds the binary data from the file we upload. There is also mimetype property which holds the MIME type of the uploaded file.




using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Castle.ActiveRecord;
using Castle.ActiveRecord.Framework.Scopes;
using NHibernate.Expression;
using System.Data;
using System.Collections;

namespace UploadToDb.Models
{
[ActiveRecord("uploads")]
public class Upload : ActiveRecordBase<Upload>
{
private int id = 0;
[PrimaryKey(PrimaryKeyType.Sequence, SequenceName = "uploads_seq")]
public int Id
{
get { return id; }
set { id = value; }
}

private byte[] data = new byte[] { };
[Property]
public byte[] Data
{
get { return data; }
set { data = value; }
}

private string mimetype = "";
[Property]
public string Mimetype
{
get { return mimetype; }
set { mimetype = value; }
}

public static Upload FindById(int id)
{
return FindOne(Expression.Eq("Id", id));
}
}
}




To search uploaded files into the database we use the above implemented method FindById(int) which is just used to enclose and wrap that extra Actie Record syntax with the expressions. Otherwise we have a lot of search capabilities coming with Active Record, for example to get list of all file uploads we use Models.Upload.FindAll() as the class Models.Upload inherits from ActiveRecordBase class.


We create table to store the uploaded files



CREATE TABLE "uploads" (
"id" int DEFAULT 0 NOT NULL,
"mimetype" Varchar NOT NULL,
"data" Bytea
);

ALTER TABLE "uploads" ADD CONSTRAINT "uploads_pk" PRIMARY KEY ("id");

CREATE SEQUENCE uploads_seq START 1;
ALTER TABLE uploads ALTER COLUMN id SET DEFAULT nextval('uploads_seq'::text);








Set up the application



At application start we will initialize Active Record. Open Global.asax.cs file and edit method Application_Start(). After RegisterRoutes we add the initialization piece of code:



protected void Application_Start()
{
RegisterRoutes(RouteTable.Routes);

// load configuration from Web.config
Castle.ActiveRecord.Framework.IConfigurationSource source =
System.Configuration.ConfigurationManager.GetSection("activerecord")
as Castle.ActiveRecord.Framework.IConfigurationSource;

// Initialize and mention which types define our model
Castle.ActiveRecord.ActiveRecordStarter.Initialize(source,
typeof(Models.Upload));
}







The UploadController class



Basically we’d need three different URLs: Add – to upload new file; Get- to get uploaded file and finally Index which gives us list of all uploads.

We’ll create UploadController class in Controllers directory. Then we implement the three methods.




using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;

namespace UploadToDb.Controllers
{
public class UploadController : Controller
{
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Add(HttpPostedFileBase file)
{
if (file != null)
{
Models.Upload upload = new UploadToDb.Models.Upload();
upload.Mimetype = file.ContentType;
upload.Data = new byte[file.ContentLength];
file.InputStream.Read(upload.Data, 0, file.ContentLength);
upload.Save();
}
return Index();
}

public FileContentResult Get(int id)
{
Models.Upload upload = Models.Upload.FindById(id);
return File(upload.Data, upload.Mimetype);
}

public ActionResult Index()
{
this.ViewData["uploads"] = Models.Upload.FindAll();
return this.View("Index");
}

}
}



Method Add() calls Index() after the file upload finished, those way we get the list of the uploads after we upload new file.


The HTML view



Created directory Views\Upload and add there view Index. I won’t select a master page for the purpose of the presentation. I also won’t use strongly-typed view, but just will thread the ViewData as a dictionary.




<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>File uploads</title>
</head>
<body>

<h3>1.List of uploads</h3>
<%
UploadToDb.Models.Upload[] uploads = (UploadToDb.Models.Upload[])this.ViewData["uploads"];
foreach (UploadToDb.Models.Upload upload in uploads.OrderBy(x=> x.Id))
{
%>
<div><%= Html.ActionLink(upload.Id.ToString(), "Get", new { upload.Id })%></div>
<%
}
%>


<h3>2.Upload new file</h3>
<% using (Html.BeginForm("Add", "Upload", FormMethod.Post, new { enctype = "multipart/form-data" }))
{ %>
Choose file: <input type="file" name="file" />
<input type="submit" />
<% } %>


</body>
</html>






Basically we have two parts here.

Part one is a list of all the uploaded files. It creates list of links to open the uploaded files via the "Get" method.

Part two is an html multipart form used to upload new file. After the file upload finished, we invoke again the Index controller method to list all the files uploaded to the database.


To upload file to database run the application and navigate the browser to ~/Upload/ directory.


I've got that screen after two files uploaded:







Enjoy!

Atanas Hristov

Friday, September 4, 2009

Logging with log4net

Log4net is a very powerful tool to get information from running application. After deployment your code has to deal with environment that is more or less different than on your development machine. Log4net not only provides your application with powerful logging where you can specify a lot of options and levels, but you can even dynamically turn on and off logging for different classes while the application is running at the same time.


The homepage of the project is:
http://logging.apache.org/log4net/index.html

I’m about to show you the way I’m usually using log4net in my projects.



Step one



The very first step is to add reference to log4net.dll to the project.



Initialization of log4net



It has to be done once in your application. You can prepare and run static initializer in the manner of:

 
public class Initializer
{
private static object _locked = new object();
protected static bool initialized = false;

public static Initializer() { Init(); }

public static void Init()
{
lock (_locked)
{
if (initialized)
return;

log4net.Config.XmlConfigurator.Configure(); // run once at app startup
...
}
}
}



Then call Initializer.Init() at application start-up from your Global.aspx or Main() for example.




Add logger for one class



One possibility to use log4net is to create separate logger for one class only. Thereof you can:

a/ write to separate and dedicated for that class only log file,
b/ or you can have different formating for logging messages from that class,
c/ or you can set different log level for the log messages from that class,
d/ or just because the class name could be written along with the logging messages so it will be easy to localize the part of the code from where the message comes.

Here is part of declaration of class named "McChannel" for which class we will define class logger.

 

// (1) add using directives:
...
using log4net;
using log4net.Config;

[ActiveRecord("mc_channels")]
public class McChannel : ActiveRecordBase
{
// (2) declare and initialize static per class logger
private static readonly ILog log = LogManager.GetLogger(typeof(McChannel));
...
}




From inside the body of that class you can use the logger "log" like:

 

log.Debug("Selecting channel names at.");
log.DebugFormat("Addiding channel {0}", one.Channel_name_official);








You have log.Debug(), log.Info(), log.Warn(), log.Error(), log.Fatal() methods you use for logging and they all have the corresponding extended with string formatting methods: log.DebugFormat(), log.InfoFormat(), log.WarnFormat(), log.ErrorFormat(), log.FatalFormat(). In the exact priority order as listed here the Debug() has the lowermost priority.

In your configuration (see below) you have to set which level of logging you want to log. The logging levels as they are defined in order of decreasing priority are:

OFF: no logging at all.
FATAL: only log.Fatal() messages will be logged
ERROR: log.Error()and higher will be logged
WARN: log.Warn() and higher will be logged
INFO: log.Info()and higher will be logged
DEBUG: log.Fatal()and higher will be logged
ALL: all logging methods will be logged.



Configuration of log4net in App.config or Web.config



Let's prepare simple configuration for log4net.

First in configSections we add element section with attribute name="log4net".
Then we add sub element "log4net" to element "configuration".

We create appender "RollingFileAppender" which defines where we write logging messages and what the pattern for formatting the log messages is. Furthermore the files will be rotated.

We can have more than one appender in your configuration.

Then we default set priority levels for the whole application to "ERROR" - see element "root" - but we define for class "McChannel" priority level of "DEBUG".

Here is the complete example:


 

<?xml version="1.0"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>
<log4net>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="D:\Logfile.txt" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="100" />
<maximumFileSize value="1024KB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date %-5level %logger - %message%newline" />
</layout>
</appender>
<root>
<!-- ALL DEBUG INFO WARN ERROR FATAL OFF -->
<!-- Setup the root category, add the appenders and set the default level to ERROR -->
<priority value="ERROR" />
<!-- Set the default logger -->
<appender-ref ref="RollingFileAppender" />
</root>
<logger name="McChannel">
<!-- Only for the logger for class McChannel we set level to DEBUG -->
<level value="DEBUG" />
</logger>
</log4net>
</configuration>




The output of logging messages from class McChannel goes to D:\Logfile.txt after we run the application.


this.Dispose()



Log4net is very flexible. You can add the logging messages into a database table, you can use the TraceContext of ASP.NET, the console appender can produce colorized output, you can append to the Application event log in Windows, address Windows Messenger Service, send emails with logging messages and many more.

Have a nice weekend!

Atanas Hristov

Friday, August 7, 2009

ASP.NET Error Logging with ELMAH

Debugging production code is always a challenge. From my experience I have used the Windows event logs and the Apache log4net so far.

Most of the time log4net is the solution for all my needs and I like how flexible it is. But they were situations where the logging solution was not enough. The real problem occurs if in your application you get unhandled exceptions. You know - database connection broken, network downtime, system software updates - it's in many ways possible to throw the application into confusion without having even the near idea where to look for. And this is where ELMAH comes to the rescue.

You can include ELMAH in your projects with a little effort and even with the default configuration you take benefits. ELMAH can log errors for you to different places - from xml files to database servers but the easiest way is to just store error messages into memory. ELMAH will give you a way to check for unhandled exceptions from special URL on your ASP.NET site.

Addiding ELMAH to a project



Having heard for ELMAH from Hanselminutes I decided to give a try.
There was a project under active development where new versions uploaded on the staging server very frequently, even many times a day.

After the download of the binaries I just added a reference to Elmah.dll into the project. Then following the instructions on the ELMAH documentation I made few additions to the Web.config file.

a/ I added to "configSections":


<sectionGroup name="elmah">
<section name="security" requirePermission="false" type="Elmah.SecuritySectionHandler, Elmah" />
<section name="errorLog" requirePermission="false" type="Elmah.ErrorLogSectionHandler, Elmah" />
<section name="errorMail" requirePermission="false" type="Elmah.ErrorMailSectionHandler, Elmah" />
<section name="errorFilter" requirePermission="false" type="Elmah.ErrorFilterSectionHandler, Elmah" />
</sectionGroup>


b/ Then added into "httpHandlers":

<add verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" />


c/ and at the end into "httpModules":

<add name="ErrorLog" type="Elmah.ErrorLogModule, Elmah"/>




I also added another configuration option to enable opening the ELMAH views from remote connection, not only on the server, into section "configuration" I added:


<elmah>
<security allowRemoteAccess="yes" />
</elmah>


That was everything necessary to include ELMAH to the project.



Did I say - ELMAH is wonderful!



Having ELMAH included to the web application with this configuration, we have got the error logging information accessible at http://APPHOME/elmah.axd.

We put into the staging server only well unit-tested code. Once as we did changes on the database schema we have forgotten to patch the database where the staging server connects.

During the integration tests the application raised an exception. On the browser the exception information was not very helpful (see: yellow screen of dead). Into the log files generated by log4net there was nothing mentioned. Then we opened the ELMAH page and we saw the reason for the exception:




ELMAH is a tool to help you with any ASP.NET project. Use it right now.

Monday, August 3, 2009

ASP.NET Globalization

I just finished a project where one of the requirements was to localize completely the input the user gave and the output of values (numbers, date) sent back to the user.

There is a nice way in dot.NET to set current culture information on a thread. You can for example examine the Request.UserLanguages - which is an array of strings and shows you which locales and languages the browser accepted - and then prepare CultureInfo object and assign that object to CurrentThread.

That way you set culture info for every request on the webserver. You use later the culture info information from CurrentThread to parse the user input and to write back localized values to the user.

Set CultureInfo for CurrentThread



The very first step is to set the culture information on CurrentThread on per page request basis. To do so edit function Application_BeginRequest from file Global.aspx.cs.


protected void Application_BeginRequest(object sender, EventArgs e)
{
// you could use Request.UserLanguages
// to set appropriate culture information
System.Globalization.CultureInfo ci = …;
System.Threading.Thread.CurrentThread.CurrentCulture = ci;
}



Set ASP.NET AJAX in your Master Page or View Page



Edit file Site.Master (or the MasterPage or ViewPage where the ScriptManager is.
Set EnableScriptGlobalization attribute to "true". Insert bug-fix for Number.parseLocal


<form id="frm" runat="server">
<asp:ScriptManager
ID="ScriptManager1"
runat="server"
EnableScriptGlobalization="true">
<Services>
<asp:ServiceReference Path="~/WebServices/Service.asmx" />
...
</Services>
</asp:ScriptManager>

<script type="text/javascript">
Number.parseLocaleFixed = function(value) {
return Number.parseLocale(value.replace(' ', ' '));
}
</script>



Parse User Input in ASP.NET Ajax


To parse user input of numbers use Number.parseLocaleFixed() javascript function. To parse user input of date use Date.parseLocale() javascript function.
Example:


Var numb = Number.parseLocaleFixed(
document.getELementById(‘txtNumb’).value);



Parse user input in ASPX page



Use the Parse() functions of the expected type. Specify the culture info in Parse(). Take the culture info from CurrentThread.


DateTime dt = DateTime.Parse(s,
System.Threading.Thread
.CurrentThread.CurrentCulture);




Write localized Information to the page



To write localized values to the page use the culture information from CurrentThread:


<%= dt.ToString(“d”, System.Threading.Thread
.CurrentThread.CurrentCulture) %>


To write localized pattern for validation of date input field you can use something like:



CultureInfo ci = System.Threading.Thread
.CurrentThread.CurrentCulture;

// validation of date input
string dSeparator = ci.DateTimeFormat.DateSeparator;
if (dSeparator == ".") dSeparator = @"\.";
string dPatternExpr = String.Format(@"^(|[0-9]{{1,4}}{0}[0-9]{{1,4}}{0}[0-9]{{1,4}})$",
dSeparator);

valSigningDate.ValidationExpression = dPatternExpr;

// validation of number input
string nDecimalSeparator = ci.NumberFormat.CurrencyDecimalSeparator;
if (nDecimalSeparator == ".") nDecimalSeparator = @"\.";
string nPatternExpr = String.Format(@"^[-+]?[0-9]*{0}?[0-9]*$",
nDecimalSeparator);

valSinglePayment.ValidationExpression = nPatternExpr;



Atanas Hristov

Saturday, August 1, 2009

Using Entity Framework with SQLite - Installation

SQLite is a simple but powerful open source database engine which you can use among everything else at development time and later connect your application to database server at run time.

Tools I have found to be pretty helpful to administer SQLite databases are: SQLite Expert Personal and sqliteadmin.

To use SQLite with the Entity Framework you need ADO.NET Provider. A complete provider is System.Data.SQLite.

Download file SQLite-1.0.65.0-setup.exe and install to appropriate location. I used directory C:\Programme\SQLite.NET as installation directory. In subdirectory bin into the installation directory you'll find two libraries - "System.Data.SQLite.Linq.dll", "System.Data.SQLite.dll" - which you optionally can register into the global assembly cache (gacutil -I ). To connect the Entity Framework to SQLite database you'll need to add references to these two assemblies.

Now, there is a nice dot.NET project on MSDN - EFQuerySamples - which demonstrates the Entity Framework with the well known Northwind database. You can use that application to learn the syntax of the Entity Framework queries and even to see the generated SQL. Download the project and unzip the file.

To get the EFQuerySamples running against SQLite database, there is a supplement to the MSDN project - EFQuerySamples_SQLite. You need to copy from this over the original project the following files.

Having the two projects unzipped next to each other copy:


file EFQuerySamples_SQLite\App.config to EFQuerySamples\App.config
file EFQuerySamples_SQLite\Entity Framework Query Samples.csproj to EFQuerySamples\Entity Framework Query Samples.csproj
file EFQuerySamples_SQLite\DB\northwindEF.db to EFQuerySamples\DB\northwindEF.db
file EFQuerySamples_SQLite\Schemas\NorthwindEFModel.SQLite.ssdl to EFQuerySamples\Schemas\NorthwindEFModel.SQLite.ssdl


Open the Visual Studio solution from EFQuerySamples.

Add references to the assemblies System.Data.SQLite and System.Data.SQLite.Linq.

Open file App.config and add new DbProviderFactory:




<system.data>
<DbProviderFactories>
<remove invariant="System.Data.SQLite"/>
<add name="SQLite Data Provider" invariant="System.Data.SQLite"
description=".Net Framework Data Provider for SQLite"
type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite, Version=1.0.65.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139"/>
</DbProviderFactories>
</system.data>



Take care of the Version in attribute type, make sure equals the version of the System.Data.SQLite.dll you downloaded.

Also in App.config there should be addition to section connectionStrings coming with the supplement project:



<connectionStrings>
<add name="NorthwindEF (SQLite)" connectionString="provider=System.Data.SQLite;metadata=Schemas\NorthwindEFModel.csdl|Schemas\NorthwindEFModel.msl|Schemas\NorthwindEFModel.SQLite.ssdl;Provider Connection String='Data Source=DB\northwindEF.db'"
providerName="System.Data.EntityClient" />


...



Now you can run the EFQuerySamples application, choose the SQLite connection and run the various examples.

Enjoy!

Atanas Hristov