MVC3 + Ninject + Entity 4 Structure

I have this dependency converter

public class NinjectDependencyResolvercs : IDependencyResolver
    {
        private readonly IResolutionRoot resolutionRoot;
        public NinjectDependencyResolvercs(IResolutionRoot kernel)
        {
            resolutionRoot = kernel;
        }

        public object GetService(Type serviceType)
        {
            return resolutionRoot.TryGet(serviceType);
        }

        public IEnumerable<object> GetServices(Type serviceType)
        {
            return resolutionRoot.GetAll(serviceType);
        }
    }

in global.asax.cs

// Ninject DI container ----------------------------------------------------------- |
        public void SetupDependencyInjection()
        {
            // Create Ninject DI kernel
            IKernel kernel = new StandardKernel();

            #region Register services with Ninject DI Container

            // DbContext to SqlDataContext
            kernel.Bind<DbContext>()
                    .To<SqlDataContext>();

            // IRepository to SqlRepository
            kernel.Bind<IRepository>()
                    .To<SqlRepository>();

            // IUsersServices to UsersServices
            kernel.Bind<IUsersServices>()
                    .To<UsersServices>();

            // IMessagesServices to MessagesServices
            kernel.Bind<IMessagesServices>()
                    .To<MessagesServices>();

            // IJobAdvertsServices to JobAdvertsServices
            kernel.Bind<IJobAdvertsServices>()
                    .To<JobAdvertsServices>();

            #endregion

            // Tell ASP.NET MVC 3 to use Ninject DI Container
            DependencyResolver.SetResolver(new NinjectDependencyResolvercs(kernel));
        }
        // --------------------------------------------------------------------------------- |

and class

public class SqlDataContext : DbContext
    {
        public DbSet<User> Users { get; set; }
        public DbSet<Profile> Profiles { get; set; }
        public DbSet<Role> Roles { get; set; }
        public DbSet<JobAdvert> JobAdverts { get; set; }
        public DbSet<Message> Messages { get; set; }

        protected override void OnModelCreating(System.Data.Entity.ModelConfiguration.ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<User>().HasMany(x => x.Roles).WithMany(x => x.Users).Map(x =>
            {
                x.MapLeftKey(y => y.UserId, "UserId");
                x.MapRightKey(y => y.RoleId, "RoleId");
                x.ToTable("UsersInRoles");
            });

            base.OnModelCreating(modelBuilder);
        }
    }

all dependecies functions work fine, but for DbContext for SqlDataContext this is a problem. If you use this:

public class SqlRepository
{
    private DbContext dataContext;
    public SqlRepository(DbContext dataContext) {
        this.dataContext = dataContext;
    }

    public DbSet<User> Users {
        get {
            return dataContext.Users;
        }
    }
}

then

dataContext.Users

and all other properties warn about this error:

'System.Data.Entity.DbContext' does not contain a definition for 'JobAdverts' and no extension method 'JobAdverts' accepting a first argument of type 'System.Data.Entity.DbContext' could be found (are you missing a using directive or an assembly reference?)   

Does anyone have an idea why the DI doent works for the DbContext class?

+3
source share
1 answer

If I understand correctly, you are introducing a DbContext that does not have these methods / properties, as they are declared in the derived type SqlDataContext. You need to enter SqlDataContext. If you want to use the interface, you need to extract the interface from SqlDataContext.

EDIT:

Ninject , , ( ), . , , .

public class SqlRepository
{
    private dynamic dataContext;
    public SqlRepository(DbContext dataContext) {
        this.dataContext = dataContext;
    }
    ...
}

, SqlDataContext:

public class SqlRepository
{
 private SqlDataContextdata Context;
    public SqlRepository(SqlDataContextdata Context) {
        this.dataContext = dataContext;
    }
  ...
}

DbContext , SqlContext. sqlcontext DbContext .

+4

Source: https://habr.com/ru/post/1793913/


All Articles