简体   繁体   English

我需要与Ninject绑定到数据库并将它们同时注入到相同的存储库中

[英]I need to bind with Ninject to database and inject them to same repository in same time

Here some parts of my code class NinjectWebCommon place where I need bind dada contexts. 在我的代码类NinjectWebCommon某些部分中,我需要绑定dada上下文。 It is just piece of the code, not complete classes. 这只是一部分代码,而不是完整的类。

private static void RegisterServices(IKernel kernel)
        {
            kernel.BindSharpRepository();
            RepositoryDependencyResolver.SetDependencyResolver(new NinjectDependencyResolver(kernel));

            kernel.Bind<DbContext>().To<EntitiesDbOne>().InRequestScope();

        //kernel.Bind<DbContext>().To<EntitiesDbTwo>().InRequestScope();
    }  

Category Repository class where i need to have two databases in same time 类别存储库类,我需要同时拥有两个数据库

public class CategoryRepository : ConfigurationBasedRepository<CategoryData, int>, ICategoryRepository
    {
    private readonly EntitiesDbOne _ctxOne;
        private readonly EntitiesDbTwo _ctxTwo;

    public CategoryRepository(EntitiesDbOne ctxOne, EntitiesDbTwo ctxTwo)
        {
            _ctxOne= ctxOne;
            _ctxTwo= ctxTwo;
        }

    public CategoryData GetById(int Id)
        {
             //dummy data, just for usage two different dcContexts
             var category = _ctxOne.Categories.Include((string) (x => x.MetaTags)).FirstOrDefault(x => x.Id == Id);
             var categoryName = _ctxTwo.category.FirstOrDefault(x => x.Id == category.Id);

            return category;
        }

In my project I use SharpRepository(ConfigurationBasedRepository) and I use UnitOfWork. 在我的项目中,我使用SharpRepository(ConfigurationBasedRepository),而我使用UnitOfWork。 I think I can skip UnitOfWork because EntityFramework is doing all this (UnitOfWork patterns) job. 我想我可以跳过UnitOfWork,因为EntityFramework正在完成所有这些(UnitOfWork模式)工作。 Booth of databases is EntityFramework one is CodeFirst approach other (DbTwo) modelFirst 数据库的展位是EntityFramework的一个是CodeFirst方法的另一个(DbTwo)模型的

public class EntitiesDbOne : DbContext
    {
        public DbSet<CategoryData> Categories { get; set; }
}

public partial class EntitiesDbTwo: DbContext
    {
        public EntitiesDbTwo()
            : base("name=EntitiesDbTwo")
        {
        }

        public DbSet<attributenames> attributenames { get; set; }
        public DbSet<category> category { get; set; }
}

Please give me some links to examples I can use, I do not think I can manage this with simple explanation. 请给我一些我可以使用的示例的链接,我认为我不能通过简单的解释来解决这个问题。 Before I wrote this question, I had search for the answer here on the Multiple DbContexts in N-Tier Application 在我写这个问题之前,我已经在N层应用程序中多个DbContexts中搜索了答案。

EF and repository pattern - ending up with multiple DbContexts in one controller - any issues (performance, data integrity)? EF和存储库模式-在一个控制器中最终包含多个DbContext-是否存在任何问题(性能,数据完整性)?

This question by the name is complete in my situation but the code for me, is far different. 这个名字的问题在我的情况下是完整的,但对于我来说,代码却大不相同。 Multiple dbcontexts with Repository, UnitOfWork and Ninject 带有存储库,UnitOfWork和Ninject的多个dbcontexts

This one is multiple databases but use one per request, and I need two databases in the same request. 这个是多个数据库,但是每个请求使用一个数据库,而我在同一请求中需要两个数据库。 http://blog.staticvoid.co.nz/2012/1/9/multiple_repository_data_contexts_with_my_repository_pattern site and other place. http://blog.staticvoid.co.nz/2012/1/9/multiple_repository_data_contexts_with_my_repository_pattern网站等。

I read about 20 suggestions, there are two close to my situation but probably not enough. 我阅读了大约20条建议,其中有2条与我的情况很接近,但可能还不够。

just as a basis for discussion: 只是作为讨论的基础:

What if you use: 如果使用:

kernel.Bind<EntitiesDbOne>().ToSelf().InRequestScope();
kernel.Bind<EntitiesDbTwo>().ToSelf().InRequestScope();

and use it like 并像这样使用

public class CategoryRepository : ConfigurationBasedRepository<CategoryData, int>,    ICategoryRepository
{

    public CategoryRepository(EntitiesDbOne ctxOne, EntitiesDbTwo ctxTwo)
    {
    }
}

what's the issue then? 那是什么问题呢?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM