【发布时间】:2019-09-04 14:25:02
【问题描述】:
我有一个 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;
}
现在我想保留我在第一个视图加载中添加的表。并使其在下一个 post-backs 中不可变。我使用了会话。我不知道应该改用 ViewBag 还是 ViewData。
简而言之:我应该遵循哪种模式来制作不可变的 BusinessLayer?
因为每次回发都不需要一次又一次地获取表信息或键或逻辑本身。
2019 年 4 月 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;
【问题讨论】:
-
Session 和 ViewBag 不是完成此类工作的好工具。对于快速简单的解决方案,您可以使用“Singleton”模式。
-
我更新了问题以添加更多积分。不知道哪个模式好?将整个图层属性转换为会话,或者只添加该图层的会话并直接使用属性
标签: c# asp.net .net asp.net-mvc session