如何将实体框架DbContext注入到SharpRepository的ConfigurationBasedRepository中 [英] How-to inject the Entity Framework DbContext into the ConfigurationBasedRepository of SharpRepository

查看:157
本文介绍了如何将实体框架DbContext注入到SharpRepository的ConfigurationBasedRepository中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的很想将 SharpRepository

I really would like to use SharpRepository together with Ninject, but I do not understand how to configure Ninject to share the Entity Framework DbContext between the repositories.

我正在使用Entity Framework版本5和Ninject版本3.

I am using Entity Framework version 5 and Ninject version 3.

当前,我在源代码中使用Ef5Repository,但是我想将其替换为ConfigurationBasedRepository.但是我不知道如何将EF DbContext传递(或注入)到存储库中.

Currently I am using Ef5Repository in my source code, but I want to replace it with ConfigurationBasedRepository. But I cannot figure out how to pass (or inject) the EF DbContext to the repositories.

示例(当前状态):

using SharpRepository.Repository;

public interface IProductRepository : IRepository<Product>
{
}

using SharpRepository.Ef5Repository;
using System.Data.Entity;

// TODO Tightly coupled to Ef5Repository.
public class ProductRepository : Ef5Repository<Product>, IProductRepository
{
    // TODO The DbContext has to be injected manually.
    public ProductRepository(DbContext context) : base(context)
    {
    }

    // [...]
}

目标:

using SharpRepository.Repository;

public interface IProductRepository : IRepository<Product>
{
}

public class ProductRepository : ConfigurationBasedRepository<Product, int>, IProductRepository
{
    // [...]
}

我已经阅读了两篇博客文章 SharpRepository:入门 SharpRepository:配置,但是由于:

I've already read the two blog posts SharpRepository: Getting Started and SharpRepository: Configuration, but they both do not help me, since:

  1. 使用的DIC是StructureMap,而不是Ninject.
  2. 源代码示例不完整(例如,使用未声明的变量).

所以我的问题是:有人可以为我提供一些源代码示例如何实现上述目标(在扩展ConfigurationBasedRepository的所有存储库之间共享一个Entity Framework DbContext实例)吗?

So my question: Can someone provide me with some source code example how-to to achieve the goal described above (sharing one Entity Framework DbContext instance between all repositories extending ConfigurationBasedRepository)?

推荐答案

首先,您需要安装

First, you will need to install the SharpRepository.Ioc.Ninject NuGet package. There are extension methods in here for hooking up Ninject to handle the loading a generic repository and setting the dependency resolver that SharpRepository uses.

无论您要在哪里设置Ninject绑定规则(对kernel.Bind 的所有调用),都需要添加:

Where ever you are setting up your Ninject binding rules (all the calls to kernel.Bind<>), you will need to add:

kernel.BindSharpRepository();

接下来,您需要在Global.asax或App_Start代码或Bootstrapper逻辑(无论您在哪里调用应用程序启动代码)中,添加以下内容:

Next, in your Global.asax, or App_Start code, or your Bootstrapper logic (where ever you are calling application startup code) you will need to add the following:

// kernel is the specific kernel that you are setting up all the binding for
RepositoryDependencyResolver.SetDependencyResolver(new NinjectDependencyResolver(kernel));

这将告诉SharpRepository在获取新的DbContext时使用此Ninject内核.

This will tell SharpRepository to use this Ninject Kernel when getting a new DbContext.

最后要做的是为DbContext本身设置绑定规则.如果您在Web应用程序中,则很可能希望DbContext的范围是每个请求的范围.我个人不使用Ninject,但是我找到了使用 InRequestScope 的参考.我相信您的代码将如下所示:

The last thing to do is to setup the rules for binding for the DbContext itself. If you are in a web application you will most likely want the scope of the DbContext to be per request. I personally don't use Ninject but I found this reference for using InRequestScope. I believe your code would look something like this:

kernel.Bind<DbContext>().To<MyCustomEfContext>().InRequestScope().WithConstructorArgument("connectionString", ConfigurationManager.ConnectionStrings["MyCustomEfContext"].ConnectionString);

大多数人将不需要下一部分,但是如果您在CustomEfContext中具有自定义逻辑(例如,我有一个重写记录来登录对SaveChanges()的调用),那么您将需要在以下位置定义您的自定义上下文类型这样的配置文件:

Most people won't need this next piece but if you have custom logic in your CustomEfContext (I have an override for logging on calls to SaveChanges() for example), then you'll need to define your custom context type in the configuration file like so:

<repositories>
  <repository name="ef5Repository" connectionString="CustomEfContext" cachingStrategy="standardCachingStrategy" dbContextType="My.Data.CustomEfContext, My.Data" factory="SharpRepository.Ef5Repository.Ef5ConfigRepositoryFactory, SharpRepository.Ef5Repository" />
</repositories>

其中dbContextType使用完整类型的名称空间语法定义您正在使用的自定义DbContext的类型.如果执行此操作,则需要通过将.Bind< DbContext>()更改为.Bind< CustomEfContext>()来在自定义上下文中将Ninject设置为Bind.但是就像我通常所说的那样,您可以直接使用DbContext而不会出现问题.

Where dbContextType defines the type of the custom DbContext you are using using the full type, namespace syntax. If you do this then you'll need to set Ninject to Bind on the custom context by changing .Bind<DbContext>() to .Bind<CustomEfContext>(). But like I said normally you can use DbContext directly without an issue.

这篇关于如何将实体框架DbContext注入到SharpRepository的ConfigurationBasedRepository中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