Repository pattern

The Repository pattern is widely used in an enterprise application and decouples the DAL with the managers defined in BL through interfaces. It abstracts the underlying technology and architecture of DAL and makes it easy for an architect or developer to change it easily without affecting the BL managers or the objects that are consuming it. For example, we use a repository to separate the implementation of retrieving the data from the business logic and keep it agnostic to the type of data that comprises the data access layer. In this way, our data source could be a database, web service, or any flat file, or the like and changing the data source would not affect the business layer by using the Repository.

In our case, we will implement a generic repository so that any model can use that interface to perform CRUD operations. To implement the Repository pattern, we will create an interface as follows:

    public interface IRepository 
{
IQueryable<T> All<T>() where T : class;
void Create<T>(T TObject) where T : class;
void Delete<T>(T TObject) where T : class;
void Delete<T>(Expression<Func<T, bool>> predicate)
where T : class;
void Update<T>(T TObject) where T : class;
void ExecuteProcedure(string procedureCommand,
params SqlParameter[] sqlParams);
IEnumerable<T> Filter<T>(Expression<Func<T, bool>> predicate)
where T : class;
IEnumerable<T> Filter<T>(Expression<Func<T, bool>> filter,
out int total, int index = 0, int size = 50)
where T : class;
T Find<T>(Expression<Func<T, bool>> predicate)
where T : class;
T Single<T>(Expression<Func<T, bool>> expression)
where T : class;
bool Contains<T>(Expression<Func<T, bool>> predicate)
where T : class;
}

The preceding IRepository interface has generic methods, which can be implemented by the Repository class to perform transaction handling, saving or updating records in the database and searching the records based on different filtering criteria.

Given next is the Repository class that implements the IRepository interface:

    public class Repository : IRepository 
{
DataContext _context;

public Repository(IDbFactory dbFactory)
{
_context = dbFactory.GetDataContext;
}

public T Single<T>(Expression<Func<T, bool>> expression)
where T : class
{
return All<T>().FirstOrDefault(expression);
}

public IQueryable<T> All<T>() where T : class
{
return _context.Set<T>().AsQueryable();
}

public virtual IEnumerable<T> Filter<T>(
Expression<Func<T, bool>> predicate) where T : class
{
return _context.Set<T>().Where<T>
(predicate).AsQueryable<T>();
}

public virtual IEnumerable<T> Filter<T>
(Expression<Func<T, bool>>
filter, out int total, int index = 0, int size = 50)
where T : class
{
int skipCount = index * size;
var _resetSet = filter != null ? _context.Set<T>
().Where<T>
(filter).AsQueryable() : _context.Set<T>().AsQueryable();
_resetSet = skipCount == 0 ? _resetSet.Take(size) :
_resetSet.Skip(skipCount).Take(size);
total = _resetSet.Count();
return _resetSet.AsQueryable();
}

public virtual void Create<T>(T TObject) where T : class
{
var newEntry = _context.Set<T>().Add(TObject);
}

public virtual void Delete<T>(T TObject) where T : class
{
_context.Set<T>().Remove(TObject);
}

public virtual void Update<T>(T TObject) where T : class
{
try
{
var entry = _context.Entry(TObject);
_context.Set<T>().Attach(TObject);
entry.State = EntityState.Modified;
}
catch (Exception ex)
{
throw ex;
}
}
public virtual void Delete<T>(Expression
<Func<T, bool>> predicate) where T : class
{
var objects = Filter<T>(predicate);
foreach (var obj in objects)
_context.Set<T>().Remove(obj);
}
public bool Contains<T>(Expression<Func<T, bool>> predicate)
where T : class
{
return _context.Set<T>().Count<T>(predicate) > 0;
}
public virtual T Find<T>(Expression<Func<T, bool>> predicate)
where T : class
{
return _context.Set<T>().FirstOrDefault<T>(predicate);
}
public virtual void ExecuteProcedure(String procedureCommand,
params SqlParameter[] sqlParams)
{
_context.Database.ExecuteSqlCommand(procedureCommand,
sqlParams);
}

}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.93.141