简体   繁体   English

.Net 我应该如何在 asp.net 项目的不同层之间共享 session 对象

[英].Net how should i share session objects between different layers of asp.net project

I have created a List objects when application starts and saved them as a session variable.我在应用程序启动时创建了一个 List 对象并将它们保存为 session 变量。

I then use this session variable throughout life of the application.然后我在应用程序的整个生命周期中使用这个 session 变量。

At times I update to this List in other layers of the application so I want to pass this session variable to other layers.有时我会在应用程序的其他层更新到此列表,因此我想将此 session 变量传递给其他层。

Should I be passing List object from asp.net to other layers in the methods?我应该将列表 object 从 asp.net 传递到方法中的其他层吗? should i create a property of List in all layers which are going to use this object and set the property from the presentation layer?我应该在将要使用此 object 的所有层中创建 List 的属性并从表示层设置属性吗? if i create a static list then all users see same list.如果我创建一个 static 列表,那么所有用户都会看到相同的列表。 i want the list to be session specific.我希望列表是 session 特定的。

I would write the other layers of your application to accept and return list of objects, they shouldn't be aware or care that the list is being stored after they're done with it.我会编写应用程序的其他层来接受和返回对象列表,他们不应该知道或关心列表在完成后被存储。

Do not send a session object to your data layers or other business logic.不要将 session object 发送到您的数据层或其他业务逻辑。 One of the primary reasons for even separating those things out is so that you can reuse the code in other programs.甚至将这些东西分开的主要原因之一是您可以在其他程序中重用代码。

As soon as you add a dependency, like on HttpContext, then you may as well not have those layers and just throw it all together.一旦你添加了一个依赖,比如在 HttpContext 上,那么你可能没有这些层,只是把它们放在一起。

Regarding the list itself, when you need to pass the data to the business / data layers send it as a List.关于列表本身,当您需要将数据传递给业务/数据层时,将其作为列表发送。

Finally, I would suggest that unless that list is used on a large number of the pages that you don't put it in session at all and instead just pull the data from the database whenever it's needed.最后,我建议除非在大量页面上使用该列表,否则您根本不会将其放入 session 中,而是在需要时从数据库中提取数据。 Session data has to be serialized and deserialized on each page access. Session 数据必须在每次页面访问时进行序列化和反序列化。 This adds processing overhead.这增加了处理开销。 It gets worse if your app is in a load balanced environment because the session data would have to be pushed and pulled across the network wire for every page access whether or not it was used... which neatly takes away any reason for having it in session anyway.如果您的应用程序处于负载平衡环境中,情况会变得更糟,因为无论是否使用 session 数据,每次访问页面都必须通过网络线推送和拉取数据……这巧妙地消除了将其放入的任何理由session 无论如何。

However, if the data is used in most places then I'm really curious as to what it is.但是,如果在大多数地方都使用数据,那么我真的很好奇它是什么。 Session shouldn't be used as a dumping ground for a bunch of objects. Session 不应用作一堆物体的倾倒场。 And I'm really hoping you are not trying to store things like datasets, command objects, or anything like that.我真的希望你不要试图存储数据集、命令对象或类似的东西。

I will never use Session variables in DataAccess Layer or Business Layer.我永远不会在数据访问层或业务层中使用 Session 变量。 It's like going against the design.这就像违背设计。 I will rather pass a value of the session variable in the function's parameter in the Data Access Layer or Business Layer which intents to use that Session variable我宁愿在数据访问层或业务层的函数参数中传递 session 变量的值,以使用该 Session 变量

Your Session variable is available using the following code:您的 Session 变量可使用以下代码获得:

HttpContext.Current.Session[....]

So, you need to create an additional properly only to reduce the code.因此,您只需要适当地创建附加代码即可减少代码。

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

相关问题 如何在两台服务器之间共享 ASP.NET Core 3 中的会话? - How to share Session in ASP.NET Core 3 between 2 servers? 如何共享不同页面和项目的ASP.NET SQLDataSource? - How to share a ASP.NET SQLDataSource acorss different page and project? 我应该如何存储少量事件数据并在两个asp.net页面之间共享? - How should I store a small amount of event data and share it between two asp.net pages? 在ASP.NET应用程序中,我们应在哪里管理会话对象? - Where should we manage session objects in an ASP.NET application? 如何将不同的配置文件添加到asp.net项目? - How do I add different profiles to an asp.net project? 如何在ASP.NET Core中的子域之间共享会话? - How can I share session among subdomains in ASP.NET Core? 我应该在asp.net中声明一个会话变量 - Where I should declare a session variable in asp.net 如何在Asp.net核心中的ViewComponent之间共享ViewData - How share ViewData between ViewComponent in Asp.net core 如何在PHP和ASP.net应用程序之间共享会话? - How to share sessions between PHP and ASP.net application? 如何从JavaScript ASP.NET中的会话获取对象列表? - How to get list of objects from session in javascript ASP.NET?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM