Tuesday 13 January 2015

Iterator Design Pattern

Iterator is one of the commonly used patterns in regular basis by most of the developers.  It  is common that most of time when we are using collections, we are using this pattern even without knowing it.

By definition the iterator pattern means that we should be able to iterate though elements of an aggregate object without exposing its underlying implementation.

Following is a basic structure of an implementation of the iterator pattern.



As we can see in this example , the client is only depending on the Interface but not with the actual implementation of the concrete class. The aggregate interface helps to create the elements of the iterator while the iterater interface used to access the elements sequentially.

The C# language implemented the Iterator pattern mainly using the IEnumerable and IEnumerator interfaces. In here the IEnumerable is used as the Aggregate interface while IEnumerator used as the Iterator interface.


The foreach loop in C# operates on any type implementing IEnumerable including basic arrays. 

Following is a very simple example.

var letters = new String[] { "a", "b", "c", "d", "e" };

            foreach(letter in letters)
                Console.WriteLine(letter);

The same can be implement using following code as well


var letters = new String[] { "a", "b", "c", "d", "e" };

            var enumerator = letters.GetEnumerator();

            while(enumerator.MoveNext())
                Console.WriteLine(enumerator.Current);

The C# LINQ is highly using this Iterator pattern.

Tuesday 6 January 2015

Singleton Pattern

Singleton pattern is one of the easiest patterns that we can implement. However, if not properly implemented it would result many issues in the system which using this pattern. Industrially, the Singleton pattern heavily used whenever the application needs to access external resources such as file reads and database access.

The main objective of the singleton pattern is to maintain only one instance of a class. Normally the constructor of a singleton class does not have any parameters. Because having a parameter means there would be different behavior based on the constructor.
Following is an example to a s Singleton class.

  public class Singleton
    {
        private static Singleton _instance;

        private Singleton()
        {
        }

        public static Singleton Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new Singleton();
                }
                return _instance;
            }
        }

        public void DoWork()
        {
        }
    }

Note that the constructor to this class is private. This means that external classes cannot create instances of this class. The instance needs to get created the class within itself.
Using the Singleton pattern is very easy. All we need to do is just refer the instance as follows.

    Singleton.Instance.DoWork ();
Or
SomeMethod(Singleton.Instance);

Being the implantation is very simple; the Singleton pattern is NOT thread safe. This is the main disadvantage of this pattern over its simplicity.

Most of the current applications are running on threaded environments and hence following is the way we can make the Singleton pattern thread safe.

One way that we can make the singleton thread safe is , using locks. But this is effecting to the performance of the application.

Following is a way we can make the singleton pattern thread safe. This is using the way that type initializers work in .Net. if the type is not marked as “Beforefieldinit ” , the type initialization happens on-demand in the compiler. Ie : lazy implementation.

Following is the code

public class LazySingleton
{
    private LazySingleton()
    {
    }

    public static LazySingleton Instance
    {
        get { return Nested.instance; }
    }

    private class Nested
    {
        static Nested()
        {
        }

        internal static readonly LazySingleton instance = new LazySingleton();
    }
}

One consequence of the Singleton pattern is , it is not easy to test it. We can avoid this using IOC. I will explain that in a different blog post.




Thursday 1 January 2015

Repository Design Pattern

Projects are accessing data from various sources from database, flat files to web services. The business layer of any particular application consumes these accessed data in order to utilize in business logic. The separation of concerns and the testability plays major role when we are accessing data from various sources. The Repository design pattern provides us generic solutions to these problems. Data access is not the only place where we can use the repository pattern, but is the most used domain of this particular design pattern.


Following diagram depicts a bird’s eye view of how the repositories do the linkage between the data source and the business layer. The Business logic would access in memory data from the repositories rather than accessing the physical data source directly. And this clearly separates the physical data access technologies and business logic technologies. 

Following is an example of how to implement the employee repository. Starting with the interface.

  public interface IEmployeeRepo
    {
        void Add(Employee newEmp);
        void Update(Employee emp);
        void Remove(Employee emp);
        IList<Employee> Search(Expression<Func<Employee, bool>> predicate);
        Employee GetById(int empId);
    }

Even though I have written the update method in this interface, it is unlikely that in a real production system that we are having this kind of individual update method. Mostly, we are using the UOW design pattern for the atomic updates in the repositories.

The Search function is also using a LINQ predicate in order to enable the calling entity to use LINQ queries for search features. 

Following is an example of how to use this Interface when we need to dealt with the Employee entity.
 
public class WorkingClass
    {
        public void DoWork(IEmployeeRepo repository)
        {
            repository.Add(new Employee());
            repository.Search(x => x.Name == "James");
            repository.GetById(23);
        }
    }

We need to create these repositories for each and every entity in the application. Which is leading to a maintenance problem. Hence it is advisable to create Generic repository to handle operations rather than create repositories for each and every entity.

 Following is a Sample Interface for a generic repository.

  public interface IRepository<T>
                    where T : class, IEntity
    {
        IQueryable<T> FindAll();
        IQueryable<T> Search(Expression<Func<T, bool>> predicate);
        T GetById(int id);       
        void Add(T newEntity);
        void Remove(T entity);
    }

We are using the generic type T to represent entities in repository.

Following is the concrete implementation of the IRepository.

public class RepositoryImpl<T> : IRepository<T>
                                    where T : class, IEntity {

        public RepositoryImpl(ObjectContext context) {
            _objectSet = context.CreateObjectSet<T>();
        }

       public IQueryable<T> Search (Expression<Func<T, bool>> predicate) {
            return _objectSet.Where(predicate);
        }

        public void Add(T newEntity) {
            _objectSet.AddObject(newEntity);
        }

        public void Remove(T entity) {
            _objectSet.DeleteObject(entity);
        }

        public IQueryable<T> FindAll()
        {
            return _objectSet;
        }

        public T GetById(int id)
        {
            return _objectSet.Single(x => x.Id == id);
        }

        protected ObjectSet<T> _objectSet;
    }

You can see that the object context playing a big role when creating the repository instance. This is the actual entity framework context we are using here to connect to the database.  We can see that how it is converted the EF specific operations on objects based on the required method. 

One thing to note in GetById method, it requires the attribute Id contains on each and every entity that we are dealing with. This is achieved using the IEntiry interface which make sure all the inherited entities consist of id attribute.

  public interface IEntity {
        int Id { get; }
    }

Following depicts how we can use the IRepository implementation in a worker class. As an example, I am using a web controller class.

public class WorkerController : Controller
    {      
        public WorkerController()
            :this(new SqlUnitOfWork())
        {
           
        }

        public WorkerController(IUnitOfWork unitOfWork)
        {
            _unitOfWork = unitOfWork;
            _repository = _unitOfWork.Employees;
        }       

        public ViewResult Index()
        {
            var model = _repository.FindAll()                                  
                                   .OrderBy(e => e.dateRegistered);
            return View(model);
        }


        public ViewResult Details(int id)
        {
            var model = _repository.FindById(id);

            return View(model);
        }

      

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            var model = _repository.FindById(id);
            TryUpdateModel(model, new[] { "FirstName", "dateRegistered" });
            if (ModelState.IsValid)
            {
                _unitOfWork.Commit();
                return RedirectToAction("Index");
            }

            return View(model);
        }

        private readonly IUnitOfWork _unitOfWork;
        private readonly IRepository<Employee> _repository;
    }

I have used the Unit Of Work pattern to perform transactions against the db. You can find how the UOW is implemented in this post.

Ultimately , you can see that the controller class do not need to bother about how this data been actually accessed by the respective repository. This is the real power of this repository pattern.