Wednesday, May 9, 2007

Singleton: Ammendment 1

An excellent issue was raised by Farzad in his comment to my previous post (Singleton) regarding the fact that when you have a multi-instanced singleton and the singleton is going to decide based on the current thread what object to return it's better to store it in the thread using SetData & GetData.
I absolutely agree with Farzad since this will save us from the hassel of clean up issues. Consider the example of the multi-instanced connection manager:

public class ConnectionManager
{
/* ConnectionManager implementation */

//Singleton logic:
private ConnectionManager() { }

private static Dictionary _instances = new Dictionary();

public static ConnectionManager Instance
{
get
{
lock (_instances)
{
if (_instances.ContainsKey(System.Threading.Thread.CurrentThread) == false)
_instances.Add(System.Threading.Thread.CurrentThread,new ConnectionManager());
}
return _instances[System.Threading.Thread.CurrentThread];
}
}
}


One problem with the above code that you would have to resolve is what happens when a thread is terminated? The singleton will still contain a reference to the ConnectionManager associated with the thread and over time this could cause a memory leak. Now in my previous post this was only supposed to be an example, but in real life you should take this stuff into account.
There are multiple ways to solve this. In some systems you wouldn't have a problem because a very limited number of threads will be created and maintained throughout the lifetime of the system. You could also use weak references that will automatically get cleaned up when the garbage collector is doing a round of clean up.
But a very elegant solution would be using the Thread.SetData & GetData methods. So the result would look like this:

public class ConnectionManager
{
/* ConnectionManager implementation */

//Singleton logic:
private ConnectionManager() { }

public static ConnectionManager Instance
{
get
{
if (Thread.GetData(Thread.GetNamedDataSlot("ConnectionManager")) == null)
Thread.SetData(Thread.GetNamedDataSlot("ConnectionManager"), new ConnectionManager());

return (ConnectionManager)System.Threading.Thread.GetData(Thread.GetNamedDataSlot("ConnectionManager"));
}
}
}

As can be seen in the above code we aren't using an explicit data structure to store our ConnectionManager objects but we are using a place that the thread will provide for storing data specific to this thread. The other interesting bit about the above code is there is no need for locking due to the fact that the SetData & GetData function are operating on the current thread's data and there is no way two threads can access one thread's data at the same time :)

Monday, May 7, 2007

Singleton

As I promised the first pattern that I'm going to write about is Singleton. Now I know that this might be the simplest and the most well known pattern ever existed but I think there are a lot of points that haven't been collected in one location so I'm going to focus on that stuff. Let me re-iterate that I'm not going to get into in depth discussion of what is Singleton but to more advanced topics and discussions, but to start off we need an introduction so here goes:

In software design there are a lot of situations where we need only one instance of an object to exist or a limited control number of objects to exist (of a specific class). This might be due to resource issues or just the simple fact that it doesn't make sense to have more than one object of a specific class. Many consider the singleton pattern an excellent solution to common coupling (global data). This pattern provides a very easy solution to solve the problems of common coupling and at the same time have the ease of use of global data.

OK so let's take a look at a very simple singleton. Let's assume that we have a PrintManager class that we can call its Print method and pass a document to it for printing. Now this class should queue all the documents that it receives and print them in order. For the sake of simplicity let's assume we have only one printer to print to and therefore only one queue. The singleton pattern says that if you want to make a class a singleton write your logic as if you were going to have multiple instances of it (as a regular class) and then perform the following steps on it:

  1. Make the constructor private so no one can create an instance.
  2. Create a private class variable (static) of your class.
  3. Create a public class method (static) to access the private variable and instantiate it on demand.

For example:


public class PrintManager
{
//The following code is regular implementation not part of
//the singleton pattern
private Queue PrintQue; //Sample member variable

public void Print(Document doc)
{
//code that would take care of queing
//and other boring stuff we don't care about
}


//The singleton pattern implementation:
private PrintManager()
{
}

private static PrintManager _instance = null;

public static PrintManager getInstance()
{
if (_instance == null)
_instance = new PrintManager();

return _instance;
}
}

Obviously the client code needing to use the single instance of the above class will access it through code similar to this:

PrintManager.getInstance().Print(myDoc);

This way no one can create a new instance or remove the one instance that exists, but it's also available for everyone's use. It is also created on first use of the PrintManager.


