简体   繁体   English

在ASP.net MVC中划分大任务的最佳实践

[英]Best practice to divide a big task in ASP.net MVC

In my ASP.Net MVC4 project I have a method which saves the user entered data. 在我的ASP.Net MVC4项目中,我有一个方法可以保存用户输入的数据。 And once the saving is done the same page is reloaded with new values. 保存完成后,同一页面将重新加载新值。

However in a new requirement we have a time consuming Index rebuild for search functionality to be added when ever user saves data. 但是,在新的要求中,我们有一个耗时的索引重建,以便在用户保存数据时添加搜索功能。 Though we were successful in getting it done, the rebuilding is a time consuming task. 虽然我们成功地完成了它,但重建是一项耗时的任务。 Hence the user need to wait for considerable amount of time to get the post back action to see the refreshed page. 因此,用户需要等待相当长的时间才能获得回发操作以查看刷新的页面。

So I was wondering is there a good practice, to break down this project. 所以我想知道是否有一个很好的做法,打破这个项目。 As we had previously, the control should return to the user, once the data is saved in DB. 如前所述,一旦数据保存在DB中,控件应返回给用户。 While the rebuilding should happen at background. 虽然重建应该在后台进行。

Previous Function Prototype. 上一个函数原型。

function saveData()
{
   //Do Saving
   Return Success
}

Current Function 当前功能

function saveData()
    {
       //Do Saving
       // Rebuild Index
       Return Success
    }

Now can I return the success before rebuilding, also I need to do the rebuilding at the back end. 现在我可以在重建之前返回成功,我还需要在后端进行重建。

One way we thought was, since we are in Azure we can create a worker role to run this ever few interval. 我们想到的一种方式是,因为我们在Azure中,我们可以创建一个辅助角色来运行这个极少的时间间隔。 However this is a big overhead since we may not get the user update their profile every now and then. 然而,这是一个很大的开销,因为我们可能不会让用户不时更新他们的个人资料。 But when ever they do it should run. 但是,如果他们这样做,它应该运行。

Is any inbuilt ASp.net / C# function or architecture can be used to achieve this? 是否可以使用任何内置的ASp.net / C#功能或架构来实现这一目标? Asynchronusly? Asynchronusly?

I dont think you need to rebuild lucene indexes all the time, All you need to do is to remove the old index of the data and just add the new index. 我不认为你需要一直重建lucene索引,你需要做的就是删除旧的数据索引,只需添加新的索引。 With this approach it is going to be fast. 通过这种方法,它将会很快。 Code snippet shown below - 代码片段如下所示 -

private static void _addToLuceneIndex(SampleData sampleData, IndexWriter writer) {
    // remove older index entry
    var searchQuery = new TermQuery(new Term("Id", sampleData.Id.ToString()));
    writer.DeleteDocuments(searchQuery);

    // add new index entry
    var doc = new Document();

    // add lucene fields mapped to db fields
    doc.Add(new Field("Id", sampleData.Id.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
    doc.Add(new Field("Name", sampleData.Name, Field.Store.YES, Field.Index.ANALYZED));
    doc.Add(new Field("Description", sampleData.Description, Field.Store.YES, Field.Index.ANALYZED));

    // add entry to index
    writer.AddDocument(doc);
} 

Ref - http://www.codeproject.com/Articles/320219/Lucene-Net-ultra-fast-search-for-MVC-or-WebForms 参考 - http://www.codeproject.com/Articles/320219/Lucene-Net-ultra-fast-search-for-MVC-or-WebForms

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM