如何注入的ModelState与Ninject参数? [英] How to inject ModelState as parameter with Ninject?

查看:122
本文介绍了如何注入的ModelState与Ninject参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即时消息Ninject很新。我想找到一种方法,通过一个控制器还到服务层的ModelState。

Im very new to Ninject. I want to find a way to pass Modelstate of a controller further to service layer.

我现在所拥有的:

       private readonly IAccountService service; 

       public AccountController(ILanguageService ls, ISessionHelper sh)
        {
            this.service = new AccountService(new ModelStateWrapper(this.ModelState));
            this.languageService = ls;
            this.sessionHelper = sh;

        }

    public AccountService(IValidationDictionary validationDictionary)
    {
        this.validationDictionary = validationDictionary;
    }

想我想以某种方式:

want i want to get to in some way:

       private readonly IAccountService service; 

       public AccountController(ILanguageService ls, ISessionHelper sh, IAccountService as)
        {
            this.service = as;
            this.languageService = ls;
            this.sessionHelper = sh;

        }

    public AccountService(IValidationDictionary validationDictionary)
    {
        this.validationDictionary = validationDictionary;
    }

但像你看到的AccountService将永远无法收到IValidationDictionary,因为它从来没有从发送的AccountController作为prameter。

But like you see AccountService will never be able to receive IValidationDictionary because it was never sent from AccountController as a prameter.

是否有可能实现这一目标?或者这只是事情我不得不住在一起吗?

Is it possible to achieve that? or is this just one of the things i have to live with?

推荐答案

的ModelState是应用程序的运行的状态的一部分。因此,当您撰写与Ninject应用它不可在时间点。

ModelState is part of the application's runtime state. Therefore, it is not available at the point in time when you compose the application with Ninject.

您可以解决此限制通过创建抽象工厂创建帐户服务键,也使您的应用程序运行时状态的一部分。

You could work around this limitation by creating an Abstract Factory to create your AccountService and also make it a part of your application's runtime state.

public interface IAccountServiceFactory
{
    IAccountService Create(IValidationDictionary validationDictionary);
}

public class AccountServiceFactory
{
    public IAccountService Create(IValidationDictionary validationDictionary)
    {
        return new AccountService(validationDictionary);
    }
}

然后在你的AccountController,注入而不是一个AccountService的AccountServiceFactory。

And then in your AccountController, inject an AccountServiceFactory instead of an AccountService.

   private readonly IAccountServiceFactory serviceFactory; 

   public AccountController(ILanguageService ls, ISessionHelper sh, IAccountServiceFactory asf)
    {
        this.serviceFactory = asf;
        this.languageService = ls;
        this.sessionHelper = sh;

    }

    public void DoSomething()
    {
        var accountService = this.serviceFactory.Create(new ModelStateWrapper(this.ModelState));

        // Do something with account service
    }

另外,你可以直接通过它要求每个公共方法调用传递运行依赖于该帐户的服务。

Alternatively, you could pass the runtime dependency directly to the account service through each public method call where it is required.

this.service.DoSomething(new ModelStateWrapper(this.ModelState));

这篇关于如何注入的ModelState与Ninject参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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