Asp.net MVC继承控制器
问题描述:
我STUDING asp.net mvc的,在我的测试项目中,我有一些问题继承: 在我的模型我在几个实体使用inheritanse:Asp.net MVC继承控制器
public class Employee:Entity
{
/* few public properties */
}
它是基类。 及后裔:
public class RecruitmentOfficeEmployee: Employee
{
public virtual RecruitmentOffice AssignedOnRecruitmentOffice { get; set; }
}
public class ResearchInstituteEmployee: Employee
{
public virtual ResearchInstitute AssignedOnResearchInstitute { get; set; }
}
我想实现一个简单的CRUD操作的每一个descedant。
什么是更好的方式来实现控制器和后裔的意见: - 每一个后代一个控制器; - 控制器继承;
- 通用控制器; - 一个控制器中的通用方法。
或者也许有另一种方式?
我的ORM是NHibernate,我有一个通用的基础知识库,每个知识库都是其子句。我认为,使用通用控制器是最好的方法,但是我只会使用通用的基础知识库,系统的可扩展性不会太好。
请帮助新手)
答
这真的取决于多少共享的逻辑存在,以及如何你想你的应用程序流。
如果大部分逻辑是相同的,我会说重复使用单个控制器并创建匹配每个继承类型的视图。您将根据类型加载相应的存储库,域对象和视图。类型可以通过参数,路由或在动作过滤器中确定的其他查找来确定。
下面是通过在参数传递(最简单的技术IMO)做的一个示例:
public class EmployeeController : Controller
{
public enum EmployeeType
{
RecruitmentOffice,
ResearchInstitute
}
public ActionResult Details(int id, EmployeeType type)
{
switch (type)
{
case EmployeeType.RecruitmentOffice:
// load repository
// load domain object
// load view specific to recruitment office
break;
case EmployeeType.ResearchInstitute:
// load repository
// load domain object
// load view specific to recruitment office
break;
}
}
}