【问题标题】:ninject with datacontext and need for static implementation in MVCninject 与 datacontext 并需要在 MVC 中静态实现
【发布时间】:2020-07-14 18:04:07
【问题描述】:

我正在使用 .net 标准类库在现有 ASP.NET MVC Web 应用程序上实现实体框架核心。 我将数据上下文注入到我正在使用的类中,但我还需要一些特定方法调用的静态实现。这里明显的问题是,在静态上下文中,我没有注入的数据上下文。所以我做了以下事情。 这样做的正确方法是什么? 我在库中使用 Ninject 模块将依赖项映射到 Web 项目中的内核。

namespace AppealTrack.Logic.Classes
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Common.Entities;
    using Data;
    using Interfaces;
    using Microsoft.EntityFrameworkCore;

    public class LookupLogic : ILookupLogic, IDisposable
    {
        private readonly AppealTrackDataContext _context;

        public LookupLogic(AppealTrackDataContext context)
        {
            _context = context;
        }

        public List<County> GetCountries(string state)
        {
            var list = _context.Countries.Where(x => state == null || x.State == state).Distinct().AsNoTracking().ToList();

            return list;
        }

        public void Dispose()
        {

        }
    }

    public static class LookupLogicStatic
    {
        public static List<County> GetCountries(string state)
        {
            // this is the part that I don't think is correct:
            using (var logic = new LookupLogic(new AppealTrackDataContext()))
            {
                var list = logic.GetCountries(state);

                return list;
            }
        }
    }
}

【问题讨论】:

  • 公共类 LogicModule : NinjectModule { public override void Load() { Bind().ToSelf();绑定().To();绑定().To();绑定().To();绑定().To();绑定().To();绑定().To(); } }
  • 你能解释为什么你需要LookupLogicStatic 是静态的吗?如果您使用 DI,则应该将 LookupLogic 注入消费者类,而不是使用其静态版本。你也能解释一下为什么LookupLogic 实现IDisposable 吗? Ninject 负责管理对象的生命周期,所以一般来说,您不需要显式地dispose 它们。
  • LookupLogic 只实现了 IDisposable,因为我将它包装在 using 中。这是试图为这个问题找到解决方案的副产品。我需要一个静态实现,因为我们将 DevExpress 用于 MVC,并且与它们的控件的一些交互性迫使您使用静态方法。我正在使用 DI,但由于这些 DevExpress 控件,我还必须有一些静态方法。 ninject 是否提供了一种解决静态类/方法的数据上下文构造函数注入的方法?如果是这样,我该怎么做?

标签: asp.net-mvc ninject


【解决方案1】:

如果您需要静态 GetCountries 方法,那么您无能为力...您需要为该方法管理 DbContext 的范围,因为静态构造函数不接受任何参数。您的LookupLogic必须成为Disposable(如果您愿意,也可以)。

如果需要静态方法,那么就不用添加新的静态类,可以在LookupLogic中添加静态方法:

public class LookupLogic : ILookupLogic
{
    private readonly AppealTrackDataContext _context;

    public LookupLogic(AppealTrackDataContext context)
    {
        _context = context;
    }

    public List<Country> GetCountries(string state)
    {
        return GetCountries(_context, state);
    }

    public static List<Country> GetCountriesStatic(string state)
    {
        using (var dbContext = new AppealTrackDataContext())
        {
            return GetCountries(dbContext, state);
        }
    }

    private static List<Country> GetCountries(AppealTrackDataContext dbContext, string state)
    {
        return dbContext.Countries.Where(x => state == null || x.State == state).Distinct().AsNoTracking().ToList();
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多