从业务逻辑中删除 ASP.net 会话调用

发布于 2024-10-15 18:24:05 字数 222 浏览 6 评论 0原文

我继承了一个 asp.net Web 解决方案,该解决方案将业务逻辑和数据调用作为单独的程序集。在业务层中,有少量的调用来获取/设置 HttpContext 会话值。我四处寻找一个示例,该示例允许我将其从业务逻辑中抽象出来,因为我希望能够在非 Web 项目中重用这些程序集,任何人都可以给我一个最佳方法的示例吗?做这个。我正在考虑某种会话工厂,它将根据使用场景从某种持久存储中获取值,但我确实是架构新手,并且希望能提供一两个指针。

I've inherited an asp.net web solution, which has business logic and data calls as seperate assemblies. In the business layer there are a small number of calls to get/set HttpContext session values. I've looked around for an example that will allow me to abstract this away from the business logic as I'd like to be able to reuse these assemblies in non-web projects, could anyone please give me an example of the best way to do this. I was thinking of some sort of session factory that will obtain values from some sort of persistant store depending on the usage scenario but I'm new to architecture really and would appreciate a pointer or two.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小…楫夜泊 2024-10-22 18:24:05

我认为最简单的方法是创建一个 ISessionProvider 接口,其中包含一个 Dictionary接口。 {得到; }。然后创建一个返回实际会话包内容的公共类 HttpSessionProvider : ISessionProvider

至少,通过手动指定或使用任何 IOC 模式,在 Web 应用程序中实例化此对象并将其提供给后端类。

[编辑]经过一番反思,这不是一个干净的方法。您不会依赖于 asp.net 框架,但您仍然可以访问会话的“asp.net”内容。
在这种情况下,保留接口,但添加特定属性(字符串 customer、int userID 等)或仅返回包含实际业务数据的字典

The simpliest way in my opinion is to create an ISessionProvider interface, with a Dictionary<string, object> {get; }. Then create a public class HttpSessionProvider : ISessionProvider that returns the actual session bag contents.

At least, instantiate this object in your web app and give it to backend class, either by specifyng it manually or by using any IOC pattern.

[Edit] after a small reflexion, it's not a clean way. You won't have dependency to the asp.net framwork, but you will still have the "asp.net" content of the session accessible.
In this case, keep the Interface, but either add specific properties (string customer, int userID, etc.) or return a dictionary only with actual business data

忘你却要生生世世 2024-10-22 18:24:05

我在一些项目中做过类似的事情:

public interface IAppContext {
    string SomeVariable {
        set;
        get;
    }
}
public class HttpContextAppContext : IAppContext {

    public static readonly string CONTEXT_PREFIX = "appcontext_";

    public string SomeVariable {
        set { HttpContext.Current.Session[CONTEXT_PREFIX + "SomeVariable"] = value; }
        get { return (string)HttpContext.Current.Session[CONTEXT_PREFIX + "SomeVariable"]; }
    }
}

I have done something like this on some projects:

public interface IAppContext {
    string SomeVariable {
        set;
        get;
    }
}
public class HttpContextAppContext : IAppContext {

    public static readonly string CONTEXT_PREFIX = "appcontext_";

    public string SomeVariable {
        set { HttpContext.Current.Session[CONTEXT_PREFIX + "SomeVariable"] = value; }
        get { return (string)HttpContext.Current.Session[CONTEXT_PREFIX + "SomeVariable"]; }
    }
}
静若繁花 2024-10-22 18:24:05

业务层跟Session有什么业务呢?为了更好地理解该陈述,请这样想:为什么业务层需要持久保存用户相关信息?

业务层需要合作处理用户相关数据,但不存储它。这意味着当前存储的数据应该被注入,即它应该作为参数传递给需要它的函数。这样做就是形成一个架构合同,它以一种非常明确的方式说“嘿,我需要用户信息来完成我的工作”——你不会在幕后用一些随机的先前存储的值来执行一些魔法。业务层可以使用该数据以某种方式对用户进行身份验证或授权,但在完成身份验证结果后应丢弃这些结果。如果持久化这些值以保存对数据库的一两次调用,那么数据库调用的效率就会出现问题,因为数据库对此类简单事物的调用速度很快。

因此,我的建议是从业务层中删除对 Session 的任何引用,并更改函数签名以包含您需要传递的用户数据。

What business has the business layer got with Session? To understand that statement a little more, think of it this way: why does the business layer need to persist user related info, ever?

The business layer needs to work with and process user related data, but not store it. This means the data that is currently stored should be injected, i.e. it should be passed in as a parameter to the functions that need it. Doing it this way is forming an architectural contract, it is saying "hey, i need that user info to do my job" in a very explicit way - you are not performing some magic under the hood with some random previously stored values. It is fine for the business layer to authenticate or authorise the user in some way using that data, but it should discard the authentication results after it has finished with them. If the values are being persisted to save a call or two to the database, then you have a problem with the efficiency of your database calls, because database calls for simple things like that are quick.

So my suggestion is to remove any references to Session from your business layer, and change your function signatures to include the user data you need passed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文