Items to discuss:

1) Almost all implementations of the singleton pattern should safe guard against a race condition in multi-threaded environments. So the correct implementation of the above code would be:


public class PrintManager
{
/* Ommited for simplicity */

//The singleton pattern implementation:
private PrintManager()
{
}

private static PrintManager _instance = null;
private static object LockObject = typeof(PrintManager);

public static PrintManager getInstance()
{
lock(LockObject)
{
if (_instance == null)
_instance = new PrintManager();
}

return _instance;
}
}

The above code will make sure that no two threads can accidentally enter the getInstance method at the same time and create two instances of the PrintManager object, one overwriting the other.


2) Using C# syntax and relying on a couple of .net CLR features we can rewrite the above code like this:


public class PrintManager
{
/* Ommited for simplicity */

//The singleton pattern implementation:
private PrintManager()
{
}

private static PrintManager _instance = new PrintManager();

public static PrintManager Instance
{
get
{
return _instance;
}
}
}

And the client code would look like this:

PrintManager.Instance.Print(myDoc);

This is a lot nicer and more readable than the previous code. Also notice that since we are relying on the CLR's static member initialization routines we don't need to worry about a race condition or similar multi threading issues.


3) Another situation that exists is a singleton that might have multiple controlled objects (instead of only one object). To keep with the above example we might want to implement our PrintManager so that it can manage multiple printers. Each printer would have a name and would work completely separately from other printers (it will have its own separate queue etc.). One solution that might immediately pop into mind is changing the Print method to accept two parameters a printer name and a document. But this means changing the logic of our code, a logic that might be working fine and we only needed to extend it. Obviously a bad solution and if you really want to know why go look up functional cohesion. Yes if we mix the code to separate different printers & queues with the printing logic we have basically downgraded our design from functional cohesion level to a lower less cohesive level (in this design it looks like a downgrade to logical cohesion; very badJ).
OK to solve this we need a multi-instance singleton

The Multi-Instanced Singleton:

This variation of the singleton pattern which can be implemented in many different ways is basically a singleton that has more than one instance of its class, but each instance is created and controlled by the singleton. For our current example assuming that each printer would be differentiated using a string name our multi-instance singleton would look like this:


public class PrintManager
{
/*Same logic as before (no changes needed) */

//The singleton pattern implementation:
private PrintManager()
{
}

//We need a method to keep multiple instances of the PrintManager class:
private static Dictionary _instances = new Dictionary();

public static PrintManager GetInstance(string printerName)
{
lock (_instances)
{
if (_instances.ContainsKey(printerName) == false)
_instances.Add(printerName, new PrintManager());
}

return _instances[printerName];
}
}

In all variations of the multi-instanced singleton we always face two design decisions:

  • How am I going to store the multiple objects (the data structure needed)?
  • How am I going to differentiate between the different instances?

The first question is usually easy to answer we might need a list or a dictionary or similar data structures. In rare cases we might not even need to store the different instances directly and they would get stored in some other sort of runtime available structure (discussed later).

The second question is a lot more interesting. The above example is a perfect example where the client code will decide which instance it needs to access and the singleton will check if it has that instance, if it does it's returned otherwise it's created (or loaded) and then returned.

Another widely used variety of the pattern is when the singleton class itself can decide which instance to return (the client code just uses the Instance property oblivious to which instance is actually returned. In these cases there has got to be some external way of figuring out which instance to return. As an example suppose we are writing an MDI document processing application. When the user clicks on the Tools menu and selects "Spell Check" we want to invoke the spell check routine passing it the current active document. Now suppose we have implemented Document class as a singleton. When accessing this Document singleton the client code doesn't need to tell it which document all it needs to do is say Document.Instance (whatever object is returned will be the active document).

Another perfectly useful example of this second variation is when you are developing objects that should only exist one per thread. The calling client doesn't need to tell the singleton which object it requires, all that is needed is to ask for the Instance and the active instance will be decided based on the calling thread. See below:


public class ConnectionManager
{
/* ConnectionManager implementation */

//Singleton logic:
private ConnectionManager()
{
}

private static Dictionary _instances = new Dictionary();

public static ConnectionManager Instance
{
get
{
lock (_instances)
{
if (_instances.ContainsKey(System.Threading.Thread.CurrentThread) == false)
_instances.Add(System.Threading.Thread.CurrentThread, new ConnectionManager());
}

return _instances[System.Threading.Thread.CurrentThread];
}
}
}

4) Other situations exist that we need a singleton object but this singleton object might be implemented in different ways or need to use mechanisms such as polymorphism to allow different implementation of the singleton. As an example suppose that we were implementing the above ConnectionManager in a single instance singleton but we needed to provide multiple implementations of it. For example an implementation to work with a SQL Server DB and another to work with an Oracle DB. These implementations would differ a lot in the actual logic of the class but would require the same singleton logic and the same access point for clients. In other words we want our clients to be able to say:

