Ninject 缓存注入的 DataContext?生命周期管理?

发布于 2024-10-09 01:34:32 字数 468 浏览 5 评论 0原文

我的存储库中出现了一系列非常奇怪的错误。未找到或更改行,2 次更新中有 1 次失败...没有任何意义。

就好像我的 DataContext 实例被缓存了......没有任何意义,我正在考虑职业变动。

然后我注意到 DataContext 实例是使用依赖注入、使用 Ninject 传入的(这是我第一次使用 DI...)。我删除了依赖注入,一切都恢复正常。即刻。

所以依赖注入是问题所在,但我仍然不知道为什么。我推测 Ninject 正在缓存注入的 DataContext。

这是正确的吗?

编辑:

Ninject 绑定如下:

Bind<IPupilBlockService>().To<SqlPupilBlockService>()
   .WithConstructorArgument("db", new dbDataContext());

I had a series of very bizarre errors being thrown in my repositories. Row not found or changed, 1 of 2 updates failed... Nothing made sense.

It was as if my DataContext instance was being cached... Nothing made sense and I was considering a career move.

I then noticed that the DataContext instance was passed in using dependency injection, using Ninject (this is the first time I have used DI...). I ripped out the Dependency Injection, and all went back to normal. Instantly.

So dependency injection was the issue, but I still don't know why. I am speculating that Ninject was caching the injected DataContext.

Is this correct?

Edit:

The Ninject binding is as follows:

Bind<IPupilBlockService>().To<SqlPupilBlockService>()
   .WithConstructorArgument("db", new dbDataContext());

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

儭儭莪哋寶赑 2024-10-16 01:34:32

对于任何生命周期必须显式管理的对象(例如实现 IDisposable 的对象)或对用户很重要的对象,请尝试不要注入它们,而是注入一个允许创建此类对象的工厂。例如定义这个接口:

public interface IDbDataContextFactory
{
    dbDataContext CreateNew();
}

并按如下方式使用它:

public class SqlPupilBlockService
{
    private IDbDataContextFactory contextFactory;

    public SqlPupilBlockService(
        IDbDataContextFactory contextFactory)
    {
        this.contextFactory = contextFactory;
    }

    public void DoSomeOperation()
    {
        using (var db = this.contextFactory.CreateNew())
        {
           // use the dbDataContext here.
        }
    }
}

的实现将非常简单,如下所示:

public class DbDataContextFactory : IDbDataContextFactory
{
    public dbDataContext CreateNew()
    {
        return new dbDataContext();
    }
}

注册过程如下:

Bind<IDbDataContextFactory>().To<DbDataContextFactory>();

工厂的使用使得非常明确谁是所创建对象的所有者以及谁应该控制它的寿命。这使您的代码更具可读性,并遵循最小惊喜原则

更新

自从我提交这个答案以来已经过去一年多了。我现在经常注入数据上下文本身,并根据每个(网络)请求进行注册,而不是使用工厂。然而;与往常一样,您可能需要改变应用程序的设计方式:这取决于情况。请查看此答案

For any object which lifetime must explicitly managed (such as objects that implement IDisposable) or matters to the user, try not to inject them, but inject a factory that allows creating such objects instead. Define this interface for instance:

public interface IDbDataContextFactory
{
    dbDataContext CreateNew();
}

And use it as follows:

public class SqlPupilBlockService
{
    private IDbDataContextFactory contextFactory;

    public SqlPupilBlockService(
        IDbDataContextFactory contextFactory)
    {
        this.contextFactory = contextFactory;
    }

    public void DoSomeOperation()
    {
        using (var db = this.contextFactory.CreateNew())
        {
           // use the dbDataContext here.
        }
    }
}

An implementation of would be very simple, like this:

public class DbDataContextFactory : IDbDataContextFactory
{
    public dbDataContext CreateNew()
    {
        return new dbDataContext();
    }
}

And registration goes like this:

Bind<IDbDataContextFactory>().To<DbDataContextFactory>();

The use of a factory makes it very explicit who is the owner of the created object and who should control its lifetime. This makes your code more readable and follows the principle of least surprise.

UPDATE

More than a year has past since I submitted this answer. Instead of using factories, I now often inject the data context itself, and register it on a per (web) request basis. However; a shift in how you need to design your application might be needed, so as always: it depends. Please take a look at this answer.

兮颜 2024-10-16 01:34:32

@Steven 给出了很好的解释,但实际上,Ninject 已经为您提供了一种指定为每个请求生成实例的方法: InRequestScope

Bind<IPupilBlockService>()
   .To<SqlPupilBlockService>()
   .InRequestScope()
   .WithConstructorArgument("db", new dbDataContext());

@Steven gave a great explanation, but actually, Ninject already gives you a way to specify that an instance be generated for per-request: InRequestScope.

Bind<IPupilBlockService>()
   .To<SqlPupilBlockService>()
   .InRequestScope()
   .WithConstructorArgument("db", new dbDataContext());
稳稳的幸福 2024-10-16 01:34:32

查看激活行为。您的 DataContext 可能被绑定在 Singleton 作用域中,或者其他作用域之一,这将导致从内核返回相同的实例,具体取决于您的调用上下文。

(我对 ninject 不太熟悉,但给定瞬态范围是默认值,我希望在您的应用程序代码中的某个位置 DataContext 被明确定义为具有非瞬态范围)

Take a look at Activation Behaviours. It's possible your DataContext is being bound in a Singleton scope, or one of the other scopes that will result in the same instance being returned from the kernel depending on your call context.

(I'm not too familiar with ninject but given transient scope is the default, I'd expect somewhere in your app code the DataContext is explicitly defined as having a non-transient scope)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文