Sunday, September 27, 2009

How to change event hanlders at run time

One of the challenges when using event handlers at run time , in my opinion , is that we can't manage previously registered event handlers very easily . I mean we don't know who else is listening to the same event.

Suppose we have two combo boxes and a list box.We want to let the user select a category in first combo box and select an object in second combo box and see a list of related information of that object in the list box.

User selects Music category from category combo box.
User selects "Abby Road" from objects comb box.
A list of album information is shown to him.

Here's the source:

private void CategorySelected(object sender,EventArgs e)
{
if(
((Categroy)_categoryComboBox.SelectedItem).Type==
CategroyType.Music
)
{
_itemComboBox.DataSource=_application.GetMusicAlbums();
}
}

private void ItemSelected(object sender,EventArgs e)
{

if(_itemComboBox.SelectedItem is MusicAlbum)
{
_list.DataSource=
_application.
GetMusicAlbumAdditionalInfo
((MusicAlbum)_itemComboBox.SelectedItem);
}
}


Well it's working but it's not very nice. As you can see we should write a lot of "if" to handle different kind of categories and objects.
OK we could change the event handler instead of using if.
Suppose that we have registered some event handlers in a dictionary then we can write :

_itemComboBox.SelectedItemChanged+=
_eventhandlersRegistry[selectedCategory.Type];



But we have a little problem : what if there's already a category selected ? so we should first remove last SelectedItemChanged event handler then we have to write:

_itemComboBox.SelectedItemChanged-=
_lastSelectedItemChangedEventHandler
_itemComboBox.SelectedItemChanged+=
_eventhandlersRegistry[selectedCategory.Type];
_lastSelectedItemChangedEventHandler=
_eventhandlersRegistry[selectedCategory.Type];


To solve this kind of scenario , I recently use a solution that I'd like to share with you:
I use a nested EventHandler delegate therefore there's no need to change the event handler of actual event .Here's what I do:
I have an event handler delegate declared as :

private EventHandler _nestedSelectedItemChangedEventHandler



Then I have this general event handler for the actual event :

_itemComboBox.SeletedItemChanged+=
GeneralItemChangedEventHandler;



When the category is changed I place a new delegate in my nested event handler like this:

_nestedSelectedItemChangedEventHandler=
_registeredEventHandlers[_selectedCategory.Type];


and GeneralItemChangedEventHandler will call nested event handler as follows:

private void GeneralItemChangedEventHandler (object sender,EventArgs e)
{
if(_nestedSelectedItemChangedEventHandler!=null)
_nestedSelectedItemChangedEventHandler(sender,e);
}



This approach can be used if you are implementing a strategy pattern so that you want to change the behavior by changing the state of your object and there's always a single event handler involved to handle an event.

Monday, September 21, 2009

Using generic methods to avoid boxing-unboxing

Every time we convert a value type (int,decimal,double,etc) to a reference type (to an object to be specific) a boxing occurs (As MSDN define it , it means to put a value type inside heap) and if we convert an object to an int , an unboxing occurs. Here's an article on MSDN that describes it all.
I'm working on this method that gets a DataRow ,a FieldName and a default value and returns row[fieldname] value (if value is null or DBNull it returns the default value)
My first vesrion of this method is something like this:

public static string GetFieldValue
(DataRow row,
string
fieldname,
string defaultvalue)
{
var value=row[fieldname];
if(value==null || value==DBNull.Value)

return defaultvalue;
return value;
}



Later ,I want to write an overload for this method to work with int values I write something like this:

public static int GetFieldValue
(DataRow row,
string fieldname,
int defaultvalue)
{
var value=row[fieldname];
if(value==null || value==DBNull.Value)

return defaultvalue;
return (int)value;
}



OK an unboxing is happening here and I can't do anything about it.
I think to myself here we have a sign of DRY violation .I can have a single method as a base,something like this:

public static object GetFieldValue
(DataRow row,
string
fieldname,
object fieldvalue)
{
var value=row[fieldname];
if(value==null || value==DBNull.Value)
return defaultvalue;
return value;
}
Then I can rewrite the int version like this:

public static int GetFieldValue
(DataRow row,
string fieldname,
int defaultvalue)
{
return (int)GetFieldValue(row,fieldname,(object) defaultvalue);
}
But wait a second, I can see a boxing here which is not necessary but how can I get rid of it?
One solution that comes to my mind is to change my base method to a generic method:

