Implementing IDbSet in Generic Repository - c#

I have followed this tutorial and already implemented in my project.
I made some changes to make more testable, so I implement interface
public class GenericRepository<T> : IGenericRepository<T> where T : class
{
internal MyEntities context;
internal IDbSet<T> dbSet;
public virtual IEnumerable<TEntity> GetWithRawSql(string query, params object[] parameters)
{
return dbSet.SqlQuery(query, parameters).ToList();
}
...
}
public interface IEmployeeRepository : IGenericRepository<Employee> { }
public class EmployeeRepository : GenericRepository<Employee>, IEmployeeRepository { }
in UnitOfWork:
public class UnitOfWork : IDisposable
{
private MyEntities context = new MyEntities();
private IEmployeeRepository employeeRepository;
public IEmployeeRepository EmployeeRepository
{
get { return employeeRepository ?? (employeeRepository = new EmployeeRepository(context)); }
}
}
Then I cannot access dbSet in GetWithRawSql() because I change to IDbSet.
(1) How to solve this?
(2) Is there any better approach (without dbSet) to this code using the above approach as this is also failed because IDbSet
unitOfWork.EmployeeRepository.dbSet.Select(c => new {
EmpID = c.EmployeeID,
FullName = (c.LastName + ", " + c.FirstName)
}).OrderBy(o => o.FullName);
Thanks.

i believe that this is a poor implementatin, here is the one im currently using:
this is going to be your IRepository
public interface IRepository<T> where T : class, IEntity
{
void Add(T item);
void Remove(T item);
void Update(T item);
void Attach(T item);
IQueryable<T> All<TProperty>(params Expression<Func<T, TProperty>>[] path);
IQueryable<T> All(params string[] path);
T Find(object id);
T First(Expression<Func<T, bool>> predicate, params string[] path);
Task<T> FirstAsync(Expression<Func<T, bool>> predicate, params string[] path);
T First<TProperty>(Expression<Func<T, bool>> predicate, params Expression<Func<T, TProperty>>[] path);
IQueryable<T> Where<TProperty>(Expression<Func<T, bool>> predicate, params Expression<Func<T, TProperty>>[] path);
IQueryable<T> Where(Expression<Func<T, bool>> predicate, params string[] includes);
}
often you need to attach entities to context so i added Attach method but if you want to keep entity framework attach Seperate (witch is not really needed) from regular IRepository you can define a new interface and called ISqlRepository and inherit from IRepository, creating two IRepository will just make it more complex since it dosnt make any problem in UnitTest. any way if you do want to seperate this is going to be your ISqlRespository:
public interface ISqlRepository<T>: IRepository<T> where T:class, IEntity
{
void Update(T item);
void Attach(T item);
}
this is unitOfWork interface:
public interface IUnitOfWork<T> where T : class, IEntity
{
IRepository<T> Repistory { get; }
IRepository<TEntity> GetTypeRepository<TEntity>() where TEntity : class, IEntity;
object GetContext();
int Commit();
Task<int> CommitAsync();
}
and finally concrete implementation (without ISqlRepository since i dont use that):
this is implementation of IRespository:
public class SqlRepository<T>: IRepository<T> where T: class, IEntity
{
protected readonly DbSet<T> _objectSet;
protected ApplicationDbContext _context;
public SqlRepository(ApplicationDbContext context)
{
_objectSet = context.Set<T>();
this._context = context;
}
public void Add(T item)
{
_objectSet.Add(item);
}
public void Update(T item)
{
_context.Entry(item).State = EntityState.Modified;
}
public void Remove(T item)
{
_objectSet.Remove(item);
}
public T First(Expression<Func<T, bool>> predicate, params string[] path)
{
IQueryable<T> query = _objectSet;
if (path != null)
{
path.ForeEach(i => query = query.Include(i));
}
return query.FirstOrDefault(predicate);
}
public T First<TProperty>(Expression<Func<T, bool>> predicate, params Expression<Func<T, TProperty>>[] path)
{
IQueryable<T> query = _objectSet;
path.ForeEach(p => query = query.Include(p));
return query.First(predicate);
}
public IQueryable<T> Where<TProperty>(Expression<Func<T, bool>> predicate, params Expression<Func<T, TProperty>>[] path)
{
IQueryable<T> query = _objectSet;
path.ForeEach(p => query = query.Include(p));
return query.Where(predicate);
}
public IQueryable<T> Where(Expression<Func<T, bool>> predicate, params string[] includes)
{
IQueryable<T> query = _objectSet;
includes.ForeEach(i => query = query.Include(i));
return query.Where(predicate);
}
public IQueryable<T> All<TProperty>(params Expression<Func<T, TProperty>>[] path)
{
IQueryable<T> query = _objectSet;
if (path != null)
{
path.ForeEach(p => query.Include(p));
}
return query;
}
public IQueryable<T> All(params string[] path)
{
IQueryable<T> query = _objectSet;
if (path != null)
{
path.ForeEach(p => query = query.Include(p));
}
return query;
}
public T Find(object id)
{
return _objectSet.Find(id);
}
public void Attach(T item)
{
T old = _objectSet.Local.FirstOrDefault(i => i.Id == item.Id);
if (old != null)
{
_context.Entry<T>(old).State = EntityState.Detached;
}
_objectSet.Attach(item);
}
public System.Threading.Tasks.Task<T> FirstAsync(Expression<Func<T, bool>> predicate, params string[] path)
{
IQueryable<T> query = _objectSet;
if(path != null)
{
path.ForeEach(i => query = query.Include(i));
}
return query.FirstOrDefaultAsync(predicate);
}
}
this the unitOfWork:
public class SqlUnitOfWork<T> : IUnitOfWork<T> where T : class, IEntity
{
private ApplicationDbContext _db;
public SqlUnitOfWork(ApplicationDbContext context)
{
_db = context;
}
public IRepository<T> Repistory
{
get
{
return new SqlRepository<T>(_db);
}
}
public int Commit()
{
return _db.SaveChanges();
}
public Task<int> CommitAsync()
{
return _db.SaveChangesAsync();
}
public object GetContext()
{
return this._db;
}
public IRepository<TEntity> GetTypeRepository<TEntity>() where TEntity : class, IEntity
{
return new SqlRepository<TEntity>(_db);
}
}

