我在我的MVC项目中使用Ninject 1.5.它运行良好,但由于我们有Ninject 2,我可以升级(并且另外使用每个请求行为,这在1.5中无法正常工作).Ninject 1.5具有InjectPropertiesWhere功能,在Ninject 2中缺失(至少在我前一段时间测试它时).有类似的东西吗?
示例InjectPropertiesWhere:
return Bind<IUserService>().To<UserService>()
.InjectPropertiesWhere(p => p.Name.EndsWith("Repository"))
.InjectPropertiesWhere(p => p.Name.EndsWith("Service"))
.InjectPropertiesWhere(p => p.Name == "ApplicationCache")
.InjectPropertiesWhere(p => p.Name == "CurrentPrincipal")
.InjectPropertiesWhere(p => p.Name == "CTEmailSender")
.InjectPropertiesWhere(p => p.Name == "CTSettings");
Run Code Online (Sandbox Code Playgroud) 我的控制器中有一个这样的方法
public string UpdateResource()
{
Thread.Sleep(2000);
return string.Format("Current time is {0}", DateTime.Now.ToShortTimeString());
}
Run Code Online (Sandbox Code Playgroud)
我的视图页面中有一个html按钮,当我点击它时,我想要一个加载的gif图像出现,然后在2000ms后消失.下面是我的HTML
<input type="button" id="btnUpdate" value="Update" />
<img id="loading" src="/Images/ajax-loader.gif" alt="Updating ..." />
<div id="result"></div>
Run Code Online (Sandbox Code Playgroud)
如何调用控制器方法.我已经看到了,Html.ActionLink但我希望在按钮点击而不是超链接.
请帮忙
最近我转到MVC 3和Ninject 2.在大多数代码中,我使用构造函数注入,但是有一些地方,我必须使用Inject属性.Ninject 2注册自己的IDepencyResolver接口.我不喜欢DependencyResolver类是System.Web.Mvc命名空间的一部分,因为它的功能与MVC并没有真正的严格关系,但现在,当它存在时,我可以做
public SomeClass
{
public IUserService UserService { get; set; }
public SomeClass()
{
UserService = DependencyResolver.Current.GetService<IUserService>();
Run Code Online (Sandbox Code Playgroud)
代替
public SomeClass
{
[Inject]
public IUserService UserService { get; set; }
Run Code Online (Sandbox Code Playgroud)
所以我不必Ninject在我的类中引用命名空间.应该DependencyResolver这样使用?
asp.net-mvc dependency-injection ninject ninject-2 ninject.web.mvc
我想知道转换的一个好办法Model来ViewModel,并ViewModel以Model没有AutoMapper或类似的东西,因为我想知道背后是什么,并学习如何做我自己.当然,模型I指的是EF生成的类.
到目前为止,我做了类似的事情,但是当涉及到嵌套类时会遇到一些问题:
// to VM
public static Author ToViewModel(EntityAuthor author)
{
if (author == null)
return null;
Author result = new Author();
result.Id = author.ATH_ID;
result.FirstName = author.ATH_FirstName;
result.LastName = author.ATH_LastName;
return result;
}
public static BlogPost ToViewModel(EntityBlogPost post)
{
if (post == null)
return null;
Experiment result = new Experiment();
result.Id = post.BP_ID;
result.Title = post.BP_Title;
result.Url = post.BP_Url;
result.Description = post.BP_Description;
result.Author = ToViewModel(post.Author);
return result;
}
// …Run Code Online (Sandbox Code Playgroud)