Skip to content

Commit 0da73e3

Browse files
committed
Merge pull request #51 from glennblock/dev
Adding support for extensions also renaming test classes
2 parents 542e29f + 5adbaf8 commit 0da73e3

File tree

15 files changed

+533
-231
lines changed

15 files changed

+533
-231
lines changed

samples/friendapi/Infrastructure/FriendDocumentWriter.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ public IReadDocument Write(IEnumerable<Friend> friends)
2626
foreach (var friend in friends)
2727
{
2828
var item = new Item { Href = new Uri(_requestUri, "/friends/" + friend.Id) };
29+
dynamic dItem = item;
30+
31+
//use the model extension
32+
dItem.Model = "friend";
33+
2934
item.Data.Add(new Data { Name = "full-name", Value = friend.FullName, Prompt = "Full Name" });
3035
item.Data.Add(new Data { Name = "email", Value = friend.Email, Prompt = "Email" });
3136
item.Data.Add(new Data{ Name = "short-name", Value = friend.ShortName, Prompt = "Short Name"});

src/WebApiContrib.CollectionJson/Collection.cs

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace WebApiContrib.CollectionJson
77
{
8-
public class Collection
8+
public class Collection : ExtensibleObject
99
{
1010
public Collection()
1111
{
@@ -15,19 +15,41 @@ public Collection()
1515
Template = new Template();
1616
}
1717

18-
public string Version { get; set; }
19-
public Uri Href { get; set; }
20-
public IList<Link> Links { get; private set; }
21-
public IList<Item> Items { get; private set; }
22-
public IList<Query> Queries { get; private set; }
23-
public Template Template { get; private set; }
24-
}
25-
26-
18+
public string Version
19+
{
20+
get { return GetValue<string>("Version"); }
21+
set { SetValue("Version", value); }
22+
}
2723

24+
public Uri Href
25+
{
26+
get { return GetValue<Uri>("Href"); }
27+
set { SetValue("Href", value); }
28+
}
2829

30+
public IList<Link> Links
31+
{
32+
get { return GetValue<IList<Link>>("Links"); }
33+
private set { SetValue("Links", value); }
34+
}
2935

36+
public IList<Item> Items
37+
{
38+
get { return GetValue<IList<Item>>("Items"); }
39+
private set { SetValue("Items", value); }
40+
}
3041

42+
public IList<Query> Queries
43+
{
44+
get { return GetValue<IList<Query>>("Query"); }
45+
private set { SetValue("Query", value); }
46+
}
3147

48+
public Template Template
49+
{
50+
get { return GetValue<Template>("Template"); }
51+
set { SetValue("Template", value); }
52+
}
53+
}
3254

3355
}

src/WebApiContrib.CollectionJson/Data.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,24 @@
55