Related

how to inherit from generic repository without needing to implement all the members

Im trying to create a generic repository ,here is my IGenericRepository:
public interface IGenericRepository<TEntity> where TEntity : class
{
Task<IEnumerable<TEntity>> FindByFilterAsync(Expression<Func<TEntity, bool>> predicate, params Expression<Func<TEntity, object>>[] including);
Task<TEntity> GetByIdAsync(int id);
Task<bool> InsertAsync(TEntity obj);
}
here is the implementation of the respository:
public class GenericRepository<TEntity> : IGenericRepository<TEntity> where TEntity : class
{
private readonly IServiceScopeFactory scopeFactory;
public GenericRepository(IServiceScopeFactory scopeFactory) => this.scopeFactory = scopeFactory;
public async Task<IEnumerable<TEntity>> FindByFilterAsync(Expression<Func<TEntity, bool>> predicate, params Expression<Func<TEntity, object>>[] including)
{
using (var scope = this.scopeFactory.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService<CleanArchitectureContext>();
var x = db.Set<TEntity>().AsQueryable();
if (including != null)
{
including.ToList().ForEach(s =>
{
if (s != null)
{
x = x.Include(s);
}
});
}
return await x.Where(predicate).ToListAsync().ConfigureAwait(true);
}
}
public Task<TEntity> GetByIdAsync(int id)
{
throw new NotImplementedException();
}
public Task<bool> InsertAsync(TEntity obj)
{
throw new NotImplementedException();
}
}
now lets say i need to have an interface which is going to inherit from the IGeneric repository and store data in the db:
public interface IStorePayoutRepository:IGenericRepository<PayoutModel>
{
}
so far so good, the problem is if i want to have a class and inherit from IStorePayoutRepository,then i need to implement all the members inside that which is not what i want,because i need only the store one(save or insert into db),
public class PayoutRepository : IGenericRepository<PayoutEntity>
{
//Im forced to implement all the members inside generic interface
}
as you see its not very optimal to implement all the members everytime as they could be irrelevant to the usecase,whats the right way here?i appreciate your help