ConnectionManager.Instance.OpenConnection();

regardless of whether the SQL Server DB is configured for use or the Oracle version is going to be used. To achieve this we would need a mechanism to decide which version of our ConnectionManager is going to be used at instantiation but that is irrelevant in this example and for the sake of simplicity I'm going to assume that we will use reflection to create the currently configured version of our ConnectionManager and start using it as the only instance available. Please see the following piece of code:

The Polymorphic Singleton:


public abstract class ConnectionManager
{
//Abstract methods that need to be implemented by concrete ConnectionManagers:
public abstract void OpenConnection();
public abstract void CloseConnection();
// .
// .
// .

//Singleton logic:

//Note that in this version the constructor should be protected
protected ConnectionManager()
{
}

private static ConnectionManager _instance = CreateInstance();

public static ConnectionManager Instance
{
get
{
return _instance;
}
}

private static ConnectionManager CreateInstance()
{
//let's assume the assembly name and class name of the currently
//active ConnectionManager is stored in the .config file
Assembly asm = Assembly.Load(ConfigurationManager.AppSettings["Assembly"]);
return (ConnectionManager)asm.CreateInstance(ConfigurationManager.AppSettings["CMFullName"]);
}
}

public class SQLConnectionManager : ConnectionManager
{
public SQLConnectionManager() : base()
{
}

public override void OpenConnection()
{
//SQL implementation
}

public override void CloseConnection()
{
//SQL implementation
}
}

public class OracleConnectionManager : ConnectionManager
{
public OracleConnectionManager() : base()
{
}

public override void OpenConnection()
{
//Oracle implementation
}

public override void CloseConnection()
{
//Oracle implementation
}
}

As is obvious in the above implementation the constructor of our singleton class must be protected (otherwise no one can inherit from it) and the sub-classes need to have a public constructor so that the singleton CreateInstance method can create them on demand.

5) Finally you might even think of a situation where we need a multi-instanced polymorphic singleton as the final type of singleton.


Singletons in Web Apps

A question that I have been asked time and again is what happens in web based applications. Do we still need the singleton pattern there? Can't we just use the Application/Session objects?

YES you can and many people do and feel that it's a lot easier dealing with Application/Session than with a singleton, but I'd like to make these couple of points regarding web based scenarios:

  1. In many cases you might be developing a library or a reusable piece of code that will be run in multiple environments. You might need to use it in a web based scenario and a windows based scenario and other similar situations. For these kinds of reusable parts using the Application/Session will tightly couple them with the web-based environment and will prevent their reuse in non-web based environments. To get over this we can implement our singleton using the methods discussed above or implement a polymorphic singleton that depending on the environment that it's in will choose a web based or a windows based implementation.
  2. Another big question in web based scenarios is what are you using the singleton for? Are you using it to store user specific information, in other words a multi-instanced singleton that has an instance per user storing that users' information or are you designing a singleton (or multi-instanced one) that has no relation to the user. For the former you have to use the Session object otherwise in server farm scenarios you would have to implement a lot of code to replicate your singleton's data across all servers. But for the latter situation a regular singleton should suffice.
  3. Speed & performance might be another reason you would pick a regular singleton over the Session/Application data. Session data (especially in a server farm environment) could be really slow (object serialization, transfer to state server, storage there and the reverse process). Again if you don't need to replicate that data across all servers in the farm why use a Session object.
  4. As a general best practice it's better to wrap all your Session/Application needs in a class similar to a singleton implementation so you don't have to deal with strings in accessing Session/Application data and you have strongly typed variables (preventing many runtime time errors). The wrapper you would create around the session/application object can be a singleton in itself.

