Design Patters with Examples [Resolved]

Posted by Karthikanbarasan under Others on 1/9/2011 | Points: 10 | Views : 2315 | Status : [Member] [Moderator] [Microsoft_MVP] [MVP] | Replies : 3
Hi,

Can any one give some nice article on design pattern with example in C# Self explanatory

Thanks
Karthik
www.f5Debug.net



Responses

Posted by: SheoNarayan on: 1/9/2011 [Administrator] HonoraryPlatinum | Points: 50

Up
0
Down

Resolved
Hi Karthik,

Read this article that has most of the famous design pattern implementation example. http://www.dotnetfunda.com/articles/article889-design-pattern-implementation-using-csharp-.aspx

Hope this helps.
Thanks

Regards,
Sheo Narayan
http://www.dotnetfunda.com

Karthikanbarasan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Suresh.mekkattil on: 1/9/2011 [Member] Bronze | Points: 25

Up
0
Down
I can give you a example of Implementing the Repository Pattern on Linq.
You can also visit my post on this in http://forums.asp.net/t/1629771.aspx


The following code snippet illustrates how a repository interface is designed:

public interface IStudentRepository
{
Student GetByID(int studentID);
Student Load(int studentID);
void Save(Student student);
void Delete(Student student);
}




Similar to the interface for the Student Repository, you can have interfaces for Product and Order Repositories as shown below:

public interface IProductRepository
{
Product GetByID(int productID);
Product Load(int productID);
void Save(Product product);
void Delete(Product product);
}

public interface IOrderRepository
{
Order GetByID(int orderID);
Order Load(int orderID);
void Save(Order order);
void Delete(Order order);
}


You can now use generics to generalize this interface. Here's a generic implementation of the interface for any repositories of this kind.

public interface IRepository<T>
{
T GetById(int id);
T Load(int id);
void Save(T entity);
void Delete(T entity);
}


Designing a LINQ Repository

To implement the repository pattern for LINQ, you would first need to define the interface that would contain the declaration of all the methods you would want in your Repository.

The following interface shows how you can use the repository pattern in LINQ:

public interface IRepository<T> where T : class
{
IQueryable<T> GetAll();
void InsertOnSubmit(T entity);
void DeleteOnSubmit(T entity);
void SubmitChanges();
}


You can then use your repository to encapsulate to store, retrieval and query would be accomplished in your data access code using the LINQ data context. The IRepository interface is implemented by the repository class as shown in the code listing below:

public class Repository<T> : IRepository<T> where T : class
{
public DataContext Context
{
get;
set;
}

public virtual IQueryable<T> GetAll()
{
return Context.GetTable<T>();
}

public virtual void InsertOnSubmit(T entity)
{
GetTable().InsertOnSubmit(entity);
}

public virtual void DeleteOnSubmit(T entity)
{
GetTable().DeleteOnSubmit(entity);
}

public virtual void SubmitChanges()
{
Context.SubmitChanges();
}

public virtual ITable GetTable()
{
return Context.GetTable<T>();
}
}




To implement the repository pattern for Entity Framework, you can start by defining an interface as shown below:

public interface IRepository<T>
{
T GetById(int id);
T[] GetAll();
IQueryable<T> Query(Expression<Func<T, bool>> filter);
void Save(T entity);
void Delete(T entity);
}


Now that the interface has been defined, you can use it to implement your repository classes. The following code listing shows how you can implement the StudentRepository class:


public class StudentRepository : IRepository<Student>, IDisposable
{
private MyEntities _context;

public StudentRepository()
{
_context = new MyEntities();
}

public Student GetById(int id)
{
return _context.Students.

Where(s => s.StudentID == id).FirstOrDefault();
}

public Student[] GetAll()
{
return _context.Students.ToArray();
}

public IQueryable<Student> Query(Expression<Func<Student, bool>> filter)
{
return _context.Students.Where(filter);
}

public void Save(Student student)
{
// Code to Save a student record to the database
}

public void Delete(Student student)
{
// Code to delete a student record
}

public void Dispose()
{
if (_context != null)
{
_context.Dispose();
}
}
}






-----
Suresh M

Karthikanbarasan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Karthikanbarasan on: 1/9/2011 [Member] [Moderator] [Microsoft_MVP] [MVP] Silver | Points: 25

Up
0
Down
Thanks Sheo Narayan...

Thanks
Karthik
www.f5Debug.net

Karthikanbarasan, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response