Search code examples
asp.net-mvc-4actionfilterattributetimed-events

Getting code to run on all pages based on date


I am trying to create a program that has two main parts: Projects and Audits

A Project will have a start date and a number that will tell it how many days until completion (they're all preset).

I need an Audit to be created say one day before the Project is complete.

No big deal so far.

The problem I forsee and am struggling with how to answer is:

How can I efficiently maintain this? I am using MVC4 and have tried Action Filters. I have the code running on every page - to cover any custom urls accessing the site. However, this seems to slow things down considerably. Here's what I have so far:

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


    }
}

Any help is appreciated.


Solution

  • Check out Revalee. It let's you schedule actions to be taken at a later time. So, when you create a project, you would then use the Revalee client to schedule an audit to be created one day before it ends.