The code below seems to work using this:
var unitOfWorkProvider = new PetaPocoUnitOfWorkProvider();
var repository = new FakeRepository();
var fake = new Fake
{
// etc.
};
using (var uow = unitOfWorkProvider.GetUnitOfWork("BlaConnectionString"))
{
repository.Insert(uow, fake);
uow.Commit();
}
which may eventually move into a service layer. I would appreciate any feedback to improve this code.
public interface IUnitOfWork : IDisposable
{
void Commit();
Database Database { get; }
}
public interface IUnitOfWorkProvider
{
IUnitOfWork GetUnitOfWork(string connectionString);
}
public class PetaPocoUnitOfWorkProvider : IUnitOfWorkProvider
{
public IUnitOfWork GetUnitOfWork(string connectionString)
{
return new PetaPocoUnitOfWork(connectionString);
}
}
public interface IRepository<T>
{
void Insert(IUnitOfWork unitOfWork, T entity);
void Update(IUnitOfWork unitOfWork, T entity);
void Delete(IUnitOfWork unitOfWork, T entity);
T Fetch(IUnitOfWork unitOfWork, long uid);
}
public class PetaPocoUnitOfWork : IUnitOfWork
{
private readonly Transaction _petaTransaction;
private readonly Database _database;
public PetaPocoUnitOfWork(string connectionString)
{
_database = new Database(connectionString);
_petaTransaction = new Transaction(_database);
}
public void Dispose()
{
_petaTransaction.Dispose();
}
public Database Database
{
get { return _database; }
}
public void Commit()
{
_petaTransaction.Complete();
}
}
public class FakeRepository : IRepository<Fake>
{
public void Insert(IUnitOfWork unitOfWork, Fake entity)
{
unitOfWork.Database.Save(entity);
}
public void Update(IUnitOfWork unitOfWork, Fake entity)
{
unitOfWork.Database.Update(entity);
}
public void Delete(IUnitOfWork unitOfWork, Fake entity)
{
unitOfWork.Database.Delete(entity);
}
public FakeJobFact Fetch(IUnitOfWork unitOfWork, long uid)
{
return unitOfWork.Database.Fetch<Fake>("SELECT * FROM Fakes WHERE [FakeId] = #0", uid).FirstOrDefault();
}
}
PS:
I have adapted the code according to #Plebsori current answer:
public abstract class BaseRepository<T>
{
protected IDatabase Database
{
get
{
return UnitOfWork.Current;
}
}
public void Insert(T entity)
{
Database.Save(entity);
}
public void Update(T entity)
{
Database.Update(entity);
}
public void Delete(T entity)
{
Database.Delete(entity);
}
}
public interface IRepository<T>
{
void Insert(T entity);
void Update(T entity);
void Delete(T entity);
T Fetch(long uid);
}
public interface IUnitOfWork : IDisposable
{
void Commit();
Database Database { get; }
}
public interface IUnitOfWorkProvider
{
IUnitOfWork GetUnitOfWork(string connectionString);
}
public class PetaPocoUnitOfWork : IUnitOfWork
{
private readonly Transaction _petaTransaction;
private readonly Database _database;
public PetaPocoUnitOfWork(string connectionString)
{
_database = new Database(connectionString);
_petaTransaction = new Transaction(_database);
}
public void Dispose()
{
UnitOfWork.Current = null;
_petaTransaction.Dispose();
}
public Database Database
{
get { return _database; }
}
public void Commit()
{
_petaTransaction.Complete();
}
}
public class PetaPocoUnitOfWorkProvider : IUnitOfWorkProvider
{
public IUnitOfWork GetUnitOfWork(string connectionString)
{
if (UnitOfWork.Current != null)
{
throw new InvalidOperationException("Existing unit of work.");
}
var petaPocoUnitOfWork = new PetaPocoUnitOfWork(connectionString);
UnitOfWork.Current = petaPocoUnitOfWork.Database;
return petaPocoUnitOfWork;
}
}
public static class UnitOfWork
{
[ThreadStatic] public static IDatabase Current;
}
You may or may not like, but here's how I removed the passing of unit of work and the unit of work from the interface.
var unitOfWorkProvider = new PetaPocoUnitOfWorkProvider();
var repository = new FakeRepository();
var fake = new Fake
{
// etc.
};
using (var uow = unitOfWorkProvider.GetUnitOfWork("BlaConnectionString"))
{
repository.Insert(fake);
uow.Commit();
}
Code
public interface IUnitOfWorkProvider
{
IUnitOfWork GetUnitOfWork(string connectionString);
}
public static class UnitOfWork
{
[ThreadStatic]
public static IUnitOfWork Current { get; set; }
}
public class PetaPocoUnitOfWorkProvider : IUnitOfWorkProvider
{
public IUnitOfWork GetUnitOfWork(string connectionString)
{
if (UnitOfWork.Current != null)
{
throw new InvalidOperationException("Existing unit of work.");
}
UnitOfWork.Current = new PetaPocoUnitOfWork(connectionString);
return UnitOfWork.Current;
}
}
public interface IRepository<T>
{
void Insert(T entity);
void Update(T entity);
void Delete(T entity);
T Fetch(long uid);
}
public class PetaPocoUnitOfWork : IUnitOfWork
{
private readonly Transaction _petaTransaction;
private readonly Database _database;
public PetaPocoUnitOfWork(string connectionString)
{
_database = new Database(connectionString);
_petaTransaction = new Transaction(_database);
}
public void Dispose()
{
UnitOfWork.Current = null;
_petaTransaction.Dispose();
}
public Database Database
{
get { return _database; }
}
public void Commit()
{
_petaTransaction.Complete();
}
}
public abstract class BaseRepository<T> : IRepository<T>
{
protected IDatabase Db
{
get
{
return UnitOfWork.Current;
}
}
}
public class FakeRepository : BaseRepository<T>
{
public void Insert(Fake entity)
{
Db.Save(entity);
}
public void Update(Fake entity)
{
Db.Update(entity);
}
public void Delete(Fake entity)
{
Db.Delete(entity);
}
public FakeJobFact Fetch(long uid)
{
return Db.Fetch<Fake>("SELECT * FROM Fakes WHERE [FakeId] = #0", uid).FirstOrDefault();
}
}
Related
I am receiving null exception error on my framework. I have tried to apply Repository and Unit of Work design patterns in my application. What I am trying to do is simply retreiving user titles from my data base with GetAll() method.
Here is my repository class:
public class Repository<T> : IRepository<T> where T : class
{
protected readonly DbContext Context;
public Repository(DbContext context)
{
this.Context = context;
}
public T Get(int id)
{
return Context.Set<T>().Find(id);
}
public IEnumerable<T> GetAll()
{
return Context.Set<T>().ToList();
}
public IEnumerable<T> Find(Expression<Func<T, bool>> predicate)
{
return Context.Set<T>().Where(predicate);
}
public void Add(T entity)
{
Context.Set<T>().Add(entity);
}
public void AddRange(IEnumerable<T> entityList)
{
Context.Set<T>().AddRange(entityList);
}
public void Remove(T entity)
{
Context.Set<T>().Remove(entity);
}
public void RemoveRange(IEnumerable<T> entityList)
{
Context.Set<T>().RemoveRange(entityList);
}
}
This is IUserTitlesRepository:
public interface IUserTitlesRepository : IRepository<UserTitles>
{
}
And, the class where above interface implemented:
public UserTitlesRepository(XaPaDataContext context) : base(context)
{
}
public XaPaDataContext XaPaDataContext
{
get { return Context as XaPaDataContext; }
}
Before coming to Controller layer, I have two more layers, which are Operation and Manager layers. And, I think I have messed up on that part (on Base Manager class as shown below).
This is operation layer:
public class UserTitlesOperations
{
private readonly IUnitOfWork _uow;
public UserTitlesOperations(IUnitOfWork uow)
{
_uow = uow;
}
public List<UserTitles> GetAllUserTitles()
{
try
{
List<UserTitles> userTitleList = _uow.UserTitles.GetAll().ToList();
_uow.Complete();
return userTitleList;
}
catch (Exception ex)
{
throw new Exception(ex.ToString());
}
}
}
Below is the BaseManager class which gives inheritance to all manager classes.
public abstract class BaseManager
{
private IUnitOfWork _iUow;
private readonly XaPaDataContext _context;
public IUnitOfWork IUOW
{
get
{
if (_iUow == null)
{
_iUow = new XaPaUnitOfWork(_context);
}
return _iUow;
}
}
}
This is the manager class:
public class UserTitlesManager : BaseManager
{
private readonly UserTitlesOperations _userTitlesOperations;
public UserTitlesManager()
{
_userTitlesOperations = new UserTitlesOperations(base.IUOW);
}
public List<UserTitlesWM> GetAllUserTitles()
{
try
{
return UserTitlesMapping.MaptoWM(_userTitlesOperations.GetAllUserTitles());
}
catch (Exception ex)
{
throw new Exception(ex.ToString());
}
}
}
Finally, this is my API Controller:
[Route("api/LoginRequest")]
public class TitlesController : BaseController
{
UserTitlesManager _userTitlesManager;
public LoginController()
{
_userTitlesManager = new UserTitlesManager();
}
[Route("RetreiveTitles")]
public HttpResponseMessage GetTitles()
{
try
{
return Request.CreateResponse(HttpStatusCode.OK, _userTitlesManager.GetAllUserTitles());
}
catch (Exception ex)
{
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex.ToString());
}
}
}
By the way BaseController is just another API controller which gives inheritance to all other API controllers, and houses a method which is used by all the other controllers.
So, I'm still trying to sharpen my self on this design patterns and would be glad if anyone could show my mistake on BaseManager class. As I said, I suppose the problem is caused by that private readonly XaPaDataContext _context; line. On the other hand,I can't figure out how to corrrect it as my operation classes' constructors are asking for IUnitOfWork.
Thank you in advance!
EDIT:
Just realized that I forgot to share my Unit of Work class:
public class XaPaUnitOfWork : IUnitOfWork
{
private readonly XaPaDataContext _context;
public XaPaUnitOfWork(XaPaDataContext context)
{
_context = context;
Categories = new CategoriesRepository(_context);
OrderDetails = new OrderDetailsRepository(_context);
Orders = new OrdersRepository(_context);
ProductImages = new ProductImagesRepository(_context);
Products = new ProductsRepository(_context);
Users = new UsersRepository(_context);
UserTitles = new UserTitlesRepository(_context);
UserTokens = new UserTokensRepository(_context);
}
public ICategoriesRepository Categories { get; private set; }
public IOrderDetailsRepository OrderDetails { get; private set; }
public IOrdersRepository Orders { get; private set; }
public IProductImagesRepository ProductImages { get; private set; }
public IProductsRepository Products { get; private set; }
public IUsersRepository Users { get; private set; }
public IUserTitlesRepository UserTitles { get; private set; }
public IUserTokensRepository UserTokens { get; private set; }
public int Complete()
{
return _context.SaveChanges();
}
public void Dispose()
{
_context.Dispose();
}
}
After I have changed my BaseManager class as below:
public abstract class BaseManager
{
private IUnitOfWork _iUow;
public IUnitOfWork IUOW
{
get
{
if (_iUow == null)
{
_iUow = new XaPaUnitOfWork(new XaPaDataContext());
}
return _iUow;
}
}
}
I have achived to receive HttpStatusCode.OK
But, honestly, I'm still unsure about the real reason. I make this correction mostly by heart.
Please help me correct this code.
I am having a compiler error in the class
public interface IGenericSaveRepository
{
void Save<TEntity>(int id, ICollection<TEntity> entities) where TEntity : class;
}
public class GenericSaveRepository<TEntity> where TEntity : class,IGenericSaveRepository
{
private IUnitofWork<TEntity> _unitofWork;
private NaijaSchoolsContext _context;
public GenericSaveRepository(NaijaSchoolsContext context)
{
_context = context;
_unitofWork = new UnitofWork<TEntity>(_context);
}
public void Save(int id, ICollection<TEntity> entities)
{
foreach (var entity1 in entities)
{
//entity.Insert(entity1);
_unitofWork.Entity.Insert(entity1);
}
}
}
public class RatingRepo : GenericRepository<Rating>
{
private IGenericSaveRepository gen;
private readonly NaijaSchoolsContext _context;
public RatingRepo(NaijaSchoolsContext context)
: base(context)
{
_context = context;
}
public void Save(School school,Rating rating)
{
List<Rating> ratings = new List<Rating>();
ratings.Add(rating);
gen = new GenericSaveRepository<Rating>(_context);
gen.Save(23, ratings);
}
}
This line gen = new GenericSaveRepository<Rating>(_context); doesn't allow me to have Rating specified as a concrete type.
How can I do this?
Thanks for the help.
This should remove your compile error .. see gen implementation if you don't want Rating must implement IGenericSaveRepository
public class RatingRepo : GenericRepository<Rating>
{
private GenericSaveRepository<Rating> gen;
private readonly NaijaSchoolsContext _context;
public RatingRepo(NaijaSchoolsContext context)
: base(context)
{
_context = context;
}
public void Save(School school, Rating rating)
{
List<Rating> ratings = new List<Rating>();
ratings.Add(rating);
gen = new GenericSaveRepository<Rating>(_context);
gen.Save(23, ratings);
}
}
Update : Complete implementation
public interface IGenericSaveRepository
{
void Save<TEntity>(int id, ICollection<TEntity> entities) where TEntity : class;
}
public class GenericSaveRepository<TEntity> where TEntity : class
{
private UnitofWork<TEntity> _unitofWork;
private NaijaSchoolsContext _context;
public GenericSaveRepository(NaijaSchoolsContext context)
{
_context = context;
_unitofWork = new UnitofWork<TEntity>(_context);
}
public void Save(int id, ICollection<TEntity> entities)
{
foreach (var entity1 in entities)
{
}
}
}
public class UnitofWork<T>
{
public UnitofWork(NaijaSchoolsContext context)
{
throw new NotImplementedException();
}
}
internal interface IUnitofWork<T>
{
}
public class NaijaSchoolsContext
{
}
public class GenericRepository<T>
{
protected GenericRepository(NaijaSchoolsContext context)
{
throw new NotImplementedException();
}
}
public class Rating
{
}
public class School
{
}
Rating must implement IGenericSaveRepository in order for that to compile.
public class GenericSaveRepository<TEntity> where TEntity : class,IGenericSaveRepository
I am using the repository pattern with entity framework 5. When I add a new user to my users entity, it does not get saved to the database. Any ideas why??
I have the following structure -
DAL (contains Entity Framework model) -> Core -> Web
Form1.cs
UserService _userService = new UserService();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
listBox1.DataSource = _userService.GetUserList(10, 1).Users;
listBox1.DisplayMember = "FullName";
}
private void btnAdd_Click(object sender, EventArgs e)
{
UserModel newUser = new UserModel();
newUser.Username = tbUsername.Text;
newUser.FirstName = tbFirstname.Text;
newUser.Surname = tbLastname.Text;
newUser.Password = tbPassword.Text;
newUser.LoginEnabled = true;
newUser.UserStatus = UserStatus.Active;
_userService.Add(newUser);
}
UserService.cs
public void Add(UserModel entity)
{
User newUser = new User();
newUser.DateCreated = DateTime.Now;
AutoMapper.Mapper.CreateMap<UserModel, User>();
try
{
_userRepository.Add(AutoMapper.Mapper.Map(entity, newUser));
}
catch (Exception ex)
{
}
}
RepositoryBase.cs
public abstract class RepositoryBase<T> : IRepository<T>
where T : class
{
public RepositoryBase()
: this(new AcRepositoryContext())
{
}
public RepositoryBase(IRepositoryContext repositoryContext)
{
repositoryContext = repositoryContext ?? new AcRepositoryContext();
_objectSet = repositoryContext.GetObjectSet<T>();
}
private IObjectSet<T> _objectSet;
public IObjectSet<T> ObjectSet
{
get
{
return _objectSet;
}
}
#region IRepository Members
public void Add(T entity)
{
this.ObjectSet.AddObject(entity);
}
public void Delete(T entity)
{
this.ObjectSet.DeleteObject(entity);
}
public IList<T> GetAll()
{
return this.ObjectSet.ToList<T>();
}
public IList<T> GetAll(Expression<Func<T, bool>> whereCondition)
{
return this.ObjectSet.Where(whereCondition).ToList<T>();
}
public T GetSingle(Expression<Func<T, bool>> whereCondition)
{
return this.ObjectSet.Where(whereCondition).FirstOrDefault<T>();
}
public void Attach(T entity)
{
this.ObjectSet.Attach(entity);
}
public IQueryable<T> GetQueryable()
{
return this.ObjectSet.AsQueryable<T>();
}
public long Count()
{
return this.ObjectSet.LongCount<T>();
}
public long Count(Expression<Func<T, bool>> whereCondition)
{
return this.ObjectSet.Where(whereCondition).LongCount<T>();
}
#endregion
}
AcRepositoryContext.cs
public class AcRepositoryContext : IRepositoryContext
{
private const string OBJECT_CONTEXT_KEY = "AC.DAL.AccessControlDBEntities";
public IObjectSet<T> GetObjectSet<T>()
where T : class
{
try
{
return ContextManager.GetObjectContext(OBJECT_CONTEXT_KEY).CreateObjectSet<T>();
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// Returns the active object context
/// </summary>
public ObjectContext ObjectContext
{
get
{
return ContextManager.GetObjectContext(OBJECT_CONTEXT_KEY);
}
}
public int SaveChanges()
{
return this.ObjectContext.SaveChanges();
}
public void Terminate()
{
ContextManager.SetRepositoryContext(null, OBJECT_CONTEXT_KEY);
}
public DbSet<User> Users { get; set; }
public DbSet<Door> Doors { get; set; }
public DbSet<Event> Events { get; set; }
}
In your Repository implementation class (RepositoryBase or its children), you should have a call to the context's SaveChanges.
For instance, in RepositoryBase<T>, I would add this method:
public void Commit()
{
repositoryContext.SaveChanges()
}
After you perform your call to Add() in your btnAdd_Click event handler, you should call this Commit method to save changes to the database.
For more information, take a look at this blog:
Using the Repository Pattern in Entity Framework
Hope this helps!
I can't see in your repository where you call SaveChanges on your AcRepositoryContext I'm assuming this inherits from dbcontext or more likely from your naming convention objectcontext
So after you've added a new entity or updated or deleted then you need to save changes to have those changes persisted to the database.
I can see in your userservice class where you add an entity to the repository but you don't appear to call SaveChanges on the context afterwards.
Say I have the following implementation:
//Generic repository.
public interface IRepository<T> where T : class {
void Insert(T entity);
void Delete(int id);
void SaveChanges();
//..more generic functions
}
//Repository implementation.
public class EFRepository<T> : IRepository<T> where T: class {
private MyDbContext context;
protected DbSet<T> dbSet;
public EFRepository(): this(new MyDbContext()){}
public EFRepository(MyDbContext context)
{
this.context = context;
dbSet = context.Set<T>();
}
public void Insert(T entity)
{
dbSet.Add(entity);
}
public void Delete(int id)
{
dbSet.Remove(dbSet.Find(id));
}
public void SaveChanges()
{
context.SaveChanges();
}
//...more generic implementations
}
//Unit of Work Interface
public interface IUnitOfWork: IDisposable
{
IRepository<EntityA> ARepository { get; }
IRepository<EntityB> BRepository { get; }
//...more stuff
}
//Unit of Work Implementation
public class EFUnitOfWork: IUnitOfWork
{
private MyDbContext context = new MyDbContext();
private IRepository<EntityA> aRepository;
private IRepository<EntityB> bRepository;
public IRepository<EntityA> ARepository
{
get
{
if (this.aRepository == null)
this.aRepository = new EFRepository<EntityA>(context);
return this.aRepository;
}
}
public IRepository<EntityB> BRepository
{
get
{
if (this.bRepository == null)
this.bRepository = new EFRepository<EntityB>(context);
return this.bRepository;
}
}
//...more stuff
}
And finally, I have the following bindings in my resolver:
kernel.Bind(typeof(IRepository<>)).To(typeof(EFRepository<>));
kernel.Bind(typeof(IUnitOfWork)).To(typeof(EFUnitOfWork));
Now, my question is... how would I go about extending the repository for EntityA so that it has more operations than just the generic ones?
I'll post what I have so far in a few...
EDIT: Here's what I have so far:
//New interface.
public class IEntityARepository : IRepository<EntityA>
{
void DoSomethingSpecificToEntityA();
}
//New implementation.
public class EFEntityARepository : EFRepository<EntityA>
{
public EFEntityARepository(MyDbContext context) : base(context) {}
//add additional methods for EntityA
public void DoSomethingSpecificToEntityA()
{
}
}
//Modify Unit of Work Interface as follows.
//Unit of Work Interface
public interface IUnitOfWork: IDisposable
{
IEntityARepository ARepository { get; }
IRepository<EntityB> BRepository { get; }
//...more stuff
}
//Modify Unit of Work Implementation as follows.
public class EFUnitOfWork: IUnitOfWork
{
private MyDbContext context = new MyDbContext();
private IEntityARepository aRepository;
private IRepository<EntityB> bRepository;
public IEntityARepository ARepository
{
get
{
if (this.aRepository == null)
this.aRepository = new EFEntityARepository<EntityA>(context);
return this.aRepository;
}
}
public IRepository<EntityB> BRepository
{
get
{
if (this.bRepository == null)
this.bRepository = new EFRepository<EntityB>(context);
return this.bRepository;
}
}
//...more stuff
}
Add the following binding:
kernel.Bind(typeof(IEntityARepository)).To(typeof(EFEntityARepository));
However, I'm sure this is not correct. Or at least, not the proper way to do it.
If I understand you correctly, you could just derrive from a specifically typed version of your generic class like this...
public class EFEntityARepository : EFRepository<EntityA>, IEntityARepository
{
//Add more opps
}
I think the unit of work needs to look like this:
public IEntityARepository ARepository
{
get
{
if (this.aRepository == null)
this.aRepository = new EFEntityARepository(context);
return this.aRepository;
}
}
OK so I've got it working by adding and/or modifying my original code as follows:
//New interface for the extension of the repository.
//Is it possible to do this without defining this new interface? Doesn't seem like it.
public class IEntityARepository : IRepository<EntityA>
{
void DoSomethingSpecificToEntityA();
}
//Add new class.
//It looks like you have to inherit from IEntityARepository as well.
public class EFEntityARepository : EFRepository<EntityA>, IEntityARepository
{
public EFEntityARepository(MyDbContext context) : base(context) {}
//add additional methods for EntityA
public void DoSomethingSpecificToEntityA()
{
}
}
//Modify Unit of Work Interface as follows.
public interface IUnitOfWork: IDisposable
{
IEntityARepository ARepository { get; }
IRepository<EntityB> BRepository { get; }
//...more stuff
}
//Modify Unit of Work Implementation as follows.
public class EFUnitOfWork: IUnitOfWork
{
private MyDbContext context = new MyDbContext();
private IEntityARepository aRepository;
private IRepository<EntityB> bRepository;
public IEntityARepository ARepository
{
get
{
if (this.aRepository == null)
this.aRepository = new EFEntityARepository(context);
return this.aRepository;
}
}
public IRepository<EntityB> BRepository
{
get
{
if (this.bRepository == null)
this.bRepository = new EFRepository<EntityB>(context);
return this.bRepository;
}
}
//...more stuff
}
It works... but is it the best way to go about it?
I'm have a Base Repository and all Entities repositories inherits from that.
In my testes i create a Fake DbContext and Fake DbSet to test my repositories, but when implementing some methods in my FakeDbContext I'm not able to implement the IDbContext.Entry method:
public class FakeDbContext : IDbContext
{
private IDbSet<Usuario> _usuario;
private IDbSet<Atividade> _atividade;
private IDbSet<Autor> _autor;
private IDbSet<CategoriaModulo> _categoriaModulo;
private IDbSet<CategoriaMateria> _categoriaMateria;
private IDbSet<Site> _site;
private IDbSet<Modulo> _modulo;
private IDbSet<Perfil> _perfil;
private IDbSet<CategoriaGaleriaImagem> _categoriaGaleriaImagem;
public IDbSet<Usuario> Usuario { get { return _usuario ?? (_usuario = new FakeDbSet<Usuario>()); } set { } }
public IDbSet<Atividade> Atividade { get { return _atividade ?? (_atividade = new FakeDbSet<Atividade>()); } set { } }
public IDbSet<Autor> Autor { get { return _autor ?? (_autor = new FakeDbSet<Autor>()); } set { } }
public IDbSet<CategoriaModulo> CategoriaModulo { get { return _categoriaModulo ?? (_categoriaModulo = new FakeDbSet<CategoriaModulo>()); } set { } }
public IDbSet<CategoriaMateria> CategoriaMateria { get { return _categoriaMateria ?? (_categoriaMateria = new FakeDbSet<CategoriaMateria>()); } set { } }
public IDbSet<Site> Site { get { return _site ?? (_site = new FakeDbSet<Site>()); } set { } }
public IDbSet<Modulo> Modulo { get { return _modulo ?? (_modulo = new FakeDbSet<Modulo>()); } set { } }
public IDbSet<Perfil> Perfil { get { return _perfil ?? (_perfil = new FakeDbSet<Perfil>()); } set { } }
public IDbSet<CategoriaGaleriaImagem> CategoriaGaleriaImagem { get { return _categoriaGaleriaImagem ?? (_categoriaGaleriaImagem = new FakeDbSet<CategoriaGaleriaImagem>()); } set { } }
public void SaveChanges()
{
//do nothing
}
public IDbSet<TEntity> Set<TEntity>() where TEntity : class
{
foreach (PropertyInfo property in typeof(FakeDbContext).GetProperties())
{
if (property.PropertyType == typeof(IDbSet<TEntity>))
return property.GetValue(this, null) as IDbSet<TEntity>;
}
throw new Exception("Type collection not found");
}
public System.Data.Entity.Infrastructure.DbEntityEntry Entry<TEntity>(TEntity entity) where TEntity : class
{
}
}
The last method I'm not able to implementing, can you guys help me?
I'm using this Entry method to update a Entity in my base repository:
public abstract class BaseRepository<TEntity> : IBaseRepository<TEntity> where TEntity : class
{
#region Fields
protected TEntity EntityType;
protected IDbSet<TEntity> DbSet;
#endregion
#region Properties
public IDbContext DbContext
{
get
{
return DbContextFactory.Instance.GetOrCreateContext();
}
}
#endregion
#region Constructors
protected BaseRepository()
{
this.EntityType = DependencyResolverFactory.Instance.Get<TEntity>();
this.DbSet = DbContext.Set<TEntity>();
}
#endregion
#region Methods
public virtual void Add(TEntity entity)
{
this.DbSet.Add(entity);
}
public virtual void Remove(TEntity entity)
{
this.DbSet.Remove(entity);
}
public virtual void RemoveById(object id)
{
TEntity entity = this.GetById(id);
this.DbSet.Remove(entity);
}
public virtual void Edit(TEntity entity)
{
this.DbContext.Entry(entity).State = EntityState.Modified;
}
public virtual TEntity GetById(object id)
{
return (TEntity)this.DbSet.Find(id);
}
public virtual IList<TEntity> GetAll()
{
return ((IEnumerable<TEntity>)this.DbSet).ToList();
}
#endregion
}
Read this and all linked questions before you continue. Unit testing anything returning EF related classes or working with linq-to-entities is dangerous.
Give up with unit testing your repositories and instead unit test your application logic by faking repositories themselves. If you want to test your repositories create integration tests talking to the real database.