forked from PHOENIXCONTACT/MORYX-Framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResourceProxy.cs
120 lines (104 loc) · 3.42 KB
/
ResourceProxy.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright (c) 2023, Phoenix Contact GmbH & Co. KG
// Licensed under the Apache License, Version 2.0
using System.Collections.Generic;
using System.Linq;
using Moryx.AbstractionLayer.Resources;
namespace Moryx.Resources.Management
{
/// <summary>
/// Base type for all proxies
/// </summary>
internal abstract class ResourceProxy : IResource
{
/// <summary>
/// Type controller field to convert references to public resources before returning them
/// </summary>
private IResourceTypeController _typeController;
/// <summary>
/// Target resource of the proxy
/// </summary>
public IResource Target { get; private set; }
/// <summary>
/// Create proxy for a given target
/// </summary>
protected ResourceProxy(IResource target, IResourceTypeController typeController)
{
Target = target;
_typeController = typeController;
}
/// <inheritdoc />
long IResource.Id => Target.Id;
/// <inheritdoc />
string IResource.Name => Target.Name;
public virtual void Attach()
{
}
public virtual void Detach()
{
_typeController = null;
Target = null;
}
public override string ToString()
{
return Target.ToString();
}
/// <summary>
/// Convert a referenced instance to a proxy
/// </summary>
protected internal TResource Convert<TResource>(IResource instance)
where TResource : IResource
{
return (TResource)_typeController.GetProxy((Resource)instance);
}
/// <summary>
/// Convert a collection of referenced resources to proxies
/// </summary>
protected internal TResource[] ConvertMany<TResource>(IEnumerable<IResource> instances)
where TResource : IResource
{
return instances.Select(Convert<TResource>).ToArray();
}
/// <summary>
/// Extract the target object from a proxy
/// </summary>
protected internal static TResource Extract<TResource>(IResource instance)
where TResource : IResource
{
var proxy = (ResourceProxy)instance;
return (TResource)proxy.Target;
}
/// <summary>
/// Extract target objects from collection of proxies
/// </summary>
protected internal static TResource[] ExtractMany<TResource>(IEnumerable<IResource> instances)
where TResource : IResource
{
return instances.Select(Extract<TResource>).ToArray();
}
}
/// <summary>
/// Resource proxy base for typed access to resources
/// </summary>
internal abstract class ResourceProxy<TTarget> : ResourceProxy
where TTarget : Resource
{
/// <summary>
/// Typed access to the target field
/// </summary>
public new TTarget Target
{
get
{
if (base.Target == null)
throw new ProxyDetachedException();
return (TTarget)base.Target;
}
}
/// <summary>
/// Create proxy for a given target
/// </summary>
protected ResourceProxy(TTarget target, IResourceTypeController typeController) : base(target, typeController)
{
}
}
}