Add Include to repository

I have a working repository.
public class Repository<TEntity> : IRepository<TEntity> where TEntity : class
{
protected readonly DbContext Context;
public Repository(DbContext context)
{
Context = context;
}
public TEntity Get(int id)
{
return Context.Set<TEntity>().Find(id);
}
public IEnumerable<TEntity> GetAll()
{
return Context.Set<TEntity>().ToList();
}
public IEnumerable<TEntity> Find(Expression<Func<TEntity, bool>> predicate)
{
return Context.Set<TEntity>().Where(predicate);
}
public TEntity SingleOrDefault(Expression<Func<TEntity, bool>> predicate)
{
return Context.Set<TEntity>().SingleOrDefault(predicate);
}
public void Add(TEntity entity)
{
Context.Set<TEntity>().Add(entity);
}
public void Remove(TEntity entity)
{
Context.Set<TEntity>().Remove(entity);
}
}
As I read in coding repositories, that you don't add any class until you really need it. Now, I need to add Include. I found this one in this community Use Include() method in repository:
public static class IncludeExtension
{
public static IQueryable<TEntity> Include<TEntity>(this IDbSet<TEntity> dbSet,
params Expression<Func<TEntity, object>>[] includes)
where TEntity : class
{
IQueryable<TEntity> query = null;
foreach (var include in includes)
{
query = dbSet.Include(include);
}
return query ?? dbSet;
}
}
Then, I changed it to fit in my code (As I think) to be:
public IEnumerable<TEntity> Include(IDbSet<TEntity> dbSet,
params Expression<Func<TEntity, object>>[] includes)
{
IEnumerable<TEntity> query = null;
foreach (var include in includes)
{
query = dbSet.Include(include);
}
return query ?? dbSet;
}
With direct access to context, I am able to write:
Provinces = _cmsDbContext.Provinces.Include(c => c.District).Include(c => c.District.Country).ToList();
But, with repository, I can't write:
Provinces = Currentunitofwork.ProvinceRepository.Include(c => c.District).Include(c => c.District.Country).ToList();
I got error:
cannot convert lambda expression to type IDbSet<Province> because it is not a delegate type
What is the problem here, please.
I suspect that your code is passing in the lambda expression to the IDbSet parameter and can not convert it to that type.
I have not been able to test but it compiles, if the method is a member of the Repository class then try this.
public IEnumerable<TEntity> Include(params Expression<Func<TEntity, object>>[] includes)
{
IDbSet<TEntity> dbSet = Context.Set<TEntity>();
IEnumerable<TEntity> query = null;
foreach (var include in includes)
{
query = dbSet.Include(include);
}
return query ?? dbSet;
}
Again thanks to #Adam Carr.
This is the method code now:
public IQueryable<TEntity> Include(params Expression<Func<TEntity, object>>[] includeExpressions)
{
IDbSet<TEntity> dbSet = Context.Set<TEntity>();
IQueryable<TEntity> query = null;
foreach (var includeExpression in includeExpressions)
{
query = dbSet.Include(includeExpression);
}
return query ?? dbSet;
}
What I change is use Set as a method not a property. So, instead of:
IDbSet<TEntity> dbSet = Context.Set<TEntity>;
I used:
IDbSet<TEntity> dbSet = Context.Set<TEntity>();
Also, I used IQueryable instead of IEnumerable.

Expression<Func<T, bool>> predicate two types

