我正在尝试创建一个包含两个主要部分的程序:Projects and Audits
项目将有一个开始日期和一个数字,告诉它完成前的天数(它们都已预设)。
我需要在项目完成前一天创建审核。
到目前为止没什么大不了的。
我认为并且正在努力解决的问题是:
我该如何有效地维护这个?我正在使用MVC4并尝试过Action Filters。我在每个页面上运行代码 - 覆盖访问该站点的任何自定义URL。然而,这似乎大大减缓了事情。这是我到目前为止所做的:
public class CreateAuditsController : ActionFilterAttribute
{
private QAAPPEntities db = new QAAPPEntities();
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
CreateAuditsFromProjects();
base.OnActionExecuted(filterContext);
}
public void CreateAuditsFromProjects()
{
// Generate list of Research Projects
List<ResearchProject> allResearchProjects = db.ResearchProjects.Include(a => a.ResearchProjectStatus).ToList();
// Cycle through those to see which ones need audits created
for (int i = 0; i < allResearchProjects.Count; i++)
{
// Get current looped project for further use
ResearchProject currentLoopedProject = allResearchProjects[i];
if (currentLoopedProject.Description == "Some Description")
{
Audit auditToAdd = new Audit();
auditToAdd.ResearchProjectID = currentLoopedProject.ResearchProjectID;
auditToAdd.Name = "Some Name";
auditToAdd.AssignedQAID = 1;
auditToAdd.CreatedDate = DateTime.Now;
auditToAdd.AuditStatusID = 1;
auditToAdd.AuditTypeID = 1;
db.Audits.Add(auditToAdd);
db.SaveChanges();
}
}
db.SaveChanges();
// Create audits
}
}
感谢任何帮助。