I don't know if the following is weird but actually need an interface for an interface< T > in order to store it in a List without specify a specific implementation.
Example:
public interface IRepository<T>
{
void Add(T newEntity);
void Remove(T entity);
IEnumerable<T> FindAll();
IEnumerable<T> Find(Expression<Func<T, bool>> predicate);
}
public interface IUnitOfWork
{
//Here i would like to store just a IRepository without implementation
IList<IRepository<**I have to specify implementation here**>> Repositories { get; set; }
bool Commit();
}
You are free to suggest me better ways to do that. This is only what i have though to do...
Thanks
EDIT
I cannot provide a non-generic interface because i'm using it like this:
public class GenericRepository<T> : IRepository<T>
{
...generic repository methods...
}
//Specific repository...
public class MyTableClassRepository<MyTable> : GenericRepository<MyTable>
{
public MyTableClassRepository(Database context) : base(context)
{
}
}
Consider the following design
public interface IUnitOfWork : IDisposable
{
T GetRepository<T>() where T : class;
void SaveChanges();
}
In the implementation of the UnitOfWork you can use IoC container (Autofac in the example below)
public class UnitOfWork : IUnitOfWork
{
private static IContainer _container;
Hashtable _repositories = new Hashtable();
public static Module CurrentRepositoriesModule { get; set; }
public UnitOfWork()
{
var builder = new ContainerBuilder();
if (CurrentRepositoriesModule != null)
builder.RegisterModule(CurrentRepositoriesModule);
_container = builder.Build();
}
public T GetRepository<T>() where T : class
{
var targetType = typeof(T);
if (!_repositories.ContainsKey(targetType))
{
_repositories.Add(targetType, _container.Resolve<T>());
}
return (T)_repositories[targetType];
}
public void SaveChanges()
{
throw new NotImplementedException();
}
}
Related
I want to add TWO DbContext in My ASP.NET MVC 5 App, One DbContext For ASPIdentity and The Other For My APP DB. I am Using Repository Pattern.
m y problem is, How to To specify the Entity of each DbContext in BaseRepository ?
Here Is What I did.
1- DatabaseFactory & IDatabaseFactory
public class DatabaseFactory<T> where T : DbContext,new()
{
private T dbContext;
public T Init()
{
return dbContext ?? (dbContext = new T());
}
}
public interface IDatabaseFactory<T> where T : DbContext
{
T Init();
}
2- IUnitOfWork & UnitOfWork
public class UnitOfWork<T> : IUnitOfWork<T> where T : DbContext
{
private readonly IDatabaseFactory<T> dbFactory;
private T dbContext;
public UnitOfWork(IDatabaseFactory<T> dbFactory)
{
this.dbFactory = dbFactory;
}
protected T DbContext
{
get { return dbContext ?? (dbContext = dbFactory.Init()); }
}
public void Commit()
{
DbContext.SaveChanges();
}
}
public interface IUnitOfWork<T> where T : DbContext, IDisposable
{
void Commit();
}
3- BaseRepository.cs
public abstract class BaseRepository<T> where T : class
{
#region Properties
private DbContext dataContext;
private readonly IDbSet<T> dbSet;
protected IDatabaseFactory DbFactory
{
get;
private set;
}
protected DbContext dbContext
{
get { return dataContext ?? (dataContext = DbFactory.Init()); }
}
#endregion
protected BaseRepository(IDatabaseFactory dbFactory)
{
this.DbFactory = dbFactory;
this.dbSet = this.DbContext.Set<T>();
}
#region Implementation
public virtual void Add(T entity)
{
dbSet.Add(entity);
}
public virtual void Update(T entity)
{
dbSet.Attach(entity);
dataContext.Entry(entity).State = EntityState.Modified;
}
public virtual void Delete(T entity)
{
dbSet.Remove(entity);
}
public virtual void Delete(Expression<Func<T, bool>> where)
{
IEnumerable<T> objects = dbSet.Where<T>(where).AsEnumerable();
foreach (T obj in objects)
dbSet.Remove(obj);
}
public virtual T GetById(int id)
{
return dbSet.Find(id);
}
public virtual IEnumerable<T> GetAll()
{
return dbSet.ToList();
}
public virtual IEnumerable<T> GetMany(Expression<Func<T, bool>> where)
{
return dbSet.Where(where).ToList();
}
public T Get(Expression<Func<T, bool>> where)
{
return dbSet.Where(where).FirstOrDefault<T>();
}
#endregion
}
I'm also trying to implement generic repository pattern but without UOW.
For creating two DbContext you should add one more type in Base Repository.
Also creation logic for DbFactory should be in UOW only not in BaseRepository.
Here's simplified code for you. And be more specific about what you tried.
2- IUnitOfWork & UnitOfWork
public class UnitOfWork<T1, T2> : IUnitOfWork<T1, T2> where T1 : DbContext where T2 : DbContext {
// FOr DbFactories
private readonly IDatabaseFactory<T1> _dbFactory1;
private readonly IDatabaseFactory<T2> _dbFactory2;
//For Seperate DbContexes
private T _dbContext1;
private T _dbContext2;
public UnitOfWork () {
_dbFactory1 = new DatabaseFactory<T1> ();
_dbFactory2 = new DatabaseFactory<T2> ();
}
//For Accessiong DbContext Objects in Base Repository
protected T DbContext1 {
get { return _dbContext1 ?? (_dbContext1 = _dbFactory1.Init ()); }
}
protected T DbContext2 {
get { return _dbContext2 ?? (_dbContext2 = _dbFactory2.Init ()); }
}
public void Commit () {
DbContext1.SaveChanges ();
DbContext2.SaveChanges ();
}
}
public interface IUnitOfWork<T1, T2> where T1 : DbContext where T2 : DbContext, IDisposable {
void Commit ();
}
}
3 - BaseRepository and Example
public abstract class BaseRepository<T1,T2,T> : IUnitOfWork<T1, T2> where T : class where T1 : DbContext where T2 : DbContext {
#region Properties
// private DbContext _dataContext1; //for first DbContext
// private DbContext _dataContext1; //for second DbContext
private readonly IDbSet<T> _dbSet1; //Going to Perform Operations using Dbsets
private readonly IDbSet<T> _dbSet2;
//For Exposing DbContext to respective Implementing Repositories This is Optional
protected DbContext DataContext1 {
get { return DbContext1; } //retuning DbCOntext Object Created in UOW class
}
protected DbContext DataContext2 {
get { return DbContext2; }
}
//For Exposing DbSets to respective Implementing Repositories This is Optional
protected IDbSet<T> DbSet1 => _dbSet1;
protected IDbSet<T> DbSet2 => _dbSet2;
protected BaseRepository () {
_dbSet1 = DataContext1.Set<T> ();
//OR
_dbSet2 = DataContext2.Set<T> ();
}
#endregion
#region Implementation
#endregion
}
//SPecific Repository Example using Person Class
public class PersonRepository:BaseRepository<AspIdentityDbContext,AppDbContext,Person> {
//can use DbContexes from BaseRepository to write Additional Methods/Queries On dbSets
}
Try this and give feedback.
How tell to Unity.WebApi dependency injection framework, inject the correct class in the correct controller?
DI Project Container
public class UnityContainerConfig
{
private static IUnityContainer _unityContainer = null;
public static IUnityContainer Initialize()
{
if (_unityContainer == null)
{
_unityContainer = new Microsoft.Practices.Unity.UnityContainer()
.RegisterType<IMyInterface, MyClass1>("MyClass1")
.RegisterType<IMyInterface, MyClass2>("MyClass2")
}
}
-MVC PROJECT-
public static class UnityConfig
{
public static void RegisterComponents()
{
GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver(DependencyInjection.UnityContainer.UnityContainerConfig.Initialize());
}
}
Controller 1:
private IMyInterface _myInterface
///MyClass1
public XController(
IMyInterface myInterface
)
{
_myInterface = myInterface
}
Controller 2:
private IMyInterface _myInterface
///MyClass2
public YController(
IMyInterface myInterface
)
{
_myInterface = myInterface
}
Rather than using a strategy or facade to solve this, a better solution would be to redesign your interfaces to be unique per controller. Once you have a unique interface type, your DI container will automatically inject the right service into each controller.
Option 1
Use a generic interface.
public interface IMyInterface<T>
{
}
public class XController
{
private readonly IMyInterface<XClass> myInterface;
public XController(IMyInterface<XClass> myInterface)
{
this.myInterface = myInterface;
}
}
public class YController
{
private readonly IMyInterface<YClass> myInterface;
public YController(IMyInterface<YClass> myInterface)
{
this.myInterface = myInterface;
}
}
Option 2
Use interface inheritance.
public interface IMyInterface
{
}
public interface IXMyInterface : IMyInterface
{
}
public interface IYMyInterface : IMyInterface
{
}
public class XController
{
private readonly IXMyInterface myInterface;
public XController(IXMyInterface myInterface)
{
this.myInterface = myInterface;
}
}
public class YController
{
private readonly IYMyInterface myInterface;
public YController(IYMyInterface myInterface)
{
this.myInterface = myInterface;
}
}
I am using the DbContextScope described here
In his example of how to get a hold of a dbcontext outside of the class its instantiated, Mehdi writes:
public class UserRepository : IUserRepository {
private readonly IAmbientDbContextLocator _contextLocator;
public UserRepository(IAmbientDbContextLocator contextLocator)
{
if (contextLocator == null) throw new ArgumentNullException("contextLocator");
_contextLocator = contextLocator;
}
public User Get(Guid id)
{
return _contextLocator.Get<MyDbContext>.Set<User>().Find(id);
}
}
But, if im going for a generic repository, say
public abstract class RepositoryBase<T> : IRepository<T> where T : class, IDomainEntity
{
private readonly DbSet<T> set;
private IAmbientDbContextLocator contextLocator;
protected RepositoryBase(IAmbientDbContextLocator ctxLocator)
{
if (ctxLocator == null) throw new ArgumentNullException(nameof(ctxLocator));
contextLocator = ctxLocator;
}
public T Get(Guid id)
{
//return _contextLocator.Get<MyDbContext>.Set<T>().Find(userId);
}
}
then how is the dbset supposed to be resolved? how do i work with "MyDbContext" in the Get method?
i do have multiple contexts.
public abstract class RepositoryBase<T, TDbContext> : IRepository<T> where T : IDomainEntity where TDbContext : DbContext
{
private readonly DbSet<T> _dbset;
private readonly IAmbientDbContextLocator _contextLocator;
protected RepositoryBase(IAmbientDbContextLocator ctxLocator)
{
if (ctxLocator == null) throw new ArgumentNullException(nameof(ctxLocator));
_contextLocator = ctxLocator;
_dbset = _contextLocator.Get<TDbContext>.Set<T>();
}
protected DbSet<T> DbSet { get { return _dbset; } }
public T Get(Guid id)
{
return DbSet.Find(id);
}
}
If you don't want TDbContext, You can send DbContext on constructor beside of contextlocator. But he forces you to use DbContextScope, I didn't read all article but let's not break his logic.
I have this class where I'm trying to inject a list of qualifying objects:
public class BlingDispatcher : IBlingDispatcher
{
readonly IEnumerable<IDomainEventHandler> _domainEventHandlers;
#region IBlingDispatcher Members
public BlingDispatcher(IEnumerable<IDomainEventHandler> domainEventHandlers)
{
_domainEventHandlers = domainEventHandlers;
}
}
Classes like this get injected here and work great:
public class NotifyFrontEndSomethingHappened : IDomainEventHandler<SomethingHappened>
{
private readonly IFrontEndNotifier _frontEndNotifier;
public NotifyFrontEndAfterSomethingHappened(IFrontEndNotifier frontEndNotifier)
{
_frontEndNotifier = frontEndNotifier;
}
}
Classes like this do not:
public class NotifyFrontEndAfterEvent : IDomainEventHandler<SomethingHappened>,
IDomainEventHandler<SomethingElseHappened>,
IDomainEventHandler<MoreThingsHappened>,
IDomainEventHandler<AndYetMoreThings>
{
readonly IFrontEndNotifier _frontEndNotifier;
public void Handle(SomethingHappened #event)
{
_frontEndNotifier.Notify(#event, #event.CommanderId);
}
...
}
How can I get classes with multiple interfaces to be injected by autofac as well?
EDIT
More information:
public interface IDomainEventHandler
{
}
public interface IDomainEventHandler<in T> : IBlingHandler<T>, IDomainEventHandler
{
}
public interface IBlingHandler<in T>
{
void Handle(T #event);
}
Registering like this in bootstapper:
container.RegisterAssemblyTypes(AppDomain.CurrentDomain.GetAssemblies())
.Where(x => x.GetInterfaces().Any(i => i.Name.StartsWith("IBlingHandler")))
.AsImplementedInterfaces();
I'm using CQRS pattern in my recent project, so I defined some Commands that I call them CommandParameter and CommandHandlers.
For CommandParameters I have these Classes and Interfaces:
public interface ICommandParameter
{
}
public abstract class BaseEntityCommandParameter<T> : IAggregateRoot,ICommandParameter
where T : ModelEntitySuperType, new()
{
public T Entity { get; set; }
protected BaseEntityCommandParameter()
{
Entity = new T();
}
}
public class InsertCommandParameter<T> : BaseEntityCommandParameter<T>
where T : class, new()
{
}
And for CommandHandlers I defined these Classes and Interfaces:
public interface ICommandHandler<TCommandParameter>
where TCommandParameter :ICommandParameter
{
void Handle(TCommandParameter parameter);
string CommandCode { get; }
}
public class InsertCommandHandler<TCommandParameter, TEntity>
: ICommandHandler<TCommandParameter>
where TCommandParameter : BaseEntityCommandParameter<TEntity>, new()
where TEntity : ModelEntitySuperType, IAggregateRoot, new()
and I used them to make appropriate CommandParameters and CommandHandlers for each Entity for example for Order I have:
public class OrderInsertCommandParameter:InsertCommandParameter<Order>
{
}
public class OrderInsertCommandHandler
: InsertCommandHandler<OrderInsertCommandParameter, Order>
{
private readonly IUnitOfWorkFactory _factory;
public OrderInsertCommandHandler(IUnitOfWorkFactory factory,
IRepository<Order> repository)
: base(repository)
{
_factory = factory;
}
public override void Handle(OrderInsertCommandParameter parameter)
{
var uow = _factory.Create();
parameter.Entity.OrderCreationTime = DateTime.Now;
base.Handle(parameter);
uow.Commit();
}
}
I want to register these CommandParameters and appropriate CommandHandlers using structuremap automatically, How could I define a custom Conversion to do this?
The following should do the trick:
container.Configure(r =>
{
r.Scan(s =>
{
s.Assembly(typeof(ICommandHandler<>).Assembly);
s.ConnectImplementationsToTypesClosing(typeof(ICommandHandler<>));
});
});