I have a problem with Microsoft Unity, very similar as in this post here, but none of answers was useful for me. I have solution, which consists from couple of projects. In first important project Bootstrap I have UnityContainer defined as bellow:
public static class Bootstrap
{
private static readonly IUnityContainer _unityContainer;
static Bootstrap()
{
_unityContainer = new UnityContainer();
_unityContainer.RegisterType(typeof (IRepository<>), typeof (Ef6Repository<>));
_unityContainer.RegisterType<IUnitOfWork, Ef6UnitOfWork>();
}
public static IUnityContainer GetConfiguredContainer()
{
return _unityContainer;
}
}
Problem is on line _unityContainer.RegisterType(typeof (IRepository<>), typeof (Ef6Repository<>));, where Unity says that there cannot resolve method 'RegisterType(System.Type, System.Type)'. But this line is correct as described in MSDN. It was compilable and working few days ago. I think that problem can be somewhere in my usage of Ef6UnitOfWork or Ef6Repository.
Can anyone help? I cant figure out what I am doing wrong. Version of Unity is 3.5
Bellow is my code from second project "Repository" in solution.
public interface IRepository<T>
{
T GetById(Guid id);
IQueryable<T> GetAll();
IEnumerable<T> Find(Expression<Func<T, bool>> predicate);
ApplicationUserManager GetUserManager();
ApplicationRoleManager GetRoleManager();
}
public class Ef6Repository<T> : IRepository<T> where T : class
{
public DbContext context;
public DbSet<T> dbset;
private readonly ApplicationUserManager _userManager;
private readonly ApplicationRoleManager _roleManager;
public Ef6Repository(DbContext context)
{
this.context = context;
dbset = context.Set<T>();
}
public Ef6Repository(DbContext context, ApplicationUserManager userManager)
{
this.context = context;
dbset = context.Set<T>();
_userManager = userManager;
}
public Ef6Repository(DbContext context, ApplicationRoleManager roleManager)
{
this.context = context;
dbset = context.Set<T>();
_roleManager = roleManager;
}
public T GetById(Guid id)
{
return dbset.Find(id);
}
public IQueryable<T> GetAll()
{
return dbset;
}
public IEnumerable<T> Find(Expression<Func<T, bool>> predicate)
{
return dbset.Where(predicate);
}
public ApplicationUserManager GetUserManager()
{
return _userManager;
}
public ApplicationRoleManager GetRoleManager()
{
return _roleManager;
}
}
public interface IUnitOfWork : IDisposable
{
IRepository<Person> PersonRepository { get; }
IRepository<ApplicationUser> ApplicationUserRepository { get; }
IRepository<IdentityRole> ApplicationRoleRepository { get; }
void Save();
}
public partial class Ef6UnitOfWork : IUnitOfWork
{
private IRepository<Person> _personRepository;
private IRepository<ApplicationUser> _applicationUserRepository;
private IRepository<IdentityRole> _applicationRoleRepository;
private Context _context;
public Ef6UnitOfWork()
{
_context = new Context();
}
public IRepository<Person> PersonRepository
{
get
{
if (_personRepository == null)
_personRepository = new Ef6Repository<Person>(_context);
return _personRepository;
}
}
public IRepository<ApplicationUser> ApplicationUserRepository
{
get
{
if (_applicationUserRepository == null)
_applicationUserRepository = new Ef6Repository<ApplicationUser>(_context,
new ApplicationUserManager(new UserStore<ApplicationUser>(_context))
);
return _applicationUserRepository;
}
}
public IRepository<IdentityRole> ApplicationRoleRepository
{
get
{
if (_applicationRoleRepository == null)
_applicationRoleRepository = new Ef6Repository<IdentityRole>(_context,
new ApplicationRoleManager(new RoleStore<IdentityRole>(_context))
);
return _applicationRoleRepository;
}
}
public void Save()
{
_context.SaveChanges();
}
#region IDisposable
private bool disposed = false;
protected virtual void Dispose(bool disposing)
{
if (!this.disposed)
{
if (disposing)
{
_context.Dispose();
}
}
this.disposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
}