我有BusinessLayer,它包含旧桌面应用程序中的集合和可重用代码。
现在我想在MVC应用程序中再次使用此层。我尝试在Controller中使用图层,例如Index()
public ActionResult Index()
{
if (Session["DataEntryLogic"] == null)
Session["DataEntryLogic"] = new DataEntryLogic();
var EntryLogic = Session["DataEntryLogic"] as DataEntryLogic;
EntryLogic.Tables.Add(new Table());
EntryLogic.Tables[0].TableID = "AccTransHed";
EntryLogic.Tables[0].TableType = TableType.Master;
}
现在我想保留我在第一个View加载中添加的表。并使其在下一个回发中不可变。我用了Session。我不知道是否应该使用ViewBag或ViewData。
简而言之:我应该遵循哪种模式来制作不可变的BusinessLayer?
因为每次发生回发事件都不需要一次又一次地获取表信息或密钥或逻辑本身。
更新时间为2019/04/14
我应该将整个BusinessLogic属性和集合替换为此模式吗?
在Windows应用程序中:
pulic class EntryLogic{
public List<Table> Tables{get;set;}
}
到MVC应用程序:
public class EntryLogic{
public List<Table> Tables{
get{
if(Session["Tables"] == null)
Session["Tables"] = new List<Table>();
return Session["Tables"] as List<Table>;
}
set { Session["Tables"] = value;}
}
}
或者只是在会话变量中初始化桌面应用程序的EntryLogic实例?
var EntryLogic = Session["EntryLogic"] as EntryLogic;
对我来说,听起来你可以使用缓存来减少对数据库的调用,而不用担心保留状态。