1

こんにちは、データベース コンテキストに沿って静的ヘルパー クラスを解析する際に問題が発生しています。たとえば、私の UserHelper クラスでは、ユーザーが管理者であるかどうかを確認するためにルックアップを行いたいと考えています。現在、Userhelper で新しいコンテキストを作成して修正していますが、ninject で作成された 1 つのインスタンスを使用したいと考えています。これはどのように可能ですか?

public static class UserHelper
{
    private static MetropolOpgavebankenEntities _context;
    public static MetropolOpgavebankenEntities Context
    {
        get
        {
            if (_context == null)
                _context =
                    new MetropolOpgavebankenEntities(
                        ConfigurationManager.Instance.Configuration.ConnectionString.Value);
            return _context;
        }
    }

    public static bool IsAdmin()
    {
        if (!HttpContext.Current.User.Identity.IsAuthenticated)
            return false;
        string username = HttpContext.Current.User.Identity.Name;
        if(Context.Administrators.Any(x => x.MetropolId.ToLower() == username.ToLower()))
            return true;
        return false;
    }
}

私のninjectコード

private static void RegisterServices(IKernel kernel)
{
    kernel.Bind<MetropolOpgavebankenEntities>().ToMethod(c => new MetropolOpgavebankenEntities(ConfigurationManager.Instance.Configuration.ConnectionString.Value)).InRequestScope();
    kernel.Bind<OpgavebankService>().To<OpgavebankService>();
}   
4

1 に答える 1