Difference between revisions of "Repository Pattern"

From Logic Wiki
Jump to: navigation, search
Line 2: Line 2:
 
[[Category:CSharp]]
 
[[Category:CSharp]]
 
[[Category:ASP.NET]]
 
[[Category:ASP.NET]]
 +
 
Reporsitory pattern is a layer between application and data storage. With this pattern application doesn't need to know how to delete, update or write data to database.
 
Reporsitory pattern is a layer between application and data storage. With this pattern application doesn't need to know how to delete, update or write data to database.
  
Line 97: Line 98:
 
     }
 
     }
 
  }
 
  }
 +
== Generic Repository Pattern ==
 +
[[File:GenericRepository.JPG]]
 +
 +
 +
-----------------
 +
See Also : [[Unit Of Work Pattern]]

Revision as of 12:04, 29 March 2017


Reporsitory pattern is a layer between application and data storage. With this pattern application doesn't need to know how to delete, update or write data to database.

In a nutshell

  • Add(obj)
  • Remove(obj)
  • Get(id)
  • GetAll()
  • Find(predicate)

Sample pattern

using System;
using System.Linq;
using System.Linq.Expressions;
namespace Remondo.Database.Repositories
 {
   public interface IRepository<T>
   {
       void Insert(T entity);
       void Delete(T entity);
       IQueryable<T> SearchFor(Expression<Func<T, bool>> predicate);
       IQueryable<T> GetAll();
       T GetById(int id);
   }
}

The generic GetById() method explicitly needs all our entities to implement the IEntity interface. This is because we need them to provide us with an Id property to make our generic search for a specific Id possible.

namespace Remondo.Database
{
   public interface IEntity
   {
       int ID { get; }
   }
}

Since we already have LINQ to SQL entities with an Id property, declaring the IEntity interface is sufficient. Since these are partial classes, they will not be overridden by LINQ to SQL code generation tools.

namespace Remondo.Database
{
   partial class City : IEntity
   {
   }

   partial class Hotel : IEntity
   {
   }
}

Sample Implementation

using System;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;

namespace Remondo.Database.Repositories
{
   public class Repository<T> : IRepository<T> where T : class
   {
       protected DbSet<T> DbSet;

       public Repository(DbContext dataContext)
       {
           DbSet = dataContext.Set<T>();
       }

       #region IRepository<T> Members

       public void Insert(T entity)
       {
           DbSet.Add(entity);
       }

       public void Delete(T entity)
       {
           DbSet.Remove(entity);
       }

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

       public IQueryable<T> GetAll()
       {
           return DbSet;
       }

       public T GetById(int id)
       {
           return DbSet.Find(id);
       }
       #endregion
   }
}

Generic Repository Pattern

GenericRepository.JPG



See Also : Unit Of Work Pattern