Ninject 和 MVCContrib 网格模型
我确信必须有一种简单的方法来做到这一点,但我似乎无法理解它。
我正在使用 MVCContrib 网格控件来显示我正在开发的 3 层应用程序中的多个网格(ASP.NET MVC3 PL -> BLL -> DAL)。我还使用 Ninject 自动注入所有依赖项。
我遇到的问题是,我正在使用网格模型在视图中显示网格,如下所示:
@Html.Grid(Model).WithModel(new UserGridModel(Html)).Attributes(id => tableName)
并定义了相应的网格模型:
public class UserGridModel : GridModel<User> {
public UserGridModel(HtmlHelper html)
{
Dictionary<int, string> userStatuses = /*TODO: GET ALL USER STATUSES*/;
Column.For(user => user.ID);
Column.For(user => html.ActionLink(user.Email, "Edit", new {id = user.ID})).Named(DtoResources.UserDto_Email);
Column.For(user => user.FirstName);
Column.For(user => user.LastName);
Column.For(user => userStatuses[user.StatusID]);
}
}
现在我需要将服务注入到该模型中,以便它可以提取所有适用的状态从服务(BLL)级别。目前,为了确保这能够正常工作,我在引导代码中公开了 IKernel 以及 IKernel.Get(),但我认为这不是获取它的最干净的方法。我会使用构造函数注入,但是如果我将 IUserStatusService 作为参数放入构造函数中,当我在视图中调用 new UserGridModel(Html) 而不显式使用IKernel 在那里。
我要么遗漏了一些东西,要么把它连接错了。不管怎样,我都被困住了......有什么帮助吗?通过 Ninject 获取我的服务实例的正确方法是什么
I am sure there has to be an easy way to do this, but I just cant seem to get my head around it.
I am using the MVCContrib Grid control to display a number of grids in a 3 tier application I am working on (ASP.NET MVC3 PL -> BLL -> DAL). I also am using Ninject to automatically inject all my dependencies.
The problem I am having is that I am using a grid model to display grids in my Views like this:
@Html.Grid(Model).WithModel(new UserGridModel(Html)).Attributes(id => tableName)
and have the corresponding grid model defined:
public class UserGridModel : GridModel<User> {
public UserGridModel(HtmlHelper html)
{
Dictionary<int, string> userStatuses = /*TODO: GET ALL USER STATUSES*/;
Column.For(user => user.ID);
Column.For(user => html.ActionLink(user.Email, "Edit", new {id = user.ID})).Named(DtoResources.UserDto_Email);
Column.For(user => user.FirstName);
Column.For(user => user.LastName);
Column.For(user => userStatuses[user.StatusID]);
}
}
Now I need to inject a service into this model so it can pull in all of the applicable statuses from the service (BLL) level. Currently just to make sure this would work, I exposed the IKernel in the Bootstrapping code and just IKernel.Get() but I don't think that is the cleanest way to get it. I would use constructor injection, but if I put the IUserStatusService as a parameter in the constructor, I can't figure out how I would get Ninject to inject the correct parameter when I call new UserGridModel(Html) in the view without explicitly using the IKernel there.
I am either missing something or wiring this up all wrong. Either way I'm stuck ... any help? What is the proper way to get an instance of my service through Ninject
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在我看来,解决您的问题的最干净的解决方案是更改您的控制器,以便它创建一个已经包含用户状态作为字符串的模型,以便在视图中不需要转换。我会在视图和网格模型中尽可能少地做。
另一种可能性是将服务属性注入到您的视图中,并将其传递到网格模型。但正如我所提到的,你正在为你的观点引入逻辑。
In my opinion the cleanest solution to your problem is to change your controller so that it creates a model that already contains the user status as string so that no convertions is required in the view. I would do as littel as possible in the view and grid model.
Another possibility is to property inject the service to your view an pass it to the grid model. But as I mentioned this way you are introducing logic to your view.