I have WebAPI method:
public async Task<MerchantViewModel> Get(string externalId)
{
return await _service.Get(m => m.ExternalId == externalId);
}
I have IService:
public interface IService<T>
where T : class
{
Task<T> Get(Expression<Func<T, bool>> predicate);
Task<IEnumerable<T>> GetAll(Expression<Func<T, bool>> predicate = null);
void Add(T viewModel);
void Delete(string id);
void Update(T viewModel);
}
And IMerchantService:
public interface IMerchantService : IService<MerchantViewModel>
{
}
Implementation with method:
public class MerchantService : IMerchantService { // ...
public async Task<IEnumerable<MerchantViewModel>> GetAll(Expression<Func<MerchantViewModel, bool>> predicate = null)
.. //
}
Where T is MerchantViewModel
and I have repository with method:
public async Task<IEnumerable<Merchant>> GetItemsAsync(Expression<Func<Merchant, bool>> predicate = null)
Where T is Merchant.
And now I want to do something like that:
public async Task<MerchantViewModel> Get(Expression<Func<MerchantViewModel, bool>> predicate)
{
var domainMerchant = this._repository.GetItemAsync(predicate)
}
How I can do that?
Merchant and MerchantViewModel have same properties. ViewModel have something more.
Solution 1
First, abstract the properties that overlap between MerchantViewModel and Merchant into an interface:
public IMerchantFilter
{
public string ExternalId { get; set; }
...
}
Then let both MerchantViewModel and Merchant inherit from this interface.
public MerchantViewModel : IMerchantFilter
{
public string ExternalId { get; set; }
...
}
public Merchant : IMerchantFilter
{
public string ExternalId { get; set; }
...
}
Use the interface in the predicate signatures:
public class MerchantService : IMerchantService
{
public Task<MerchantViewModel> Get(Expression<Func<IMerchantFilter, bool>> predicate)
{
...
}
...
}
public class MerchantRepository : ...
{
public async Task<IEnumerable<Merchant>> GetItemsAsync(
Expression<Func<IMerchantFilter, bool>> predicate = null)
{
...
}
...
}
Solution 2
A different solution (based on this question) could map predicates between the types:
using System;
using System.Linq.Expressions;
public static class PredicateMapper
{
public static Expression<Func<TTo, bool>> CastParameter<TFrom, TTo>(
this Expression<Func<TFrom, bool>> predicate)
{
var parameter = Expression.Parameter(typeof(TTo));
var body = new ParameterReplacer<TTo>(parameter).Visit(predicate.Body);
return Expression.Lambda<Func<TTo, bool>>(body, parameter);
}
private class ParameterReplacer<TTo> : ExpressionVisitor
{
private readonly ParameterExpression parameter;
public ParameterReplacer(ParameterExpression parameter)
{
this.parameter = parameter;
}
protected override Expression VisitParameter(ParameterExpression node)
{
return this.parameter;
}
protected override Expression VisitMember(MemberExpression node)
{
var matchingMember = typeof(TTo).GetProperty(node.Member.Name);
return Expression.Property(this.Visit(node.Expression), matchingMember);
}
}
}
In your scenario the usage would look as follows:
public async Task<MerchantViewModel> Get(
Expression<Func<MerchantViewModel, bool>> predicate)
{
var domainMerchant = this._repository.GetItemAsync(
predicate.CastParameter<MerchantViewModel, Merchant>());
}
See the working example for more details of this implementation.
Be aware that this solution might look nicer in terms of usage but will result in runtime errors instead of compile time errors in case of types that don't match. This makes it in fact more error prone and less maintanable.

Mocking using MOQ and generic types

