-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAttachedNameUntypedExample.cs
60 lines (53 loc) · 1.8 KB
/
AttachedNameUntypedExample.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using System.Diagnostics;
using FluentAssertions;
using Xunit;
namespace MicroElements.Metadata.Tests.examples
{
/// <summary>
/// Example of using metadata for any object that does not implements IMetadataProvider.
/// </summary>
public static class AttachedNameUntypedExample
{
/// <summary>
/// Sets AttachedName to an object.
/// </summary>
public static void SetAttachedName(this object target, string name)
{
target.AsMetadataProvider().SetMetadata("AttachedName", name);
}
/// <summary>
/// Sets AttachedName and returns the same object.
/// </summary>
public static T WithAttachedName<T>(this T target, string name) where T : class
{
target.AsMetadataProvider().SetMetadata("AttachedName", name);
return target;
}
/// <summary>
/// Gets AttachedName.
/// </summary>
public static string GetAttachedName(this object target)
{
return target.AsMetadataProvider().GetMetadata<string>("AttachedName");
}
}
public class AttachedNameUsage
{
[DebuggerTypeProxy(typeof(MetadataProviderDebugView))]
public class MyEntity
{
}
[Fact]
public void attached_name_should_be_get_and_set()
{
// Sets AttachedName to an object.
MyEntity instance1 = new MyEntity();
instance1.SetAttachedName("Instance1");
// Sets AttachedName to an object (other way).
MyEntity instance2 = new MyEntity().WithAttachedName("Instance2");
// Get AttachedName
instance1.GetAttachedName().Should().Be("Instance1");
instance2.GetAttachedName().Should().Be("Instance2");
}
}
}