具有接口类型的 Asp.Net Mvc 模板化助手
我想使用 Asp.net MVC 模板化助手功能为整个应用程序中的对象生成标准 UI,但我遇到了一个重大问题:
我没有直接将类类型从控制器传递到它们的视图中。相反,我传递接口类型......并将模型的实际实现包装在间接引用的项目中的 Mongo 或 NHibernate 特定类中。
为了进行讨论,我的对象如下所示:
public interface IProductRepository {
IProduct GetByName(string name);
}
public interface IProduct {
string Name { get; set; }
}
public class NHibernateProductRepository : IProductRepository {
public IProduct GetByName(string name) {
/* NHibernate Magic here */
return nhibernateFoundProduct;
}
}
public class NHibernateProduct : IProduct {
public virtual Name { get; set; }
}
public class ProductController : Controller {
public ProductController(IProductRepository productRepo) {
_ProductRepo = productRepo;
}
public ActionResult Index(string name) {
IProduct product = _ProductRepo.GetByName(name);
return View(product);
}
}
Is it possible to use interface types with the Editor.For() 语法?有什么问题或症结需要我注意吗?
我有一个可用的 EditorTemplate\IProduct.ascx 文件。目前,如果不将“IProduct”名称硬编码到 Editor.For() 调用中,我似乎无法渲染该模板。我更喜欢这种类型的“约定优于配置”......
I would like to use the Asp.net MVC templated helpers functionality to generate a standard UI for my objects throughout my application, but I've run into a significant issue:
I do not directly pass class types from my controllers into their views. Instead, I pass interface types.. with the actual implementation of the Model wrapped up in a Mongo or NHibernate specific class in an indirectly referenced project.
For discussion, my objects look like:
public interface IProductRepository {
IProduct GetByName(string name);
}
public interface IProduct {
string Name { get; set; }
}
public class NHibernateProductRepository : IProductRepository {
public IProduct GetByName(string name) {
/* NHibernate Magic here */
return nhibernateFoundProduct;
}
}
public class NHibernateProduct : IProduct {
public virtual Name { get; set; }
}
public class ProductController : Controller {
public ProductController(IProductRepository productRepo) {
_ProductRepo = productRepo;
}
public ActionResult Index(string name) {
IProduct product = _ProductRepo.GetByName(name);
return View(product);
}
}
Is it possible to use interface types with the Editor.For() syntax? Are there any problems or sticking points that I need to be aware of?
I have an EditorTemplate\IProduct.ascx file available. At this time, I can't seem to get that template to be rendered without hardcoding the "IProduct" name into the Editor.For() call. I would prefer this type of 'Convention over Configuration'....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
模板助手将使用对象的运行时类型作为名称。在这种情况下,您应该将文件命名为 NHibernateProduct.ascx
如果您在设计时不知道类型的名称,那么您可以编写一个辅助方法来检查对象实例并遍历特定类型正在实现的接口列表并根据该名称返回一个名称。然后,您可以调用带有字符串“templateName”参数的
EditorFor
的适当重写。The templates helpers will use the runtime type of the object for the name. In this case you should name the file NHibernateProduct.ascx
If you don't know the name of the type at design time than you could write a helper method that would inspect the object instance and walk the list of interfaces that a particular type is implementing and return a name based on that. Then you would call the appropriate override to
EditorFor
that takes the string "templateName" parameter.我决定使用一种方法,将 ViewModel 原生用于实现 IProduct 接口的 Web 项目。
I have decided to use an approach with a ViewModel native to the Web project that implements the IProduct interface.