Method to unit test: GetUserInfo
Following is the class containing the method:
public class AccountService : IAccountService
{
IUnitOfWork _UnitOfWork;
public AccountService(IUnitOfWork unitOfWork)
{
_UnitOfWork = unitOfWork;
}
public UserInfo GetUserInfo(string userName, string password)
{
var userInfo = new UserInfo();
userInfo.UserType = UserType.Invalid;
// Statement of interest
var portalUser = _UnitOfWork.Repository<DvaPortalUser>().Query().Filter(t => t.Email == userName && t.Password == password).Get().FirstOrDefault();
//....Rest of the code is not included for clarity
}
}
The interface to mock IUnitOfWork:
public interface IUnitOfWork
{
void Dispose();
void Save();
void Dispose(bool disposing);
IRepository<T> Repository<T>() where T : class;
}
Repository Implementation:
public class Repository<TEntity> : IRepository<TEntity> where TEntity : class
{
public virtual RepositoryQuery<TEntity> Query()
{
var repositoryGetFluentHelper = new RepositoryQuery<TEntity>(this);
return repositoryGetFluentHelper;
}
internal IEnumerable<TEntity> Get(
Expression<Func<TEntity, bool>> filter = null,
Func<IQueryable<TEntity>,
IOrderedQueryable<TEntity>> orderBy = null,
List<Expression<Func<TEntity, object>>>
includeProperties = null,
int? page = null,
int? pageSize = null)
{
IQueryable<TEntity> query = DbSet;
if (includeProperties != null)
includeProperties.ForEach(i => query.Include(i));
if (filter != null)
query = query.Where(filter);
if (orderBy != null)
query = orderBy(query);
if (page != null && pageSize != null)
query = query
.Skip((page.Value - 1)*pageSize.Value)
.Take(pageSize.Value);
return query.ToList();
}
}
RepositoryQuery Implementation:
public sealed class RepositoryQuery<TEntity> where TEntity : class
{
private readonly List<Expression<Func<TEntity, object>>> _includeProperties;
private readonly Repository<TEntity> _repository;
private Expression<Func<TEntity, bool>> _filter;
private Func<IQueryable<TEntity>,
IOrderedQueryable<TEntity>> _orderByQuerable;
private int? _page;
private int? _pageSize;
public RepositoryQuery(Repository<TEntity> repository)
{
_repository = repository;
_includeProperties = new List<Expression<Func<TEntity, object>>>();
}
public RepositoryQuery<TEntity> Filter(Expression<Func<TEntity, bool>> filter)
{
_filter = filter;
return this;
}
public IEnumerable<TEntity> Get()
{
return _repository.Get(
_filter,
_orderByQuerable, _includeProperties, _page, _pageSize);
}
}
Unit Test Method:
[TestMethod]
public void AccountService_GetUserInfo_SuccessfulLogin()
{
var _UnitOfWork = new Mock<IUnitOfWork>();
_AccountService = new AccountService(_UnitOfWork.Object);
_UnitOfWork.Setup(a => a.Repository<T>())).Returns(??); //How do I setup this statement?
_UnitOfWork.VerifyAll();
}
Question: How do I setup mock call for the statement _UnitOfWork.Repository()?
I dont know how your IRepository<T> is implemented, but i gues your Query method returns a IEnumerable<T> or a list.
internal interface IRepository<T>
{
IEnumerable<T> Query();
}
// your mock
_UnitOfWork.Setup(a => a.Repository<DvaPortalUser>())).Returns(() => new MyTestRepository());
1. Solution
Define a explicit implemtation of you repository.
// your implementation
public class MyTestRepository : IRepository<DvaPortalUser>
{
public IEnumerable<DvaPortalUser> Query()
{
// return some test users (mocks)
return new List<DvaPortalUser> {new DvaPortalUser(), new DvaPortalUser()};
}
}
2. Solution (thanks to Yuliam Chandra)
Define a mock instead of the implemtation
var repository = new Mock<IRepository<DvaPortalUser>>();
// return some test users (mocks)
repository.Setup(a => a.Query()).Returns(new[] { new DvaPortalUser() });
_UnitOfWork.Setup(a => a.Repository<DvaPortalUser>()).Returns(repository.Object);
What you choose depends on your solution.

Select a database table with a button and insert into it with another