public static
T GetFieldValue<T>
(DataRow row,
string fieldname,
T defaultValue)
{
var value=row[fieldname];
if(value==null || value==DBNull.Value)
return defaultvalue;
return (T)value;
}



Again if T is a value type an unboxing will occur but there's no boxing and I can rewrite the int version as :

public static int GetFieldValue
(DataRow row,
string fieldname,
int defaultvalue)
{
return GetFieldValue<int>(row,fieldname, defaultvalue);
}

I wrote a test that ran each method for 1,000,000 times and it turns out that the omitted boxing impact is very small.(418 ms for object version vs. 358 ms for generic version)

Saturday, September 19, 2009

A Custom linq query builder

In my opinion one the most interesting features of Linq is Expression Trees.They let us build structured queries over IQueryable type.
IQueryable ,as its name implies, is an interface that let us build a query over a provided object of type T.Although IQueryable and expression trees are mostly used in Linq to sql but since every IEnumerable can be converted to an IQueryable object (using System.Linq.AsQueryable method) these great tools are available to every kind of Linq (linq to Xml and linq to objects for example).
When we build an expression tree over an IQueryable object , the expression itself won't be executed until we really want to get the data.
For example if we write the following code:
(DataSource is an IQueryable object)

var result=DataSource.Where(post=>post.Title.IndexOf("Linq")>0);




result is yet another IQueryable that you can query again :

result=result.Where(post=>post.CreationDate>new DateTime(2009,1,1));




Now if we've added all we wanted to our query we can execute it either by converting the result to an ICollection (a List or an array for example) or by executing GetEnumrator method ( usually by using a for each loop).

Based on this introduction I designed a custom query builder that facilitate querying an object using collected search arguments (in a windows /web form for example)

Here's the scenario that I had in mind :
There's a web/win form that lets a user fill-in some information about what he wants to see.
Consider a blog application , a user can search post based on their titles and/or their creation date.Therefore a QueryPostArgs object is created and populated using the information provided by user.
Then this object is passed to a QueryBuilder to build a query from it.Query builder contains some register query methods and when is asked to build the query it will use them to build the final query.
Enough talking let write some code :

To collect and transfer the arguments information we have an AbstractQueryArgs class.

public abstract class AbstractQueryArgs<t> where T :class
{
}




To build queries over a post object we then derived from it and create a PostQueryArgs class.

public class PostQueryArgs : AbstractQueryArgs<post>
{
public PostQueryArgs()
{
CreationDateFrom = DateTime.MinValue;
CreationDateTo = DateTime.MaxValue;
}
public string Title { get; set; }
public DateTime CreationDateFrom { get; set; }
public DateTime CreationDateTo { get; set; }
public string[] Tags { get; set; }
}



To build query using an AbstractQueryArgs we have an AbstractQueryBuilder like this:

public abstract class AbstractQueryBuilder<t,tsearchargs> 
where
T :class where TSearchArgs :AbstractQueryArgs<t>
{
protected delegate IQueryable<t>
QueryMethodDelegate
(IQueryable<t> queryable,TSearchArgs searchArgs);

public IQueryable<t> DataSource { get; private set; }
private readonly List<QueryMethodDelegate>
_queryMethodDelegates = new List<QueryMethodDelegate>();


protected AbstractQueryBuilder(IQueryable<t> dataSource)
{
DataSource = dataSource;
}


protected void AddQueryMethod(QueryMethodDelegate queryMethodDelegate)
{
_queryMethodDelegates.Add(queryMethodDelegate);
}
public IQueryable<t> Build(TSearchArgs searchArgs)
{
var queryable = DataSource
foreach (var queryMethodDelegate in _queryMethodDelegates)
{
queryable=queryMethodDelegate(queryable,searchArgs);
}

return queryable;
}
}



It allow us to build custom query builders for any T class.As you can see it gets an
IQueryable as its data source via constructor.Then we can add some query methods to a list of query methods . A query method is any method that gets an IQueryable and a query arguments object and returns an IQueryable (It somehow a visitor method that visits an IQueryable object and do something on it based on provided query arguments and return it back ) To define a query method a protected delegate is defined.Every class that is derived from this abstract class can add its custom query methods to the query methods list using AddQueryMethod protected method.
Finally there's this Build method that build an IQueryable object using the provided datasource and passing it to each query method.As you can see this method is also returning an IQueryable object that lets us to perform any further queries over it if necessary.

Now we can create our custom query builder for our post object as follows:

