Skip to content

Commit 0386c2f

Browse files
committed
Merge pull request #56 from WebApiContrib/dev
Dev
2 parents e18208b + 9d421b0 commit 0386c2f

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed

readme.md

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,26 @@ data.Add(new Data { Name = "avatar", Prompt = "Avatar" });
5656
```
5757

5858
## Using extensions
59-
The `Collection`, `Data`, `Item`, `Link` and `Query` classes are all extensible to allow for using CollectionJson extensions. There are two different methods for working with extensions.
59+
The `Collection`, `Data`, `Item`, `Link` and `Query` classes are all extensible to allow for using CollectionJson extensions. There are three different methods for working with extensions.
6060

61-
### Using dynamic
62-
Extensions can be set by casting to dynamic and setting arbitrary extension properties. Below is an example setting the Model extension.
61+
### Using `Extensions`
62+
Extensions can be set by using the `Extensions` method which returns a dynamic object. Below is an example setting the Model extension*.
63+
```csharp
64+
var item = new Item { Href = new Uri(_requestUri, "/friends/" + friend.Id) };
65+
item.Extensions().Model = "friend";
66+
```
67+
68+
*Note: `Extensions` is a method rather than a property to avoid from being serialized, and to make it compatible with multiple serializers.
69+
70+
### Casting to Dynamic
71+
Each of the aforementioned classes can be cast directly to `dynamic`.
6372
```csharp
6473
var item = new Item { Href = new Uri(_requestUri, "/friends/" + friend.Id) };
6574
dynamic dItem = item;
66-
item.Model = "friend";
75+
dItem.Model="friend";
6776
```
6877

78+
6979
### Using SetValue
7080
Extensions can be set by calling the SetValue method.
7181
```csharp

samples/friendapi/Infrastructure/FriendDocumentWriter.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,7 @@ 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";
29+
item.Extensions().Model = "friend";
3330

3431
item.Data.Add(new Data { Name = "full-name", Value = friend.FullName, Prompt = "Full Name" });
3532
item.Data.Add(new Data { Name = "email", Value = friend.Email, Prompt = "Email" });

src/WebApiContrib.CollectionJson/ExtensibleObject.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,10 @@ public void SetValue(string key, object value)
5656
{
5757
Members[key] = value;
5858
}
59+
60+
public dynamic Extensions()
61+
{
62+
return this;
63+
}
5964
}
6065
}

0 commit comments

Comments
 (0)