操作使用一个静态类会话数据 [英] Handling session data using a static class

查看:118
本文介绍了操作使用一个静态类会话数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的手哪里有图形用户界面和业务逻辑清晰的分离Windows窗体应用程序。该解决方案是大的(40个项目,几千个类,约2000数据库表)。我的任务是找到如何重用的业务逻辑来构建Web应用程序作为前端解决方案。

I have on my hands a Windows forms application where there is a clear separation of GUI and business logic. The solution is big (40 projects, thousands of classes, about 2000 database tables). My task is to find a solution on how to reuse business logic to build a web application as a front end.

主要的问题是,我认为,我们这是在解决方案使用的所有其他类的静态会话类:

The main issue is, I think, our static Session class which is used by every other class in the solution:

public static class Session
{
    public static string CurrentUser { get; set; }

    public static string CurrentDatabase { get; set; }

    public static string CurrentCompanyProfile { get; set; }

    public static string CurrentLanguage { get; set; }
}

这个类将不会在ASP.NET在那里将被所有用户共享的工作,因为它使用了很多,我不能没有它。

This class will not work in ASP.NET where it will be shared by all users and I cannot replace it since it is used a lot.

我在想,我需要从这个类中删除所有信息,只保留了接口。通过调用属性code的get方法会以某种方式重定向该呼叫HttpContext.Current.Session在那里我将存储其中将包含所有这些信息的一类。

I was thinking that I need to remove all information from this class and keep just the interface. By calling the get method of an property code would somehow redirect this call to HttpContext.Current.Session where I would store a class which would contain all this info.

我不知道如何做到这一点适当考虑会话驻留在其中不会有System.Web程序引用一个程序集,考虑到我不能破坏我们的WinForms实现。

I'm not sure how to do this properly considering that Session resides in an assembly which will not have a reference to System.Web and considering that I must not disrupt our WinForms implementation.

感谢。

推荐答案

假设你可以更新会话类,你可以抽象的Session类属性的存储。在当前的应​​用程序,你可以创建一个简单的内存存储,并且在Web应用程序可以创建一个网络会话存储。

Assuming you can update the Session class, you can abstract the storage of the properties in the Session class. In your current application you can create a simple in-memory storage, and in the web application you can create a web-session storage.

首先定义一个会话值提供者,你在你目前的静态会话类中使用。

First define a session-value-provider, which you use in your current static Session class.

public interface ISessionValueProvider {
    string CurrentUser { get; set; }
    string CurrentDatabase { get; set; }
    string CurrentCompanyProfile { get; set; }
    string CurrentLanguage { get; set; }
}

public static class Session {
    private static ISessionValueProvider _sessionValueProvider;
    public static void SetSessionValueProvider(ISessionValueProvider provider) {
        _sessionValueProvider = provider;
    }

    public static string CurrentUser { 
        get { return _sessionValueProvider.CurrentUser; } 
        set { _sessionValueProvider.CurrentUser = value; } 
    }
    // Etc for the other props
}

比当前应用程序,定义一个使用内存来存储值使sesion值人员。

Than for your current application, define a sesion value provider which uses memory to storage the values.

public class MemorySessionValueProvider: ISessionValueProvider {
    public string CurrentUser {get; set; }
    // Etc for the other props
}

要使用它,创建一个实例,并给它的静态会话类。例如,您可以添加到您的主要方法。

To use it, create an instance and give it to the static session class. For instance, you can add this to your Main method.

Session.SetSessionValueProvider(new MemorySessionValueProvider());

现在的Web应用程序,创建了使用网络的会话的会话值人员。

Now for your web application, create a session value provider which uses the web-session.

public class WebSessionValueProvider: ISessionValueProvider {
    private const string CURRENTUSERKEY = "CurrentUser"; // TODO: Change this if necessary
    public string CurrentUser {
        get { return (string)HttpContext.Current.Session[CURRENTUSERKEY]; }
        set { HttpContext.Current.Session[CURRENTUSERKEY] = value; }
    }

    // Etc for the other props
}

再次,给这个网络的会话值提供商的静态会话类的一个实例。例如,在Global.asax

And again, give an instance of this web-session-value-provider to the static session class. For instance, in the global.asax.

Session.SetSessionValueProvider(new WebSessionValueProvider);

这篇关于操作使用一个静态类会话数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