Click here to Skip to main content
15,887,416 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm using Generic Repository to interact with Database(I'm using EF6.0)

I'm using two Databases.

Ninject has been using to implement DI.

Sample code snippet below.
1. Generic Repository
C#
/// Genertic Repository absract class with class and context
  /// </summary>
  /// <typeparam name="C"></typeparam>
  /// <typeparam name="T"></typeparam>
  public class   GenericRepository<T> :
  IGenericRepository<T>
      where T : class
  {

      public GenericRepository(DbContext DbContext)
      {
          _entities = DbContext;
      }

      private DbContext _entities;



2. DB Context (I want to introduce one more DBContext for another Database)
C#
/// <summary>
   /// Administrative context
   /// </summary>
   public class DatabaseContext: DbContext
   {
       /// <summary>
       /// Reference Code Value DataSet
       /// </summary>
       public DbSet<ReferenceCodeValue> ReferenceValues { get; set; }
       /// <summary>
       /// Reference Code Type DataSet
       /// </summary>
       public DbSet<ReferenceDataType> ReferenceTypes { get; set; }
       ///<summary>
       /// Static Constructor
       /// </summary>
       static DatabaseContext()
       {
           Database.SetInitializer<DatabaseContext>(null);
       }


3. Register the DB Context using Ninject
C#
internal static class DependencyRegistry
    {
        [DependencyRegistrar]
        public static void Register(Dependency Container)
        {
            Container.
            RegisterPerRequestScopeType<DbContext, DatabaseContext>();
        }
    };


4. Domain Service-where I'm invoking the generic repository through constructor injection

C#
/// Concrete Class AdministrativeManager which implements IAdministrativeManager
    /// </summary>
    internal class AdministrationDomain : DomainElement, IAdministrationDomain
    {
        private IGenericRepository<DTO.ReferenceDataType> _referenceTypeRepo;
        private IGenericRepository<DTO.ReferenceCodeValue> _referenceValueRepo;

        public AdministrationDomain(
            IGenericRepository<DTO.ReferenceDataType> referenceTypeRepo,
            IGenericRepository<DTO.ReferenceCodeValue> referenceValueRepo)
        {
            _referenceTypeRepo = referenceTypeRepo;
            _referenceValueRepo = referenceValueRepo;
        }



I want to use the same IGenericRepository with two implementations one for Database1 and another one Database2.

I tried Named binding to register
C#
Container.
            RegisterPerRequestScopeType&lt;DbContext, DatabaseContext&gt;().Named("DB1";

C#
Container.
            RegisterPerRequestScopeType&lt;DbContext, AppContext&gt;().Named("DB2");



the DatabaseContext has been overriden by AppContext. how I can resolve the issue?
Posted

1 solution

thanks for your support. Resolved the issue using Contextual Binding in Ninject
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900