Session: Refactoring to SOLID Code

We recently organized October User Group Meeting on Design Principles and Practices at MIC, Karachi. I did a session on “Refactoring to SOLID code” and really enjoyed discussion with developers on design principles and practices.
We talked about aspects of bad design, better design and how we can apply SOLID principles to our day to day code. The session was irrespective of any technology and more related to Object Oriented Principles. As ‘software design’ is one of the area I am passionate about, so I love the talk.

AttendeesAdil Mughal - Refactoring to SOLID Code

The objective was to provide awareness and realization to developers on how following different design principles and practices will eventually make their life easy. I would like to thank everyone who attended the session and the contributors who made this event reality especially speaker (visitor) Zubair Ahmed and Kamran (from MIC) for needful arrangements.
As promised, I have shared session presentation and source code. See details below. StructureMap, the free Dependency Injection tool can be downloaded from github or Nuget Package
You can download source code demonstrated and presentation from shared folder.
P.S. Hope everyone enjoyed lucky draw 😉

Book Review: Professional Enterprise .NET – Wrox

Professional Enterprise .NET Cover

Book Title: Professional Enterprise .NET
Authors: Jon Arking and Scott Millet
Publisher: Wrox

I must say one of the best book I have read on Enterprise Design and Code. The author did very well in explaining how to write flexible, extensible code. If you are a mid level .NET developer and you are curious enough to think about more flexible, testable and extensible applications, you will definitely love this book.

This book covers some of the very exciting topics such as Separation of Concerns, Flexibility, Reusability, Unit Testing, Test Driven Development, Inversion of Control, Domain-Driven, Data access layer, Model-View Presenter, Model-View-Controller. Although it may sounds dry but the author did a great job in keeping me stick to the book. After so many time, this was the book which kept me reading it until i finished it.

The book started with the discussion on What is Enterprise Architecture and Development? Then with an example of Mortgage Loan sample application, the author have applied above mention topics and concepts.

You can also download code of this book from codeplex site.

All in all, I think that it’s worth investing time and money on this book and it’s really a comprehensive guide to the latest enterprise development patterns and methodologies that will make your code cleaner and maintainable.

Design Patterns

Design Patterns are reusable solutions to a commonly occurring problem in software. It’s not a ready to use code instead it is more like a general template or solution for a particular problem. When you get understanding of Design Patterns then at a time designing solutions you can come across a situation like “Oh Yes! I can apply here that particular design pattern” 🙂

Design Patterns provides you a proven and tested solution to a particular problem. They speed up design process and hence enhances productivity. I’m not going to give detail introduction to Design Patterns as purpose here is to understand and see how we can apply a particular design pattern in a language here C#. Briefly, there are four basic categories of Design Patterns

  • Creational Patterns
  • Behavior Patterns
  • Structural Patterns
  • Concurrency Patterns

Personally, I have more hands on of Creational Patterns. There are numerous design patterns in each category. We will look some of the more common in them.

So Let’s begin with a very simple design pattern which I learnt first and i.e. Singleton.

Singleton Design Pattern

Singleton ensures a single instance of the class and have global access. To apply singleton to a class we make it’s constructor(s) private and the class itself creates its object. For external world to get the object of the class it uses a static method that returns the object. Lets see the code below:

public class Singleton
{
private static Singleton instance;

private Singleton() {}

public static Singleton GetInstance()
{
if (instance == null)
{
instance = new Singleton();
}
return instance;
}
}

Here you can see, the class has a static object of itself. Whenever the want the instance of that class you will call GetInstance Method. If the object is not initialized, the method will initialize itself or it’s done already it will give you the same object. In this way, the class will ensure a single instance and will provide a global access through static method GetInstance.

Note that it is the simple way of creating Singleton class, There are optimized version as well that provides multi-thread safe version by using lock mechanism of C# and .NET. For details please visit Implementing Singleton in C#

In the coming posts, we will see different design patterns belonging to different categories. Besides, a very good material dedicated to Patterns and Practices is available on MSDN.

Bye for now! Have a good time.