I want to save a record using the entity framework with windows forms. On database I have many tables which are similar to each other; they all have on auto-increment ID column and a string column. What I want is to create a set of buttons that when one of them is pressed it fills a listbox with the designated table. Also another fixed button will insert the string in the fixed textbox into the table selected by the button pressed previously.
The problem is I can't use the created context item in the second button.
Here is the code for one the buttons on the left:
private void btnBB_Click(object sender, EventArgs e)
{
lbItems.DataSource = GsmContext.bbler;
lbItems.DisplayMember = "bb";
bb = new bbler();
bb.bb = txtItem.Text;
GsmContext.AddTobbler(bb);
}
And here is the code for saving the item in the textbox:
private void SaveItem(object sender, EventArgs e)
{
if (txtItem.Text == string.Empty)
{ this.errorProvider1.SetError(txtItem, "Değer girin"); }
else
{
try
{
GsmContext.SaveChanges();
txtItem.Clear();
}
catch (SqlException x)
{
MessageBox.Show(x.Message);
}
}
}
This is what I have got. My Entity framework model defines all the classes from the database. And then I create an interface IRepository and a DataRepository class.
public interface IRepository<T> : IDisposable where T : class
{
IQueryable<T> Fetch();
IEnumerable<T> GetAll();
IEnumerable<T> Find(Func<T, bool> predicate);
T Single(Func<T, bool> predicate);
T First(Func<T, bool> predicate);
void Add(T entity);
void Delete(T entity);
void Attach(T entity);
void Detach(T entity);
void UpdateChanges(T entity);
void SaveChanges();
void SaveChanges(SaveOptions options);
}
public class DataRepository<T> : IRepository<T> where T : class
{
private ObjectContext _context;
private IObjectSet<T> _objectSet;
public DataRepository()
: this(new ModelContainer()) //ModelContainer is the name of the EF model class.
{
}
public DataRepository(ObjectContext context)
{
_context = context;
_objectSet = _context.CreateObjectSet<T>();
}
public IQueryable<T> Fetch()
{
return _objectSet;
}
public IEnumerable<T> GetAll()
{
return Fetch().AsEnumerable();
}
public IEnumerable<T> Find(Func<T, bool> predicate)
{
return _objectSet.Where<T>(predicate);
}
public T Single(Func<T, bool> predicate)
{
return _objectSet.Single<T>(predicate);
}
public T First(Func<T, bool> predicate)
{
return _objectSet.First<T>(predicate);
}
public void Delete(T entity)
{
if (entity == null)
{
throw new ArgumentNullException("entity");
}
_objectSet.DeleteObject(entity);
}
public void Delete(Func<T, bool> predicate)
{
IEnumerable<T> records = from x in _objectSet.Where<T>(predicate) select x;
foreach (T record in records)
{
_objectSet.DeleteObject(record);
}
}
public void Add(T entity)
{
if (entity == null)
{
throw new ArgumentNullException("entity");
}
_objectSet.AddObject(entity);
}
public void Attach(T entity)
{
_objectSet.Attach(entity);
}
public void Detach(T entity)
{
_objectSet.Detach(entity);
}
public void UpdateChanges(T entity)
{
Add(entity);
_context.ObjectStateManager.ChangeObjectState(entity, System.Data.EntityState.Modified);
SaveChanges();
}
public void SaveChanges()
{
_context.SaveChanges();
}
public void SaveChanges(SaveOptions options)
{
_context.SaveChanges(options);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
if (_context != null)
{
_context.Dispose();
_context = null;
}
}
}
}
Then select records like this:
public IList<MyDbClass> GetMyDbClassData()
{
IList<MyDbClass> myDbClassData = null;
using (IRepository<MyDbClass> repository = new DataRepository<MyDbClass>())
{
myDbClassData = (from x in repository.GetAll()
select x).ToList();
}
return myDbClassData;
}
To create a new record:
public void CreateUser(User user)
{
using (IRepository<User> repository = new DataRepository<User>())
{
repository.Add(user);
repository.SaveChanges();
}
}

Categories