Other patterns:

Many patterns can be combined with the singleton object to solve more complex problems and I will try to touch on them in the next posts, but maybe one of the most famous ones is the Abstract Factory pattern. This pattern in most cases gets combined with a polymorphic singleton and we'll get into this on the next design pattern post I'm going to get into.

SharePoint Cross List Queries not upto the task

I've been using SharePoint Cross List Queries in multiple places in a recent site development project and I've noticed that no matter how you use them with indexed columns or without indexed columns or any other settings these monsters consume huge amounts of memory and are extermely slow. So unless you have a small number of lists and not a lot of items in each list you shouldn't be using them.
In a recent project I was using SharePoint queries to fetch latest posts based on a selection & filtering algorithm from SharePoint forums. Now we had a huge number of forums each located in different sub sites and we needed to run a query where we could get the latest post that matched a specific criteria. Once the system grew to a huge amount of data we realized that the Cross List queries were running very slow and at one point we started getting "Server Out of Memory" exceptions. So to cut a long story short we eventually had to create SharePoint events put them on list adding, updating & deleting events and replicate the fields needed in a SQL table and then run our query on the table.
My personal on why SharePoint Cross List Queries suck in performance: I think when Microsoft developed this feature they only had small lists or a small number of lists in mind plus they also had to make sure that this feature was compatible with other SharePoint features (like list item level security) so they couldn't just translate the CAML in cross list query to a SQL statement and run it on the DB even when you have configured all the columns in your query as indexed columns. So what happens is SharePoint loads all the data related to those lists into memory, sorts them, tries to filter them and then you get a Server out of memory exception on large amounts of data.

Friday, May 4, 2007

Excellent Blog

Hi everyone,

If you know how to read Farsi this is an excellent blog by a knowledgeable and wise software engineer and a very good friend of mine:

Owner: Yusef Mehrdad
Blog Address: http://somamos.blogfa.com

Wednesday, May 2, 2007

Design Patterns Pros & Cons

Well I guess the Gang of Four (GoF) design patterns (or Gamma's design patterns as many like to call it) are probably the most renown set of patterns available (and used) in software engineering (if you want a good explanation of what a design pattern is please refer to http://en.wikipedia.org/wiki/Design_pattern_(computer_science), to the book Design Patterns: Elements of Reusable Object-Oriented Software by Gamma et al or do a search for 'design patterns' you can't miss it!J). We've all read many articles or heard a lot of stuff in regards to the pros & cons of using design patterns, and although anyone who knows me professionally would agree that I'm a pro-design pattern kind of person, I do believe that the issues raised against design patterns are extremely interesting and enlightening.

A lot of articles I've read that try to undermine design patterns usefulness, although they are a bit unfair, do have some valid points. One of these points that I found very interesting was the fact that many believe that design patterns are taking us back to a pre-reuse era where everything had to be re-coded. The argument looks valid when you look at design patterns from a distance: design patterns are ultimately providing you a solution and a couple of examples, you have to understand (or misunderstandJ) the solution and using the examples as help try to implement it, and solve your own problem.

On closer examination, especially if you have used design patterns in real life, you realize that design patterns are supposed to provide you with a different type of reuse. A type of reuse that I would like to call "concept reuse" or "idea reuse." Design patterns try to provide a structured way of thinking and organizing a solution so that people with less experience with that domain or problem can soak up the experience of the more adept designer/developer and hopefully save a whole lot of time, agony and trial & error in the process. That being said we can't ignore the fact that design patterns in their purest form will not give you a ".dll" or ".class" or any other similar re-usable binary code that you can plug into your program and start using it. But the point behind design patterns is that if they did provide that kind of plugable/reusable solution we would have not had the whole idea/concept type of reuse. Let's delve into this a bit further:

Software reuse has been a major driving force behind a lot of innovations in the industry. From the very early days of software development reusing a piece of code has been the "second priority" of many projects (hopefully the "main priority" has been delivering to customer requirements). So why shouldn't design patterns be a step forward in the same evolutionary process? As I mentioned above I believe design patterns are another form of reuse which is different from binary code or source code reuse. Yes we can develop a series of components, frameworks and even code generators to ease the implementation process of design patterns [see http://se.ethz.ch/~meyer/publications/computer/visitor.pdf] but that still will not fill in the function of design patterns. At a higher level than components, binary code, etc. etc. there exist concepts and ideas and solutions in which if we have a uniform way of describing them we can communicate faster and more efficiently in a team. When we use design patterns we can document & evaluate an implementation a lot easier than actually describing everything in detail. We can compare and discuss two competing designs based on the merits of each one and finally pick a good "design pattern" to shape our solution. Design Patterns are a method to encapsulate "expertise."