public class PostQueryBuilder:AbstractQueryBuilder<post,postqueryargs>
{
public PostQueryBuilder(IQueryable<post> dataSource) : base(dataSource)
{
AddQueryMethod(AddTitleExpression);
AddQueryMethod(AddCreationDateExpression);
AddQueryMethod(ContainsAllTags);
}

private static IQueryable<post>
ContainsAllTags(IQueryable<post> queryable , PostQueryArgs queryArgs)
{
if (queryArgs.Tags == null || queryArgs.Tags.Length == 0) return queryable;
return queryable.Where
(x => x.Tags.Intersect(queryArgs.Tags).Count() == queryArgs.Tags.Length);
}

private static IQueryable<post>
AddTitleExpression(IQueryable<post> queryable,PostQueryArgs queryArgs)
{
return string.IsNullOrEmpty(queryArgs.Title) ? queryable :
queryable.Where(x=>x.Title.IndexOf(queryArgs.Title)!=0);
}

private static IQueryable<post>
AddCreationDateExpression(IQueryable<post> queryable,
PostQueryArgs queryArgs)
{

return queryable.Where(
x => x.CreationDate >= queryArgs.CreationDateFrom &&
x.CreationDate <= queryArgs.CreationDateTo); }


As you can see this class add three query methods to query methods list.One for title,one for creation date and one for tags.

Now that we have created these classes , all we need to do to build a query on a post datasource is like this:

var posts=new []{_post1,_post2,_post3};
var queryBuilder=new PostQueryBuilder(posts);

var queryArgs=new PostQueryArgs{Title="Intro",Tags=new []{"C#","Linq"}};
var query=queryBuilder.Buid(queryArgs);




Then we have a IQeryable object that when we try to enumerate it , post objects with "Intro" in their title and containing "C#" and "Linq" tags will be returned.

P.S. I have upload the source code in codeproject site you can fint it here.

Monday, September 14, 2009

Intercepting NHibernate

One of the greatest things about NHibernate ,that I like so much, is that you can customize it very easily.
Most of the times you can work with POCO but if you want to do some custom stuffs when an object is going to be worked upon via NHibernate you can implement NHibernate.ILifeCycle (NHibernate.Classic.ILifeCycle in NHibernate 2) interface that has some OnXXX methods that let you add your functionality when something is going to happen.Using this mechanism you can customize the persistence behavior of individual objects but what if you have a pattern that applies to large amount of objects in your application?

To make it clear let's consider this scenario :
You want to add a feature to a set of implemented objects to keep who has created them and who changed them for the last time.
Since there might be so many places where these common information should be set in the code it could be a hard task to change the code.Here's where NHibernate.IInterceptor comes to play.IInterceptor provides some methods that let you intercept session actions.
Enough talking let's write some code for above scenario:

First I create an IUserActivity interface :

public interface IUserActivity
{
User CreatedBy{get;set}
User LastUpdatedBy{get;set;}
}



Now all the objects that want to save their user activity should implement this interface (and these properties should be defined in database and in mapping files)

Then I create an Interceptor that implements NHibernate.IInterceptor :
(Since I'm going to change the state of objects before saving/updating them I only need to implement OnSave method and to notify NHibernate that other methods are not implemented simply we return the method default value of its return type.)

public class NHInterceptor:IInterceptor
{
.....

public void OnSave(object entity,object id,object[] state,
string
[] propertyNames,IType[] types )
{
var userActivity=entity as IUserActivity;

if(userActivity==null) return;
//I use an IoC container to get the AuthenticationProvider that
//provides user information.

var authenticationProvider=
ObjectRepository.Resolve<AutenticationProvider>();
if(userActivity.CreatedBy==null) // this object is not created yet

{
userActivity.CreatedBy=authenticationProvider.CurrentUser;
}
userActivity.LastUpdatedBy=authenticationProvider.CurrentUser;
}
.....
}


As you can see you can easily change the state of your objects before saving them using an interceptor.

Other arguments of OnSave methods hold new information for entity.State array has the values ,propertyNames array has the name of properties and types array has types of properties.

To intercept a session simply open a session providing an interceptor:

var session=SessionFactory.OpenSession(new NHInterceptor());

Try Finally Pattern

This kind of coding style seems to be very common:

public void DoSomething(string filename)
{
var file=File.OpenText(filename);
if(! some_condition)
{
file.Close();
file.Dispose();
return;
}
//Do some other stuff
file.Close();
file.Dispose();
}
The problem here (as you can see) is that we are repeating some lines that
should be executed either everything is ok or if something is wrong.
(A violation of DRY)

We can refactor this code like this:
public void DoSomething(string filename)
{
try
{
var file=File.OpenText(filename);
if(! some_condition)
{
return;
}
//Do some other stuff

}
finally
{
file.Close();
file.Dispose();
}
}

Now no matter what will happen in our try block , finally block
will always run.

In this particular case since we are using an IDisposable class
(FileStream)we can make our code even more readable and
maintainable like this:

public void DoSomething(string filename)
{

using(var file=File.OpenText(filename))
{
try
{
if(! some_condition) return;
//Do some other stuff

}
finally
{
if(file!=null)file.Close();
}
}

}



Sunday, September 13, 2009

Permission Granted

If you are working on a real world project chances are so high that authentication (who can access the system) and authorization (what he has permission to do)are among its non-functional requirements.
Authentication is somehow straight forward . There are a lot of approaches and patterns we can stick to but when it comes to authorization it's not that simple. The problem is people can have permission to perform an action globally or to perform it on some objects so whenever we want to check if somebody can do something we should check 1.the action and if necessary 2.the object that is acted upon.
Checking the action itself can be done by hard coding the action names in regarding methods so that every time a method is called it says if you want to run me you should have a XXXX permission but checking objects can't be hard coded and should be retrieved in run time therefore we should have a way to tell our program where to find the object.

Here's a solution that we are using on our projects and I'd like to share it with you:
Database:
We keep these information in a table:
  • Source (Who granted this permission)
  • Target(To whom this permission is granted)
  • Action(What action is granted)
  • Context(the action is granted on this context ,it can be a global definition or a type of an object or id of an object)
  • Type (if it's granted or denied )
Domain Model:
in our domain model we have this PermissionAttribute to mark a method so that whenever we want to run it ,its permission should be checked.

for example if there a AddFolder method and user should have a AddFolder permission we write it as follows:

[Permission(Action="AddFolder",Context="Folder")]
public void AddFolder(Folder folder)
{

}
Then we know that every user who wants to run this method should have an "AddFolder" permission on Folder context but what if we want to check if he can add a folder to a specific folder here's what we do :
[Permission(Action="AddFolder",Context="Folder")]
[Permission(Action="AddFolder"
,ContextLookup="folder.ParentFolder.Id")]
public void AddFolder(Folder folder)
{

}

It says that not only user should have a permission to AddFolder generally but also
should have a AddFolder permission on ParentFolder.

How it works :
Finally we have this security checker interceptor in place that runs before a method
is called and check to see if there's a permissoin attribute has set and
if there's a ContextLookup is available it gets object information query over
granted permissions and throws an exception if permission is not granted or is denied.

Well that's all folks :)

Friday, September 4, 2009

Playing with fluent interfaces

A while ago I was reviewing one of our projects code and I noticed that it was very complicated and I had a hard time understanding what was going on and I was one of the senior developers of the team.I thought to myself maybe we could have a more readable approach in coding so that it would be easier to understand what is the intention of the code.

I think that using DSLs can help us to improve readability of our codes.As Martin Fowler teaches us in his brilliant book about DSL there are mainly two kind of DSLs internal and external and there's this concept of "fluent interface" that let us implement a kind of internal DSL.

Since I'm working with C# 3.0 and as far as I know C# is not a DSL friendly language I think using this fluent interface concept can be a good candidate to improve our code and have some useful DSLs in place.But how?

Suppose that we have a Login method in our domain that lets us to authenticate a user using a login name and a password,Something like this:

UsersDomain.Login(username,password);

Actually every method like this is an action/command executing on a specific domain.Then we can have our domain specific language as follows:

On Domain Do Action With Patameters

And here's a C# example of the same:

userDomain.Login().With((action)=>{action.Username=username;
action.Password=password;}).Do();

I know this seems very verbose in comparison with our traditional version but if we have thousands lines of code combining different domains and logic declaring everything in the latter way should help us understand the code better.

Let me show you a better example:

Suppose we have a mailing application that lets a client to send an email and here's our traditional approach to implement it:

var mail=new Mail();
mail.From=fromAddress;
mail.To=toAddress;
mail.Header=header;
mail.Body=body;
mail.Send();

And here's the same using a fluent interface:
mail.Send().From(fromAddress).To(toAddress).Header(header).Body(body).Do();

I would like to know what you think of this approach.Please let me know