Decorator Pattern in C#

Read full article on Falafel blog

Decorator pattern attach additional functionalities or responsibilities to an existing object at the runtime”. It adds new behaviour to an individual object without affecting other objects of the same class. New behaviour or functionalities can be either added dynamically at the run time or statically.

image

The decorator pattern:

  • · Adds new functionality or behaviour
  • · Either at run time or statically
  • · Without affecting other objects of the class
  • Why do we need this pattern?

    To understand, let us consider a Student object with the basic responsibility to handle and print information like name, age, and grade etc.

    Now you have a requirement to add functionality to maintain lab reports for Science students. How will you do this? Maybe extend the Student class to create a ScienceStudent subclass for this responsibility. Good enough.

Read full article on Falafel blog

Implement Step-by-Step Generic Repository Pattern in C#

Read full article on Falafel blog

It is not a good idea to access the database logic directly in the business logic. Tight coupling of the database logic in the business logic make applications tough to test and extend further. Direct access of the data in the business logic may cause problems such as

  1. Difficulty completing Unit Test of the business logic
  2. Business logic cannot be tested without the dependencies of external systems like database
  3. Duplicate data access code throughout the business layer

Without a Repository, an application will be seen as below:

image

Repository Pattern separates the data access logic and maps it to the entities in the business logic. It works with the domain entities and performs data access logic. In the Repository pattern, the domain entities, the data access logic and the business logic talk to each other using interfaces. It hides the details of data access from the business logic. In other words, business logic can access the data object without having knowledge of the underlying data access architecture. For example, in the Repository pattern, business logic is not aware whether the application is using LINQ to SQL or ADO.NET Entity Model ORM. In the future, underlying data sources or architecture can be changed without affecting the business logic.

Read full article on Falafel blog