I would like to add that for design patterns to be really helpful as "expertise encapsulations" your team members should all be intimately familiar with them and this sometimes can be a big downside to extensively using them in a project. Obviously if some members of your team are design pattern experts and the rest of them think design patterns pertain to fashion than you basically have two groups in your team each trying to talk in completely different languages and well we all know what happens next.

I would also like to add that sometimes extensive use of design patterns is overkill. Some problems, especially if the requirements and future change is very predictable and stable, can be solved very easily without employing design patterns. It's also fair to say that once a developer/designer is fluent in the use of design patterns he/she can incorporate them into a solution as easily as using any other design construct. But this doesn't mean that the next guy who comes along and is going to maintain that project or make some changes to it has the same intimacy with design patterns as the first designer therefore we have another overkill. To sum up, design patterns should be used wisely and where needed and like any other tool that we have at our disposal if you try to fix everything using design patterns you're going to end up in a big mess.

So the next post I'll do on design patterns would be a fresh look at probably the most famous design pattern of all time (and probably the simplest): Singleton. Now I know it sounds kind of cheesy to be writing about the same thing as everyone else has already done a million times but I believe that I have some very interesting experiences and fresh ideas regarding this very simple (and useful) pattern to share with everyone. Plus it will give everyone an idea on the level of detail I'm going to get into once I start writing about the other GoF design patterns.

Blog Topic Expansion

It hasn't been that long since I started this blog but I've decided to do a blog topic expansion and also write about other software dev related issues that I like and feel confident in. So I'm also going to blog on OOSD (Object Oriented Software Development) issues & topics ranging from analysis & design issues all the way over to software process & team management issues that I've had to deal with during my current (or past) work experience.

I'm also going to dedicate a fair amount of its contents to design patterns and their practical real life usage. I've had huge exposure to design pattern usage both through practice and through design pattern courses that I have taught, and I would like to share that with everyone through this blog. I'm also hoping that I would find some interesting AOSD (Aspect Oriented Software Development) issues to talk about, especially in conjunction or parallel to more "classical" OO & Design Pattern concepts.

Wish me luck and don't hesitate in adding comments & feedbacks,

Ehsan

Tuesday, May 1, 2007

Hiding the SharePoint blog admin links

When you're setting up a SharePoint public blog site where users don't need the blogs admin links you are pretty much stuck with nothing but a hack. Now why would you want to hide the admin links: it could be many reasons from simplicity to the fact that your users don't need those links since their security settings doesn't permit them to do anything with them. I've had this experience when developing a public blogging site for a news agency. They wanted their users to be able to create a blog, post to their own blog but not be able to edit/modify/approve/reject comments or be able to approve/reject blogs. Now all this can be setup with SharePoint's security features but anyone with Contribute access will get to see the admin links (but won't be able to do much with them). Now since this will be extremely annoying you would want to remove it from a public site.

One of two solutions can be used to achieve this you either have to modify the default.aspx page that exists in each blog using SharePoint designer. This solution is too much work and would hinder other future changes that you need to make to all blogs (since you have to go and change every default.aspx page again).

The second solution is to create a WebPart, put it on the default.aspx file that exists in the templates directory under the blog template site. This WebPart can scan the page and find the AdminLinks WebPart and then hide it:


protected override void OnPreRender(EventArgs e)

{

Control blogadmin = RecursiveFindControl(Page);


if (blogadmin != null) //hide the control

((Microsoft.SharePoint.WebPartPages.BlogAdminWebPart)blogadmin).Hidden = true;

}


private Control RecursiveFindControl(Control control)

{

foreach (Control c in control.Controls)

{

if (c is Microsoft.SharePoint.WebPartPages.BlogAdminWebPart)

return c;

if (c.HasControls())

{

Control rc = RecursiveFindControl(c);

if (rc != null)

return rc;

}

}

return null;

}