Tôi đã đưa ra một giải pháp tránh những nhược điểm nhất định của các giải pháp khác được đăng bằng cách giữ nguyên cấu trúc của các biến Session. Nó chỉ đơn giản là một phím tắt an toàn để nhận và thiết lập các biến Session.
Đó là C#, nhưng tôi đã đăng một số VB.NET được tạo tự động ở cuối.
Các giải pháp tốt nhất mà tôi đã thấy (câu trả lời được chấp nhận và câu trả lời của TheObjectGuy) yêu cầu một lớp tùy chỉnh được lưu trữ trong biến Phiên và sau đó được lấy từ Phiên để truy cập các thuộc tính của nó bằng MySessionClass.Current. Tài sản của tôi.
Vấn đề với điều này là nếu bạn hiện đang sử dụng (hoặc có thể sử dụng trong tương lai) một cái gì đó khác với chế độ Phiên-trạng thái phiên bản (xem https://msdn.microsoft.com/en-us/library/ms178586%28v=vs.140%29.aspx), cả lớp sẽ phải trải qua quá trình tuần tự để truy cập bất động sản.
Ngoài ra, điều đó có nghĩa là bạn mất các triển khai IEnumerable và ICollection được cung cấp bởi Phiên thực tế, nếu bạn cần. Với giải pháp của tôi, bạn chỉ cần truy cập vào Phiên thực tế nếu bạn cần chức năng này.
Bạn có thể dễ dàng sử dụng các biến phiên này và chúng là loại an toàn. Nó có thể được sử dụng cùng với các câu lệnh như Session ["MyProperty"], điều này sẽ cho phép chuyển đổi một dự án hiện tại thành một tham chiếu tại một thời điểm. Vì vậy:
int myInt = (int)Session["MyInt"];
Session["MyInt"] = 3;
trở thành:
int myInt = SessionVars.MyInt;
SessionVars.MyInt = 3;
Đây là lớp thực tế. CallerMemberName yêu cầu .NET 4.5, nhưng ngay cả khi bạn đang sử dụng phiên bản cũ hơn, bạn vẫn có thể quản lý nó bằng cách chuyển tên propertyName một cách rõ ràng. Ngoài ra, các loại của các thuộc tính phải nullable để làm cho nó hoạt động hoàn toàn giống như tiêu chuẩn Session [ "MyProp"] gọi vì một tổ chức phi thiết
public static class SessionVars
{
private static T Get2<T>([System.Runtime.CompilerServices.CallerMemberName] string propertyName = "")
{
if (HttpContext.Current.Session[propertyName] == null)
{
return default(T);
}
return (T)HttpContext.Current.Session[propertyName];
}
private static void Set2<T>(T value, [System.Runtime.CompilerServices.CallerMemberName] string propertyName = "")
{
HttpContext.Current.Session[propertyName] = value;
}
public static int MyInt { get { return Get2<int>(); } set { Set2<int>(value); } }
public static bool MyBool { get { return Get2<bool>(); } set { Set2<bool>(value); } }
public static string MyString { get { return Get2<string>(); } set { Set2<string>(value); } }
}
Tôi thậm chí đã viết một đoạn mã để tạo điều kiện thêm các thuộc tính:
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippet Format="1.0.0" xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<Header>
<Title>SessionVars Property</Title>
<Author>kevinpo</Author>
<Shortcut>sv</Shortcut>
<Description>Adds a property for use in a SessionVars class</Description>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>type</ID>
<Default>int</Default>
</Literal>
<Literal>
<ID>property</ID>
<Default>PropertyName</Default>
</Literal>
</Declarations>
<Code Language="CSharp">
<![CDATA[public static $type$ $property$ { get { return Get2<$type$>(); } set { Set2<$type$>(value); } }]]>
</Code>
</Snippet>
</CodeSnippet>
tôi là một anh chàng C#, vì vậy VB.NET đây chỉ là tính năng tự động chuyển đổi bởi http://converter.telerik.com/:
Public NotInheritable Class SessionVars
Private Sub New()
End Sub
Private Shared Function Get2(Of T)(<System.Runtime.CompilerServices.CallerMemberName> Optional propertyName As String = "") As T
If HttpContext.Current.Session(propertyName) Is Nothing Then
Return Nothing
End If
Return DirectCast(HttpContext.Current.Session(propertyName), T)
End Function
Private Shared Sub Set2(Of T)(value As T, <System.Runtime.CompilerServices.CallerMemberName> Optional propertyName As String = "")
HttpContext.Current.Session(propertyName) = value
End Sub
Public Shared Property MyInt() As Integer
Get
Return Get2(Of Integer)()
End Get
Set
Set2(Of Integer)(value)
End Set
End Property
Public Shared Property MyBool() As Boolean
Get
Return Get2(Of Boolean)()
End Get
Set
Set2(Of Boolean)(value)
End Set
End Property
Public Shared Property MyString() As String
Get
Return Get2(Of String)()
End Get
Set
Set2(Of String)(value)
End Set
End Property
End Class
'=======================================================
'Service provided by Telerik (www.telerik.com)
'Conversion powered by NRefactory.
'Twitter: @telerik
'Facebook: facebook.com/telerik
'=======================================================
tôi khuyên bạn nên tái gắn thẻ như phiên biến và phiên bang là tốt. –