Base/Resolver.cs
1 //
2 // Copyright (c) Microsoft. All rights reserved.
3 // Licensed under the MIT license.
4 //
5 // Microsoft Bot Framework: http://botframework.com
6 //
7 // Bot Builder SDK GitHub:
8 // https://github.com/Microsoft/BotBuilder
9 //
10 // Copyright (c) Microsoft Corporation
11 // All rights reserved.
12 //
13 // MIT License:
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 //
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 //
25 // THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33 
35 using System;
36 using System.Collections.Generic;
37 using System.Linq;
38 using System.Text;
39 using System.Threading;
40 using System.Threading.Tasks;
41 
42 namespace Microsoft.Bot.Builder.Scorables.Internals
43 {
50  public interface IResolver
51  {
52  bool TryResolve(Type type, object tag, out object value);
53  }
54 
55  public delegate bool TryResolve(Type type, object tag, out object value);
56 
57  public static partial class Extensions
58  {
59  public static bool CanResolve(this IResolver resolver, Type type, object tag)
60  {
61  object value;
62  return resolver.TryResolve(type, tag, out value);
63  }
64 
65  public static object Resolve(this IResolver resolver, Type type, object tag)
66  {
67  object value;
68  if (!resolver.TryResolve(type, null, out value))
69  {
70  throw new ArgumentOutOfRangeException();
71  }
72 
73  return value;
74  }
75 
76  public static bool TryResolve<T>(this IResolver resolver, object tag, out T value)
77  {
78  object inner;
79  if (resolver.TryResolve(typeof(T), tag, out inner))
80  {
81  value = (T)inner;
82  return true;
83  }
84  else
85  {
86  value = default(T);
87  return false;
88  }
89  }
90  }
91 
92  public sealed class NullResolver : IResolver
93  {
94  public static readonly IResolver Instance = new NullResolver();
95 
96  private NullResolver()
97  {
98  }
99 
100  bool IResolver.TryResolve(Type type, object tag, out object value)
101  {
102  value = null;
103  return false;
104  }
105  }
106 
107  public sealed class NoneResolver : IResolver
108  {
109  public static readonly IResolver Instance = new NoneResolver();
110 
111  private NoneResolver()
112  {
113  }
114 
115  public static readonly object BoxedToken = CancellationToken.None;
116 
117  bool IResolver.TryResolve(Type type, object tag, out object value)
118  {
119  if (typeof(CancellationToken).IsAssignableFrom(type))
120  {
121  value = BoxedToken;
122  return true;
123  }
124 
125  value = null;
126  return false;
127  }
128  }
129 
130  public abstract class DelegatingResolver : IResolver
131  {
132  protected readonly IResolver inner;
133 
134  protected DelegatingResolver(IResolver inner)
135  {
136  SetField.NotNull(out this.inner, nameof(inner), inner);
137  }
138 
139  public virtual bool TryResolve(Type type, object tag, out object value)
140  {
141  return inner.TryResolve(type, tag, out value);
142  }
143  }
144 
145  public sealed class EnumResolver : DelegatingResolver
146  {
147  public EnumResolver(IResolver inner)
148  : base(inner)
149  {
150  }
151 
152  public override bool TryResolve(Type type, object tag, out object value)
153  {
154  if (type.IsEnum)
155  {
156  var name = tag as string;
157  if (name != null)
158  {
159  if (Enum.IsDefined(type, name))
160  {
161  value = Enum.Parse(type, name);
162  return true;
163  }
164  }
165  }
166 
167  return base.TryResolve(type, tag, out value);
168  }
169  }
170 
171  public sealed class ArrayResolver : DelegatingResolver
172  {
173  private readonly IReadOnlyList<object> services;
174 
175  public ArrayResolver(IResolver inner, IReadOnlyList<object> services)
176  : base(inner)
177  {
178  SetField.NotNull(out this.services, nameof(services), services);
179  }
180 
181  public ArrayResolver(IResolver inner, params object[] services)
182  : this(inner, (IReadOnlyList<object>)services)
183  {
184  }
185 
186  public override bool TryResolve(Type type, object tag, out object value)
187  {
188  if (tag == null)
189  {
190  for (int index = 0; index < this.services.Count; ++index)
191  {
192  var service = this.services[index];
193  if (service != null)
194  {
195  var serviceType = service.GetType();
196  if (type.IsAssignableFrom(serviceType))
197  {
198  value = service;
199  return true;
200  }
201  }
202  }
203  }
204 
205  return base.TryResolve(type, tag, out value);
206  }
207  }
208 }
override bool TryResolve(Type type, object tag, out object value)
ArrayResolver(IResolver inner, IReadOnlyList< object > services)
override bool TryResolve(Type type, object tag, out object value)
delegate bool TryResolve(Type type, object tag, out object value)
static bool CanResolve(this IResolver resolver, Type type, object tag)
static object Resolve(this IResolver resolver, Type type, object tag)
virtual bool TryResolve(Type type, object tag, out object value)
Allow the resolution of values based on type and optionally tag.
Namespace for internal machinery that is not useful for most developers and may change in the future...
bool TryResolve(Type type, object tag, out object value)
Namespace for the internal fibers machinery that is not useful for most developers and may change in ...
Root namespace for the Microsoft Bot Builder SDK.
ArrayResolver(IResolver inner, params object[] services)