[X]

Asp.Net 4.0 - Custom Output Caching Provider (Design Patterns in Action)

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.

Let's start-

1. Create a C# Library Project

2. Add reference to System.Web dll

More...

C# Abstract Factory Pattern - Design Patterns in Action

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

 More...

C# Factory Pattern - Design Patterns in Action

Factory Method Pattern


Factory method pattern is a very light weight pattern. It is used in the scenarios where we do not want the client directly refer to the concrete classes for object instantiation, rather we handover the responsibility to a "Creator" class which will be instantiating the appropriate object based on the client requirement and thus centralizing the code of object creation. And client deals with interface instead of concrete classes. Factory method is also termed as "Virtual Constructor" in C++.

Intent & Motivation of Factory Pattern: "Define an interface for creating an object, but let subclasses decide which class to instantiate." Since library knows when an object needs to be created, but not what kind of object is going to be created. As this is specific to the application it can use the Factory Method.


Factory pattern is used when:

  • Flexibility is important.
  • Objects can be extended in subclasses
  • There is a specific reason why one subclass would be chosen over another. This logic forms part of the Factory Method.

We can think of below scenarios where Factory method pattern can be implemented:

 

  1. Set upa system to draw circles, squares, and lines. Create a Factory Method that instantiates one of these classes at random and uses it to draw a shape of random size on the screen. Run the program to see what interesting shapes you get.

  2. A company has a web site to display test results from a plain text file. The company recently purchased a new computer that produces a binary data file, and it has another new machine on the way that will possibly produce a different data file. The company is also considering switching to an XML format. Write a system to deal with such changes. The web site just needs data to display; your job is to provide the specified data format for the web site.

  3. We have list of movies categorized into genre. Client wants to get the specific genre movies. Here client can deal with movie interface and based on his parameter passed to FactoryMethod he will get the list of movies.


So let us work on the 3rd movie list example. More...

C# Repository Pattern - Design Patterns in Action

Repository Pattern

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

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...

Learning C# Design Patterns Series -Why Design Patterns

Design Patterns - The Best Practices

Few years back, design patterns for .net developers were less known though it is widely being used by java developers.

So as .net languages and developers got more matured over the time, people started looking for .net code optimization, performance, best practices, extensibility and maintainability of the code side.

First obvious question about making use of design patters is why? Why we should implement design patterns in our solutions/applications.

Well I will try to read and make notes of why and how of design patterns and share in this series. So let begin.

1. Why design patterns? What benefit I will get?

Productivity: Design pattern helps to build an architecture which can enhance the productivity.

Extensibility: When a software is released and becomes successful and widely accepted it is obvious that it will receive lots of request change and updates. So if while building the application proper design patterns is implented it will be easy to supply those updates and request. Most design patterns promote the idea of coding against interfaces and base classes, which makes it easier to change and enhance your application at a later stage.

Simplicity: Our solution architecture should be simple, well thought out, clean, crisp and easy to understand. This can be acheived by knowing the design pattern in advance.More...

asp.net SQL server - import export excel worksheet using ado.net

Import/Export E worksheet using ADO.Net

Excel is very known format for storing small amount of data. So you may want your data residing in ms sql or oracle or any other data source do be exported because of any of the following reason:

  • Running calculation to derive the wanted information
  • Doing some statistical computation like MEAN, MEDIAN, MODE
  • Filtering/Sorting
  • Generating Graphical report
  • Making use of PivotTable and PivotChart

Apart from above listed one there exists also lots of other reasons to export data in excel format. Similarily we can use excel to do bulk import of data and put it into sql server database. It is very easy and faster to do data entry in excel sheet and then import those data to rdbms system.

Export to Excel from SQL Server:

There are may ways we can read data from sql server and export to excel sheet like using third party controls, binding data to some data controls and then exporting using "GridView.RenderControl()" method.

But here I am going to show how to export from sql server to ms excel using ado.net.

1. Create an asp.net website project named "ExcelExportImport" and 2 folders to it "DownloadFiles" and "UploadFiles".

2. Download AdventureWorksLT2008 database and attach ot your sql server database. More...

Posts By Month