66
namespace WebApiContrib.CollectionJson
77
{
8-
public class Data
8+
public class Data : ExtensibleObject
99
{
10-
public string Name { get; set; }
11-
public string Value { get; set; }
12-
public string Prompt { get; set; }
10+
public string Name
11+
{
12+
get { return GetValue<string>("Name"); }
13+
set { SetValue("Name", value); }
14+
}
15+
16+
public string Value
17+
{
18+
get { return GetValue<string>("Value"); }
19+
set { SetValue("Value", value); }
20+
}
21+
22+
public string Prompt
23+
{
24+
get { return GetValue<string>("Prompt"); }
25+
set { SetValue("Prompt", value); }
26+
}
1327
}
1428
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Dynamic;
4+
using System.Linq;
5+
using System.Reflection;
6+
using System.Text;
7+
8+
namespace WebApiContrib.CollectionJson
9+
{
10+
public abstract class ExtensibleObject : DynamicObject
11+
{
12+
public ExtensibleObject()
13+
{
14+
Members = new Dictionary<string, object>();
15+
}
16+
17+
protected IDictionary<string, object> Members { get; private set; }
18+
19+
public override bool TryGetMember(GetMemberBinder binder, out object result)
20+
{
21+
var name = binder.Name;
22+
var found = Members.TryGetValue(name, out result);
23+
24+
if (result == null)
25+
return false;
26+
27+
return found;
28+
}
29+
30+
public override bool TrySetMember(SetMemberBinder binder, object value)
31+
{
32+
Members[binder.Name] = value;
33+
return true;
34+
}
35+
36+
public override IEnumerable<string> GetDynamicMemberNames()
37+
{
38+
foreach (var entry in Members)
39+
{
40+
if (entry.Value != null)
41+
yield return entry.Key;
42+
}
43+
}
44+
45+
public T GetValue<T>(string key)
46+
{
47+
object val;
48+
var found = Members.TryGetValue(key, out val);
49+
if (found)
50+
return (T) val;
51+
52+
return default(T);
53+
}
54+
55+
public void SetValue(string key, object value)
56+
{
57+
Members[key] = value;
58+
}
59+
}
60+
}

src/WebApiContrib.CollectionJson/Item.cs

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,43 @@
55

66
namespace WebApiContrib.CollectionJson
77
{
8-
public class Item
8+
public class Item : ExtensibleObject
99
{
1010
public Item()
1111
{
1212
Data = new List<Data>();
1313
Links = new List<Link>();
1414
}
1515

16-
public Uri Href { get; set; }
17-
public string Rel { get; set; }
18-
public string Rt { get; set; }
19-
public IList<Data> Data { get; private set; }
20-
public IList<Link> Links { get; private set; }
16+
public Uri Href
17+
{
18+
get { return GetValue<Uri>("Href"); }
19+
set { SetValue("Href", value); }
20+
}
21+
22+
public string Rel
23+
{
24+
get { return GetValue<string>("Rel"); }
25+
set { SetValue("Rel", value); }
26+
}
27+
28+
public string Rt
29+
{
30+
get { return GetValue<string>("Rt"); }
31+
set { SetValue("Rt", value); }
32+
}
33+
34+
public IList<Data> Data
35+
{
36+
get { return GetValue<IList<Data>>("Data"); }
37+
private set { SetValue("Data", value); }
38+
}
39+
40+
public IList<Link> Links
41+
{
42+
get { return GetValue<IList<Link>>("Links"); }
43+
private set { SetValue("Links", value); }
44+
}
2145

2246
}
2347
}

src/WebApiContrib.CollectionJson/Link.cs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,30 @@
55

66
namespace WebApiContrib.CollectionJson
77
{
8-
public class Link
8+
public class Link : ExtensibleObject
99
{
10-
public string Rel { get; set; }
11-
public Uri Href { get; set; }
12-
public string Prompt { get; set; }
13-
public string Render { get; set; }
10+
public String Rel
11+
{
12+
get { return GetValue<String>("Rel"); }
13+
set { SetValue("Rel", value); }
14+
}
15+
16+
public Uri Href
17+
{
18+
get { return GetValue<Uri>("Href"); }
19+
set { SetValue("Href", value); }
20+
}
21+
22+
public String Prompt
23+
{
24+
get { return GetValue<String>("Prompt"); }
25+
set { SetValue("Promot", value); }
26+
}
27+
28+
public String Render
29+
{
30+
get { return GetValue<String>("Render"); }
31+
set { SetValue("Render", value); }
32+
}
1433
}
1534
}

src/WebApiContrib.CollectionJson/Query.cs

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,41 @@
55

66
namespace WebApiContrib.CollectionJson
77
{
8-
public class Query
8+
public class Query : ExtensibleObject
99
{
1010
public Query()
1111
{
1212
Data = new List<Data>();
1313
}
1414

15-
public string Rt { get; set; }
16-
public string Rel { get; set; }
17-
public Uri Href { get; set; }
18-
public string Prompt { get; set; }
19-
public IList<Data> Data { get; private set; }
15+
public String Rt
16+
{
17+
get { return GetValue<String>("Rt"); }
18+
set { SetValue("Rt", value); }
19+
}
20+
21+
public String Rel
22+
{
23+
get { return GetValue<String>("Rel"); }
24+
set { SetValue("Rel", value); }
25+
}
26+
27+
public Uri Href
28+
{
29+
get { return GetValue<Uri>("Href"); }
30+
set { SetValue("Href", value); }
31+
}
32+
33+
public string Prompt
34+
{
35+
get { return GetValue<String>("Prompt"); }
36+
set { SetValue("Prompt", value); }
37+
}
38+
39+
public IList<Data> Data
40+
{
41+
get { return GetValue<IList<Data>>("Data"); }
42+
set { SetValue("Data", value); }
43+
}
2044
}
2145
}

src/WebApiContrib.CollectionJson/WebApiContrib.CollectionJson.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
<Compile Include="CollectionJsonDocumentWriterExtensions.cs" />
4848
<Compile Include="Data.cs" />
4949
<Compile Include="Error.cs" />
50+
<Compile Include="ExtensibleObject.cs" />
5051
<Compile Include="ICollectionJsonDocumentReader_Of_T.cs" />
5152
<Compile Include="ICollectionJsonDocumentWriter_Of_T.cs" />
5253
<Compile Include="IEnumerable_Of_DataExtensions.cs" />

0 commit comments

Comments
 (0)