如何将控制器动作的参数传递给 Ninject 具体类型?

Sta*_*hel 4 c# asp.net-mvc dependency-injection ninject ninject.web.mvc

我有 ProductController.cs

    namespace AmazonProductAdvertisingAPI.WebUI.Controllers
    {
        public class ProductController : Controller
        {

            public ProductController(IProductCollection productCollection)
            {
                _productCollection = productCollection;
            }

            public static string Title
            {
                get
                {
                    return _title;
                }
                set
                {
                    _title = value;
                }
            }
            public static int PageNumber
            {
                get
                {
                    return _pageNumber;
                }
                set
                {
                    _pageNumber = value;
                }
            }
            public static int ItemsPerPage
            {
                get
                {
                    return _itemsPerPage;
                }
                set
                {
                    _itemsPerPage = value;
                }
            }
            // GET: Product
            public ActionResult List(int page = 1, string search = null)
            {

                ProductListViewModel model = new ProductListViewModel
                {
                    Products = _productCollection.Products
                                                 .OrderBy(product => product.Title)
                                                 .Skip((page - 1) * pageSize)
                                                 .Take(pageSize),
                    PagingInfo = new PagingInfo
                    {
                        CurrentPage = page,
                        ItemsPerPage = pageSize,
                        TotalItems = _productCollection.Products.Count()
                    }
                };
                return View(model);
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

NinjectDependencyResolver.cs

namespace AmazonProductAdvertisingAPI.WebUI.Infrastructure
{
    public class NinjectDependencyResolver : IDependencyResolver
    {
        private IKernel kernel;

        public NinjectDependencyResolver(IKernel kernelParam)
        {
            kernel = kernelParam;
            AddBindings();
        }
        public object GetService(Type serviceType)
        {
            return kernel.TryGet(serviceType);
        }

        public IEnumerable<object> GetServices(Type serviceType)
        {
            return kernel.GetAll(serviceType);
        }

        private void AddBindings()
            {
                // Create dependency here

                kernel.Bind<IProductCollection>().To<AmazonProductCollection>()
                                                 .WhenInjectedInto<ProductController>()
                                                 .WithConstructorArgument("title", ProductController.Title)
                                                 .WithConstructorArgument("pageNumber", ProductController.PageNumber)
                                                 .WithConstructorArgument("itemsPerPage", ProductController.ItemsPerPage);
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

AmazonProductCollection 类具有构造函数:

public AmazonProductCollection(string title, int pageNumber, int itemsPerPage)
Run Code Online (Sandbox Code Playgroud)

我希望 AmazonProductCollection 从产品控制器的操作列表参数中获取自己的参数,因为当用户填充 TextBoxt 并单击 html 视图表单中的“搜索”按钮时,它会获得其中的一些参数。例如,我想使用操作列表中的参数字符串“search”并将其作为构造函数参数“title”传输到 AmazonProductCollection。

我读了这篇文章:如何将参数传递给由 Ninject 创建的瞬态对象?,但我不明白在我的情况下如何创造同样的东西。

有人可以帮助我使用 Ninject 吗?

Yac*_*sad 5

一种解决方案是使用工厂。

工厂界面看起来像这样:

public interface IAmazonProductCollectionFactory
{
    AmazonProductCollection Create(string title, int pageNumber, int itemsPerPage);
}
Run Code Online (Sandbox Code Playgroud)

这样的接口将存在于控制器所在的同一个项目(MVC 项目)中。

这种工厂的实现如下所示:

public class AmazonProductCollectionFactory : IAmazonProductCollectionFactory
{
    private readonly IResolutionRoot m_ResolutionRoot;

    public AmazonProductCollectionFactory (IResolutionRoot resolution_root)
    {
        m_ResolutionRoot = resolution_root;
    }

    public AmazonProductCollection Create(string title, int pageNumber, int itemsPerPage)
    {
        return resolution_root.Get<AmazonProductCollection>(
            new ConstructorArgument("title", title),
            new ConstructorArgument("pageNumber", pageNumber),
            new ConstructorArgument("itemsPerPage", pageNumber));
    }
}
Run Code Online (Sandbox Code Playgroud)

AmazonProductCollectionFactory会住里面成分根项目。在您的情况下,这可能是同一个 MVC 项目。请注意,拥有这种依赖于IResolutionRoot除组合根之外的任何地方的工厂是服务位置的一个例子,它被认为是一种反模式

现在,IProductCollection您需要注入IAmazonProductCollectionFactory的构造函数而不是,ProductController并让List操作使用它来创建这样的AmazonProductCollection实例:

var productCollection = factory.Create(...);
Run Code Online (Sandbox Code Playgroud)

其中factory是您用来存储注入的IAmazonProductCollectionFactory.

不要忘记向IAmazonProductCollectionFactoryNinject 容器注册。

请注意,您应该考虑创建符合List操作要求的服务。例如,您可以将整个搜索逻辑包装在一个知道如何进行搜索的服务中,并让此类服务担心AmazonProductCollection通过抽象工厂创建。然后,不是将工厂注入控制器,而是将服务注入控制器。