Skip to content
This repository was archived by the owner on Nov 20, 2018. It is now read-only.

Commit 114d834

Browse files
committed
Add Session feature, object model, etc..
1 parent dc600a6 commit 114d834

File tree

12 files changed

+267
-17
lines changed

12 files changed

+267
-17
lines changed

src/Microsoft.AspNet.FeatureModel/FeatureCollection.cs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,8 @@ public object GetInterface()
3131
return GetInterface(null);
3232
}
3333

34-
public object GetInterface(Type type)
34+
public object GetInterface([NotNull] Type type)
3535
{
36-
if (type == null) throw new ArgumentNullException("type");
3736
object feature;
3837
if (_featureByFeatureType.TryGetValue(type, out feature))
3938
{
@@ -63,10 +62,8 @@ public object GetInterface(Type type)
6362
return null;
6463
}
6564

66-
void SetInterface(Type type, object feature)
65+
void SetInterface([NotNull] Type type, object feature)
6766
{
68-
if (type == null) throw new ArgumentNullException("type");
69-
7067
if (feature == null)
7168
{
7269
Remove(type);
@@ -153,27 +150,22 @@ public bool IsReadOnly
153150
get { return false; }
154151
}
155152

156-
public bool ContainsKey(Type key)
153+
public bool ContainsKey([NotNull] Type key)
157154
{
158-
if (key == null) throw new ArgumentNullException("key");
159155
return GetInterface(key) != null;
160156
}
161157

162-
public void Add(Type key, object value)
158+
public void Add([NotNull] Type key, [NotNull] object value)
163159
{
164-
if (key == null) throw new ArgumentNullException("key");
165-
if (value == null) throw new ArgumentNullException("value");
166160
if (ContainsKey(key))
167161
{
168162
throw new ArgumentException();
169163
}
170164
SetInterface(key, value);
171165
}
172166

173-
public bool Remove(Type key)
167+
public bool Remove([NotNull] Type key)
174168
{
175-
if (key == null) throw new ArgumentNullException("key");
176-
177169
lock (_containerSync)
178170
{
179171
Type priorFeatureType;
@@ -188,7 +180,7 @@ public bool Remove(Type key)
188180
}
189181
}
190182

191-
public bool TryGetValue(Type key, out object value)
183+
public bool TryGetValue([NotNull] Type key, out object value)
192184
{
193185
value = GetInterface(key);
194186
return value != null;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
6+
namespace Microsoft.AspNet.FeatureModel
7+
{
8+
[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false)]
9+
internal sealed class NotNullAttribute : Attribute
10+
{
11+
}
12+
}

src/Microsoft.AspNet.FeatureModel/project.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
{
22
"version": "1.0.0-*",
3-
"dependencies": {},
3+
"dependencies": {
4+
"Microsoft.Framework.Runtime.Interfaces": "1.0.0-*"
5+
},
46
"frameworks": {
57
"aspnet50": {},
68
"aspnetcore50": {
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Text;
6+
7+
namespace Microsoft.AspNet.Http
8+
{
9+
public static class SessionCollectionExtensions
10+
{
11+
public static void SetInt(this ISessionCollection session, string key, int value)
12+
{
13+
var bytes = new byte[]
14+
{
15+
(byte)(value >> 24),
16+
(byte)(0xFF & (value >> 16)),
17+
(byte)(0xFF & (value >> 8)),
18+
(byte)(0xFF & value)
19+
};
20+
session.Set(key, bytes);
21+
}
22+
23+
public static int? GetInt(this ISessionCollection session, string key)
24+
{
25+
var data = session.Get(key);
26+
if (data == null || data.Length < 4)
27+
{
28+
return null;
29+
}
30+
return data[0] << 24 | data[1] << 16 | data[2] << 8 | data[3];
31+
}
32+
33+
public static void SetString(this ISessionCollection session, string key, string value)
34+
{
35+
session.Set(key, Encoding.UTF8.GetBytes(value));
36+
}
37+
38+
public static string GetString(this ISessionCollection session, string key)
39+
{
40+
var data = session.Get(key);
41+
if (data == null)
42+
{
43+
return null;
44+
}
45+
return Encoding.UTF8.GetString(data);
46+
}
47+
48+
public static byte[] Get(this ISessionCollection session, string key)
49+
{
50+
byte[] value = null;
51+
session.TryGetValue(key, out value);
52+
return value;
53+
}
54+
55+
public static void Set(this ISessionCollection session, string key, byte[] value)
56+
{
57+
session.Set(key, new ArraySegment<byte>(value));
58+
}
59+
}
60+
}

src/Microsoft.AspNet.Http.Extensions/project.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
},
1010
"aspnetcore50" : {
1111
"dependencies": {
12-
"System.Reflection": "4.0.10-beta-*",
13-
"System.Reflection.Extensions": "4.0.0-beta-*",
1412
"System.Reflection.TypeExtensions": "4.0.0-beta-*",
1513
"System.Runtime": "4.0.20-beta-*"
1614
}

src/Microsoft.AspNet.Http/HttpContext.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ public abstract class HttpContext : IDisposable
2828

2929
public abstract CancellationToken RequestAborted { get; }
3030

31+
public abstract ISessionCollection Session { get; }
32+
3133
public abstract bool IsWebSocketRequest { get; }
3234

3335
public abstract IList<string> WebSocketRequestedProtocols { get; }
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
7+
namespace Microsoft.AspNet.Http
8+
{
9+
public interface ISessionCollection : IEnumerable<KeyValuePair<string, byte[]>>
10+
{
11+
byte[] this[string key] { get; set; }
12+
13+
bool TryGetValue(string key, out byte[] value);
14+
15+
void Set(string key, ArraySegment<byte> value);
16+
17+
void Remove(string key);
18+
19+
void Clear();
20+
}
21+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using Microsoft.Framework.Runtime;
7+
8+
namespace Microsoft.AspNet.HttpFeature
9+
{
10+
[AssemblyNeutral]
11+
public interface ISession
12+
{
13+
void Load();
14+
15+
void Commit();
16+
17+
bool TryGetValue(string key, out byte[] value);
18+
19+
void Set(string key, ArraySegment<byte> value);
20+
21+
void Remove(string key);
22+
23+
void Clear();
24+
25+
IEnumerable<string> Keys { get; }
26+
}
27+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using Microsoft.Framework.Runtime;
6+
7+
namespace Microsoft.AspNet.HttpFeature
8+
{
9+
[AssemblyNeutral]
10+
public interface ISessionFactory
11+
{
12+
bool IsAvailable { get; }
13+
14+
ISession Create();
15+
}
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using Microsoft.Framework.Runtime;
5+
6+
namespace Microsoft.AspNet.HttpFeature
7+
{
8+
// TODO: Is there any reason not to flatten the Factory down into the Feature?
9+
[AssemblyNeutral]
10+
public interface ISessionFeature
11+
{
12+
ISessionFactory Factory { get; set; }
13+
14+
ISession Session { get; set; }
15+
}
16+
}

0 commit comments

Comments
 (0)