how can I write on generic class for all Repository - c#

I want to write a one class generic for all Repository
I have a class Entity (Product,price,.... )
and the Interface IRepository
public interface IRepository<T>
{
Dictionary<int, T> myProduct { get; set; }
IEnumerable<T> List { get; }
void Add(T entity);
void Delete(T entity,int key);
void Update(T entity,int key);
T FindById(int Id);
}
I can write my Repository for each business object like ProudductRepository, priceReporsitory,.....
but I need to implements as a generic something like this
public class Repository<E, C> : IRepository<E>
and I use this for all my business object not one foreach entity

You can do that as shown below.This is just an example.
Create a Generic Repository
namespace ContosoUniversity.DAL
{
public class GenericRepository<TEntity> where TEntity : class
{
internal SchoolContext context;
internal DbSet<TEntity> dbSet;
public GenericRepository(SchoolContext context)
{
this.context = context;
this.dbSet = context.Set<TEntity>();
}
public virtual IEnumerable<TEntity> Get(
Expression<Func<TEntity, bool>> filter = null,
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
string includeProperties = "")
{
IQueryable<TEntity> query = dbSet;
if (filter != null)
{
query = query.Where(filter);
}
foreach (var includeProperty in includeProperties.Split
(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
{
query = query.Include(includeProperty);
}
if (orderBy != null)
{
return orderBy(query).ToList();
}
else
{
return query.ToList();
}
}
public virtual TEntity GetByID(object id)
{
return dbSet.Find(id);
}
public virtual void Insert(TEntity entity)
{
dbSet.Add(entity);
}
public virtual void Delete(object id)
{
TEntity entityToDelete = dbSet.Find(id);
Delete(entityToDelete);
}
public virtual void Delete(TEntity entityToDelete)
{
if (context.Entry(entityToDelete).State == EntityState.Detached)
{
dbSet.Attach(entityToDelete);
}
dbSet.Remove(entityToDelete);
}
public virtual void Update(TEntity entityToUpdate)
{
dbSet.Attach(entityToUpdate);
context.Entry(entityToUpdate).State = EntityState.Modified;
}
}
}
Please read more about it on Implement a Generic Repository and a Unit of Work Class article.
More Articles for you :
Generic Repository and UnitofWork patterns in MVC
Generic Repository and Unit of Work Pattern

Related

IRepository and Unit Of Work vs Eager Loading

I know having a Unit Of Work is having an abstraction on top of an abstraction (DbContext) and surely that is an anti-pattern, or at least is not necessary.
I have the following problem:
I have a generic IRepository like so:
public interface IGenericRepository<TEntity> where TEntity : class
{
IEnumerable<TEntity> Get(
Expression<Func<TEntity, bool>> filter = null,
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
string includeProperties = "");
TEntity GetByID(object id);
void Insert(TEntity entity);
void Delete(object id);
void Delete(TEntity entityToDelete);
void Update(TEntity entityToUpdate);
}
and this is the implementation of this interface:
public class GenericRepository<TEntity> : IGenericRepository<TEntity> where TEntity : class
{
internal GymHelperContext context;
internal DbSet<TEntity> dbSet;
public GenericRepository(GymHelperContext context)
{
this.context = context;
dbSet = context.Set<TEntity>();
}
public virtual IEnumerable<TEntity> Get(
Expression<Func<TEntity, bool>> filter = null,
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
string includeProperties = "")
{
IQueryable<TEntity> query = dbSet;
if (filter != null)
{
query = query.Where(filter);
}
foreach (var includeProperty in includeProperties.Split
(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
{
query = query.Include(includeProperty);
}
if (orderBy != null)
{
return orderBy(query).ToList();
}
else
{
return query.ToList();
}
}
public virtual TEntity GetByID(object id)
{
return dbSet.Find(id);
}
public virtual void Insert(TEntity entity)
{
dbSet.Add(entity);
}
public virtual void Delete(object id)
{
TEntity entityToDelete = dbSet.Find(id);
Delete(entityToDelete);
}
public virtual void Delete(TEntity entityToDelete)
{
if (context.Entry(entityToDelete).State == EntityState.Detached)
{
dbSet.Attach(entityToDelete);
}
dbSet.Remove(entityToDelete);
}
public virtual void Update(TEntity entityToUpdate)
{
dbSet.Attach(entityToUpdate);
context.Entry(entityToUpdate).State = EntityState.Modified;
}
}
I have a proyect called Facade that instantiates a Mapper and a UnitOfWork, like so:
public class MuscleFacade
{
private readonly UnitOfWork _unitOfWork = new UnitOfWork();
private readonly MuscleMapping _muscleMapping = new MuscleMapping();
public MuscleFacade()
{
}
public IEnumerable<MuscleViewModel> GetAllMuscles()
{
var source = _unitOfWork.MuscleRepository
.Get()
.ToList();
var result = source.Select(x => _muscleMapping.MuscleToModel(x));
return result;
}
public GymViewModel GetGymViewModel()
{
GymViewModel gymViewModel = new GymViewModel
{
ListOfMuscles = GetAllMuscles().ToList()
};
return gymViewModel;
}
}
The MuscleFacade class it's what I inject on my controller with Autofac, I inject an IMuscleFacade in its constructor.
Now the thing is, my MuscleTypeViewModel have a list of MuscleViewModel these models are mapped with their Domain classes counterparts, and in this particular case a MuscleType have many Muscle (Eg: Arm have bicep, tricep, etc) so I put navigational properties on each of them, like so:
public class MuscleType : IEntity
{
public int Id { get; set; }
[StringLength(100)]
public string MuscleTypeName { get; set; }
public ICollection<Muscle> Muscles { get; set; }
}
public class Muscle : IEntity
{
public int Id { get; set; }
[StringLength(100)]
public string MuscleName { get; set; }
public int MuscleTypeId { get; set; }
public MuscleType MuscleType { get; set; }
}
Now let's look at GetAllMuscles method in the Facade again:
public IEnumerable<MuscleViewModel> GetAllMuscles()
{
var source = _unitOfWork.MuscleRepository
.Get()
.ToList();
var result = source.Select(x => _muscleMapping.MuscleToModel(x));
return result;
}
What if I want to Eager-Load MuscleType, how can I change the Get() in order to receive and Expression of Func instead of a string?
You can define a helper class that contains your include definitions:
abstract class IncludeDefinition<TEntity>
{
public abstract IQueryable<TEntity> Include(IQueryable<TEntity> entities);
}
class IncludeDefinition<TEntity, TProperty> : IncludeDefinition<TEntity>
{
public IncludeDefinition(Expression<Func<TEntity, TProperty>> includeEx)
{
_includeEx = includeEx;
}
private readonly Expression<Func<TEntity, TProperty>> _includeEx;
public override IQueryable<TEntity> Include(IQueryable<TEntity> entities)
{
return entities.Include(_includeEx);
}
}
Then use the IncludeDefinition in your Get method
public IEnumerable<Muscle> Get(params IncludeDefinition<Muscle>[] includes)
{
IQueryable<Muscle> muscles = ...;
foreach (var item in includes)
{
muscles = item.Include(muscles);
}
return muscles.ToList();
}
And call the method
_unitOfWork.MuscleRepository
.Get(new IncludeDefinition<Muscle, MuscleType>(m => m.MuscleType));
// Include as many as you wish
_unitOfWork.MuscleRepository
.Get(new IncludeDefinition<Muscle, MuscleType>(m => m.MuscleType),
new IncludeDefinition<Muscle, SomeOtherRelatedEntity>(m => m.SomeOtherProperty));
Edit here comes some way to "just include" instead of writing complicated syntax.
Create a new interface IQueryRepository that supports Get without explicit includes and Include, derive IGenericRepository from this interface:
public interface IQueryRepository<TEntity>
where TEntity : class
{
IEnumerable<TEntity> Get(
Expression<Func<TEntity, bool>> filter = null,
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null);
IQueryRepository<TEntity> Include<TProperty>(Expression<Func<TEntity, TProperty>> referenceExpression);
}
public interface IGenericRepository<TEntity> : IQueryRepository<TEntity>
where TEntity : class
{
IEnumerable<TEntity> Get(
Expression<Func<TEntity, bool>> filter = null,
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
params IncludeDefinition<TEntity>[] include);
// other methods like GetByID, Add, Update...
}
Update the GenericRepository definition - it uses the approach with IncludeDefinition that I initially described and it returns a GenericQueryRepositoryHelper when Include is called.
public class GenericRepository<TEntity> : IGenericRepository<TEntity> where TEntity : class
{
internal DbSet<TEntity> dbSet;
public IEnumerable<TEntity> Get(Expression<Func<TEntity, bool>> filter = null, Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null)
{
return Get(filter, orderBy, new IncludeDefinition<TEntity>[0]);
}
public IEnumerable<TEntity> Get(Expression<Func<TEntity, bool>> filter = null, Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null, params IncludeDefinition<TEntity>[] includes)
{
IQueryable<TEntity> query = dbSet;
foreach (var item in includes)
{
query = item.Include(query);
}
if (filter != null)
{
query = query.Where(filter);
}
if (orderBy != null)
{
return orderBy(query).ToList();
}
else
{
return query.ToList();
}
}
public IQueryRepository<TEntity> Include<TProperty>(Expression<Func<TEntity, TProperty>> referenceExpression)
{
return new GenericQueryRepositoryHelper<TEntity>(this, new IncludeDefinition<TEntity, TProperty>(referenceExpression));
}
// other methods like GetByID, Add, Update...
}
Implement the GenericQueryRepositoryHelper to store includes and apply them when Get is called
public class GenericQueryRepositoryHelper<TEntity> : IQueryRepository<TEntity>
where TEntity : class
{
private readonly IList<IncludeDefinition<TEntity>> _includeDefinitions;
private readonly IGenericRepository<TEntity> _repository;
internal GenericQueryRepositoryHelper(IGenericRepository<TEntity> repository, IncludeDefinition<TEntity> includeDefinition)
{
_repository = repository;
_includeDefinitions = new List<IncludeDefinition<TEntity>> { includeDefinition };
}
public IEnumerable<TEntity> Get(Expression<Func<TEntity, bool>> filter = null, Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null)
{
return _repository.Get(filter, orderBy, _includeDefinitions.ToArray());
}
public IQueryRepository<TEntity> Include<TProperty>(Expression<Func<TEntity, TProperty>> referenceExpression)
{
_includeDefinitions.Add(new IncludeDefinition<TEntity, TProperty>(referenceExpression));
return this;
}
}
Happy querying with includes:
var repo = new GenericRepository<Muscle>(...);
repo.Include(x => x.MuscleType)
.Include(x => x.MuscleType.Muscles)
.Get(x => x.MuscleName == "Test", x => x.OrderBy(m => m.MuscleName));

i would like to do the generic search option in repository pattern

Here i implemented generic repository pattern for do the CRUD operation at the same time , i would like to do the search option as well, anyone can you tell me query to do generic search options, my code is below
public interface IRepository<T> where T : class
{
IEnumerable<T> GetAll();
T GetById(object Id);
T Insert(T obj);
void Delete(object Id);
T Update(T obj);
void Save();
long Count();
}
public class Repository<T> : IRepository<T> where T : class
{
private PegasusPlusEntities context;
private DbSet<T> dbSet;
public Repository()
{
context = new PegasusPlusEntities();
dbSet = context.Set<T>();
}
public IEnumerable<T> GetAll()
{
return dbSet.ToList();
}
public T GetById(object id)
{
return dbSet.Find(id);
}
public T Insert(T obj)
{
dbSet.Add(obj);
Save();
return obj;
}
public void Delete(object id)
{
T entityToDelete = dbSet.Find(id);
Delete(entityToDelete);
}
public void Delete(T entityToDelete)
{
if (context.Entry(entityToDelete).State == EntityState.Detached)
{
dbSet.Attach(entityToDelete);
}
dbSet.Remove(entityToDelete);
}
public T Update(T obj)
{
dbSet.Attach(obj);
context.Entry(obj).State = EntityState.Modified;
Save();
return obj;
}
public long Count()
{
return dbSet.Count();
}
public void Save()
{
try
{
context.SaveChanges();
}
catch (DbEntityValidationException dbEx)
{
foreach (var validationErrors in dbEx.EntityValidationErrors)
{
foreach (var validationError in validationErrors.ValidationErrors)
{
}
}
}
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
if (context != null)
{
context.Dispose();
context = null;
}
}
}
}
thanks in advance...
There are a few ways to do this but here is a simple example
Add a method to the current interface to allow for generic search
IEnumerable<T> Search(Expression<Func<T,bool>> predicate);
A simple implementation can look like this
public IEnumerable<T> Search(Expression<Func<T,bool>> predicate) {
return dbSet.Where(predicate).ToList();
}
An example of it in use would be
var repository = new Repository<Person>();
var searchResults = repository.Search(p => p.FirstName == "John" && p.LastName == "Doe");

CRUD operations with EntityFramework using generic type

I want to be able to use a generic service class of type T that will allow me to query the database dynamically. For example. Normally i would do something like this to delete a record
public void Delete(Post post)
{
this._context.Posts.Remove(post);
}
I want to be able to do this
public void Delete(T post)
{
this._context<T>.Remove(post);
}
I found an article here that sort of brushes over it, but if doesnt seem like a clean way to implement it. https://blog.magnusmontin.net/2013/05/30/generic-dal-using-entity-framework/
You need DbContext.Set
https://msdn.microsoft.com/en-us/library/gg679544(v=vs.113).aspx
Returns a non-generic DbSet instance for access to entities of the given type in the context and the underlying store
public void Delete<T>(T post)
where T : class
{
this._context.Set<T>.Remove(post);
}
For later on, you can also query based on:
this._context.Set<T>.AsQueryable().Where(predicate);
In this instance predicate would be a Expression<Func<T, bool>>
So you can have a generic query method:
public IEnumerable<T> Query<T>(Expression<Func<T, bool>> predicate)
where T : class
{
return this._context.Set<T>().AsQueryable().Where(predicate).ToList();
}
... but I'm digressing from the question slightly now!
You can use Generic repository pattern
public class Repository<TEntity> where TEntity : class
{
private dynamic _context;
private DbSet<TEntity> _dbSet;
protected DbContext Context
{
get
{
if (_context == null)
{
_context = DataContextFactory.GetDataContext();
}
return _context;
}
}
protected DbSet<TEntity> DBSet
{
get
{
if (_dbSet == null)
{
_dbSet = this.Context.Set<TEntity>();
}
return _dbSet;
}
}
public virtual IEnumerable<TEntity> Find(Expression<Func<TEntity, bool>> predicate, Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderExpression = null)
{
return this.GetQuery(predicate, orderExpression).AsEnumerable();
}
public virtual IQueryable<TEntity> GetQuery(Expression<Func<TEntity, bool>> predicate = null, Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderExpression = null)
{
IQueryable<TEntity> qry = this.DBSet;
if (predicate != null)
qry = qry.Where(predicate);
if (orderExpression != null)
return orderExpression(qry);
return qry;
}
public virtual void Insert<T>(T entity) where T : class
{
DbSet<T> dbSet = this.Context.Set<T>();
dbSet.Add(entity);
}
public virtual void Insert(TEntity entity)
{
this.DBSet.Add(entity);
}
public virtual void Update<T>(T entity) where T : class
{
DbSet<T> dbSet = this.Context.Set<T>();
dbSet.Attach(entity);
this.Context.Entry(entity).State = EntityState.Modified;
}
public virtual void Update(TEntity entity)
{
this.Attach(entity);
this.Context.Entry(entity).State = EntityState.Modified;
}
public virtual void Delete<T>(T entity) where T : class
{
DbSet<T> dbSet = this.Context.Set<T>();
if (this.Context.Entry(entity).State == EntityState.Detached)
dbSet.Attach(entity);
dbSet.Remove(entity);
}
public virtual void Delete(TEntity entity)
{
if (this.Context.Entry(entity).State == EntityState.Detached)
this.Attach(entity);
this.DBSet.Remove(entity);
}
public virtual void Delete<T>(object[] id) where T : class
{
DbSet<T> dbSet = this.Context.Set<T>();
T entity = dbSet.Find(id);
dbSet.Attach(entity);
dbSet.Remove(entity);
}
public virtual void Delete(object id)
{
TEntity entity = this.DBSet.Find(id);
this.Delete(entity);
}
public virtual void Attach(TEntity entity)
{
if (this.Context.Entry(entity).State == EntityState.Detached)
this.DBSet.Attach(entity);
}
public virtual void SaveChanges()
{
this.Context.SaveChanges();
}
}
Usage:
public class Sample
{
public void AddNewPerson(Person newPerson)
{
var personRepo = new Repository<Person>();
personRepo.Insert(newPerson);
personRepo.SaveChanges();
}
public void DeletePerson(int personId)
{
var personRepo = new Repository<Person>();
Person person= personRepo.Find(p => p.Id == personId).SingleOrDefault();
personRepo.Delete(person);
}
}

Autofac and WebApi

I am using Autofac for IoC in my project. I have layers for repository, service and web api.
I have class AutofacWebApi in my web api project that I use for registering repository, service and dbcontext.
public class AutofacWebApi
{
public static void Initialize(HttpConfiguration config)
{
Initialize(config, RegisterServices(new ContainerBuilder()));
}
public static void Initialize(HttpConfiguration config, IContainer container)
{
config.DependencyResolver =
new AutofacWebApiDependencyResolver(container);
}
private static IContainer RegisterServices(ContainerBuilder builder)
{
builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
// registration goes here
builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()).PropertiesAutowired();
//EF DbContext
builder.RegisterType(typeof(MOCContext)).As(typeof(DbContext)).InstancePerLifetimeScope();
builder.RegisterType(typeof(UnitOfWork)).As(typeof(IUnitOfWork)).InstancePerLifetimeScope();
//Repositories
builder.RegisterGeneric(typeof(GenericRepository<>)).As(typeof(IGenericRepository<>)).InstancePerLifetimeScope();
builder.RegisterType<CityRepository>()
.As<ICityRepository>()
.InstancePerLifetimeScope();
//Services
builder.RegisterType<CityService>().As<ICityService>().InstancePerLifetimeScope();
return builder.Build();
}
}
My web api controler is like this
public class CityController : ApiController
{
readonly ICityService _cityService;
public CityController(ICityService cityService)
{
_cityService = cityService;
}
public IHttpActionResult Get(int id)
{
var result = _cityService.GetById(id);
return Ok("fdsafds");
}
}
When I call method GetById I got null, but I know that the method should return something because if I call the same method from mvc application I got correct result.
CityService
public class CityService : ICityService
{
IUnitOfWork _unitOfWork;
ICityRepository _cityRepository;
public CityService(IUnitOfWork unitOfWork, ICityRepository cityRepository)
: base()
{
_unitOfWork = unitOfWork;
_cityRepository = cityRepository;
}
public CityDTO GetById(int Id)
{
var result=_cityRepository.GetById(Id);
Mapper.CreateMap<City, CityDTO>()
.ForMember(x=>x.Id,m=>m.MapFrom(d=>d.Id));
var map = Mapper.Map<City, CityDTO>(result);
return map;
}
}
ICityService
public interface ICityService
{
CityDTO GetById(int Id);
}
ICityRepository
public interface ICityRepository:IGenericRepository<City>
{
City GetById(int id);
}
CityRepository
public class CityRepository:GenericRepository<City>, ICityRepository
{
public CityRepository(DbContext context)
: base(context)
{
}
public City GetById(int id)
{
var result = Get(x => x.Id == id);
return result;
}
}
IGenericRepository
public interface IGenericRepository<T> where T : BaseEntity
{
IEnumerable<T> GetAll();
IEnumerable<T> FindBy(Expression<Func<T, bool>> predicate);
T Add(T entity);
T Delete(T entity);
void Edit(T entity);
void Save();
}
GenericRepository
public abstract class GenericRepository<T> : IGenericRepository<T>
where T : BaseEntity
{
protected DbContext _entities;
protected readonly IDbSet<T> _dbset;
public GenericRepository(DbContext context)
{
_entities = context;
_dbset = context.Set<T>();
}
public virtual IEnumerable<T> GetAll()
{
return _dbset.AsEnumerable<T>();
}
public T Get(System.Linq.Expressions.Expression<Func<T, bool>> predicate)
{
T query = _dbset.Where(predicate).SingleOrDefault();
return query;
}
public IEnumerable<T> FindBy(System.Linq.Expressions.Expression<Func<T, bool>> predicate)
{
IEnumerable<T> query = _dbset.Where(predicate).AsEnumerable();
return query;
}
public virtual T Add(T entity)
{
return _dbset.Add(entity);
}
public virtual T Delete(T entity)
{
return _dbset.Remove(entity);
}
public virtual void Edit(T entity)
{
_entities.Entry(entity).State = System.Data.Entity.EntityState.Modified;
}
public virtual void Save()
{
_entities.SaveChanges();
}
}
I don't know why method GetById return result null.
Thanks

C# with Entity Framework Is there a way to implement repository pattern with an abstract base class?

Instead of an interface, I would like my Repositories to inherit from an abstract class (Base Repository). I decided I wanted to use an abstract class because I won't be unit testing the repositories (I will unit test service layer instead) and i didn't want to copy over the same implementation to every repository class that inherits from this base repository.
For example:
public abstract class BaseRepository<T> where T : class
{
protected DbSet<T> dbSet;
public BaseRepository(DbContext dataContext)
{
dbSet = dataContext.Set<T>();
}
public void Insert(T entity)
{
dbSet.Add(entity);
}
public IEnumerable<T> SearchFor(Expression<Func<T, bool>> predicate)
{
return dbSet.Where(predicate);
}
public IEnumerable<T> GetAll()
{
return dbSet;
}
public T GetById(int id)
{
return dbSet.Find(id);
}
}
When I tried to create other repositories that inherit from this base repository, I get problems with the constructor. How do I pass the datacontext into these derived classes so that they can be used?
You need to pass the context in the constructor:
public class Repository<TEntity> where TEntity : class
{
internal MyEntities context;
internal DbSet<TEntity> dbSet;
public Repository(MyEntities context)
{
this.context = context;
this.dbSet = context.Set<TEntity>();
}
public virtual IEnumerable<TEntity> Get(
Expression<Func<TEntity, bool>> filter = null,
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
string includeProperties = "")
{
IQueryable<TEntity> query = dbSet;
if (filter != null)
{
query = query.Where(filter);
}
foreach (var includeProperty in includeProperties.Split
(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
{
query = query.Include(includeProperty);
}
if (orderBy != null)
{
return orderBy(query).ToList();
}
else
{
return query.ToList();
}
}
public virtual TEntity GetByID(object id)
{
return dbSet.Find(id);
}
public virtual void Insert(TEntity entity)
{
dbSet.Add(entity);
}
public virtual void Delete(object id)
{
TEntity entityToDelete = dbSet.Find(id);
Delete(entityToDelete);
}
public virtual void Delete(TEntity entityToDelete)
{
if (context.Entry(entityToDelete).State == EntityState.Detached)
{
dbSet.Attach(entityToDelete);
}
dbSet.Remove(entityToDelete);
}
public virtual void Update(TEntity entityToUpdate)
{
dbSet.Attach(entityToUpdate);
context.Entry(entityToUpdate).State = EntityState.Modified;
}
}
Use:
var repo = new Repository<MyClass>(context);

Categories

Resources