Desing Patterns in Action
Today we will develop a custom output caching provider in asp.net 4.0. In .net framework 4.0 microsoft has emphasized on provider model for pluggable development. The provider model in .net 4.0 has been used and promoted extensively.
Developers can write their custom provider for logging, membership, caching, viewstate without affecting the existing application and plug that up with just configuration changes.
I will try to show this by writing custom file based output caching for asp.net 4.0 application. There may be scenario where memory based caching is expensive, so we can make use of provider model and write our own custom output caching.
1. Create a C# Library Project
2. Add reference to System.Web dll
Abstract Factory Design Pattern
We have already seen about factory method design pattern. abstract factory is about grouping similar family of factory pattern and providing abstract layering for them.
The main motto of abstract factory design pattern is "Provide an interface for creating families of related or dependent objects without specifying their concrete classes." as specified in GoF. Below are the main members of this design pattern:
AbstractFactory - An interface with Get operations for each of the abstract products
Factory1, Factory2 - Implementations of all the AbstractFactory creation operations
AbstractProduct - An interface for a kind of product with its own operations
ProductA1, ProductA2, ProductB1, ProductB2 - Classes that implement the AbstractProduct interface and define product objects to be created by the corresponding factories
Client - A class that accesses only the AbstractFactory and AbstractProduct interfaces
Repository pattern is one of the pattern which is most frequently used in Data access layer. This provides an abstraction to underlying database. This is also useful when database is not ready and we want to test the application by building an stub.
So repository pattern provides testability, abstration and dependency injection.
Repository is defined as "an abstraction that provides us with persistence ignorance and a separation of concerns where the responsibility of persisting domain objects is encapsulated by the Repository that leaving the domain objects to deal entirely with the domain model and domain logic."
I am going to show a sample of repository pattern implementation with c# and entity framework. Our entity framework DataContext object will be inside the constructor of the repository class. This will implement IDisposable interface to make sure that object is disposed properly in case of any failure or when object is no more required.
We will delcare some of the most frequenlty used operations in interface such as GetOne, GetMany, Filter, Save, Delete.
So IRepository and RepositoryImplementation Class will be the building blocks of repository pattern.More...