这是一个简单的例子来说明行为:
给出这个html标记:
<div data-company="Microsoft"></div>
Run Code Online (Sandbox Code Playgroud)
和这个jQuery代码(使用jQuery 1.5.1):
// read the data
alert($("div").data("company"));
// returns Microsoft <<< OK!
// set the data
$("div").data("company","Apple");
alert($("div").data("company"));
// returns Apple <<< OK!
// attribute selector
alert($("div[data-company='Apple']").length);
// returns 0 <<< WHY???
// attribute selector again
alert($("div[data-company='Microsoft']").length);
// returns 1 <<< WHY???
// set the attribute directly
$("div").attr("data-company","Apple");
alert($("div[data-company='Apple']").length);
// now returns 1 <<< OK!
Run Code Online (Sandbox Code Playgroud)
由于jQuery自动将HTML5 data-*导入jQuery的数据对象,因此在数据更改时不应更新属性吗?
由于MVC 3中的IoC/DI实现很可能是RC中的最终形式,我正在寻找使用Caste Windsor的DependencyResolver,IControllerActivator和IViewPageActivator的更新实现.有没有为MVC 3 RC更新过的例子?
编辑#1 实现Windsor依赖解析器确实是微不足道的,但仍然缺少一些东西.与Jeff Putz的Ninject示例(下图)相反,它似乎并不像Windsor那么简单.设置依赖解析器之后,
DependencyResolver.SetResolver(new WindsorDependencyResolver(container));
Run Code Online (Sandbox Code Playgroud)
Windsor抛出ComponentNotFoundException.我需要为IControllerFactory和IControllerActivator提供实现.由于DefaultControllerFactory是DependencyResolver,因此可以按如下方式解决:
Component.For<IControllerFactory >().ImplementedBy<DefaultControllerFactory>()
Component.For<IControllerActivator >().ImplementedBy<WindsorControllerActivator>(),
Run Code Online (Sandbox Code Playgroud)
WindsorControllerActivator也是微不足道的.但是,这会导致IViewPageActivator的另一个ComponentNotFoundException.
这让我相信我错过了什么.没有办法比实现一个控制器工厂和调用ControllerBuilder.Current.SetControllerFactory MVC 2.0风格更复杂.
编辑#2 我错过了在无法找到服务时Dependency解析器需要返回null的微妙但重要的细节.实施如下:
public class WindsorDependencyResolver : IDependencyResolver
{
private readonly IWindsorContainer container;
public WindsorDependencyResolver(IWindsorContainer container)
{
this.container = container;
}
public object GetService(Type serviceType)
{
return container.Kernel.HasComponent(serviceType) ? container.Resolve(serviceType) : null;
}
public IEnumerable<object> GetServices(Type serviceType)
{
return container.Kernel.HasComponent(serviceType) ? container.ResolveAll(serviceType).Cast<object>() : new object[]{};
}
}
Run Code Online (Sandbox Code Playgroud)
编辑#3
回答评论中的问题.如果你确实发现你需要自己的IControllerActivator,这里有一个简单的Windsor实现:
public class WindsorControllerActivator : IControllerActivator
{
private readonly IWindsorContainer container;
public WindsorControllerActivator(IWindsorContainer container) …Run Code Online (Sandbox Code Playgroud)