My favorites | Sign in
Project Home Downloads Wiki Issues Source
Repository:
Checkout   Browse   Changes   Clones  
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
//
// Copyright 2011 Ekon Benefits
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Text;
using ImpromptuInterface.Internal.Support;
using ImpromptuInterface.Optimization;
using System.Reflection;

namespace ImpromptuInterface.Dynamic
{

/// <summary>
/// Interface for simplistic builder options
/// </summary>
public interface IImpromptuBuilder
{

/// <summary>
/// Creates a prototype list
/// </summary>
/// <param name="contents">The contents.</param>
/// <returns></returns>
dynamic List(params dynamic[] contents);

/// <summary>
/// Setup List or Array, takes either one <see cref="Activate"/> or a list of constructor args that will use objects Type
/// </summary>
/// <param name="constructorArgs">The constructor args.</param>
/// <returns></returns>
dynamic ListSetup(params dynamic[] constructorArgs);

/// <summary>
/// Setup List or Array if list has a default constrcutor
/// </summary>
/// <typeparam name="TList"></typeparam>
/// <returns></returns>
dynamic ListSetup<TList>();

/// <summary>
/// Setup List or Array, takes either one <see cref="Activate"/> or a list of constructor args that will use objects Type
/// </summary>
/// <param name="constructorArgsFactory">The constructor args factory.</param>
/// <returns></returns>
dynamic ListSetup(Func<object[]> constructorArgsFactory);

/// <summary>
/// Setup List or Array if list has a default constrcutor
/// </summary>
/// <typeparam name="TList"></typeparam>
/// <returns></returns>
dynamic ArraySetup<TList>();

/// <summary>
/// Alternative name for <see cref="ListSetup(object[])"/>
/// </summary>
/// <param name="constructorArgs">The constructor args.</param>
/// <returns></returns>
dynamic ArraySetup(params dynamic[] constructorArgs);


/// <summary>
/// Alternative name for <see cref="ListSetup{TList}"/>
/// </summary>
/// <param name="constructorArgsFactory">The constructor args factory.</param>
/// <returns></returns>
dynamic ArraySetup(Func<object[]> constructorArgsFactory);


/// <summary>
/// Alternative name for <see cref="List"/>
/// </summary>
/// <param name="contents">The contents.</param>
/// <returns></returns>
dynamic Array(params dynamic[] contents);

/// <summary>
/// Generates Object, use by calling with named arguments <code>builder.Object(Prop1:"test",Prop2:"test")</code>
/// returns new object;
/// </summary>
dynamic Object { get; }

/// <summary>
/// Sets up object builder
/// </summary>
/// <param name="constructorArgs">The constructor args.</param>
/// <returns></returns>
dynamic ObjectSetup(params dynamic[] constructorArgs);

/// <summary>
/// Sets up object builder
/// </summary>
/// <param name="constructorArgsFactory"></param>
/// <returns></returns>
dynamic ObjectSetup(Func<object[]> constructorArgsFactory);

/// <summary>
/// Setups up named builders
/// </summary>
/// <value>The setup.</value>
dynamic Setup { get; }
}

/// <summary>
/// Builds Expando-Like Objects with an inline Syntax
/// </summary>
/// <typeparam name="TObjectProtoType">The type of the object proto type.</typeparam>
public class ImpromptuBuilder<TObjectProtoType>: ImpromptuObject, IImpromptuBuilder
{
/// <summary>
/// Build factory storage
/// </summary>
protected IDictionary<string,Activate> _buildType;

/// <summary>
/// Initializes a new instance of the <see cref="ImpromptuBuilder&lt;TObjectProtoType&gt;"/> class.
/// </summary>
public ImpromptuBuilder(){
_buildType = new Dictionary<string, Activate>();
Setup = new SetupTrampoline(this);
Object = new BuilderTrampoline(this);
}

/// <summary>
/// Creates a prototype list
/// </summary>
/// <param name="contents">The contents.</param>
/// <returns></returns>
public dynamic List(params dynamic[] contents)
{
Activate tBuildType;
if (!_buildType.TryGetValue("List", out tBuildType))
tBuildType = null;

if (tBuildType != null)
{
dynamic tList = tBuildType.Create();

if (contents != null)
{
foreach (var item in contents)
{
tList.Add(item);
}
}
return tList;
}

return new ImpromptuList(contents);
}



public dynamic ListSetup(params dynamic[] constructorArgs)
{
var tActivate =constructorArgs.OfType<Activate>().SingleOrDefault();


if (tActivate == null)
{

if (!_buildType.TryGetValue("Object", out tActivate))
tActivate = null;
if (tActivate != null)
{
tActivate = new Activate(tActivate.Type,constructorArgs);
}
if(tActivate == null)
tActivate = new Activate<ImpromptuList>(constructorArgs);
}

_buildType["List"] = tActivate;
_buildType["Array"] = tActivate;
return this;
}

public dynamic ListSetup<TList>()
{
return ListSetup(new Activate<TList>());
}

public dynamic ListSetup(Func<object[]> constructorArgsFactory)
{
return ListSetup((object)constructorArgsFactory);
}

public dynamic ArraySetup<TList>()
{
return ListSetup(new Activate<TList>());
}

public dynamic ArraySetup(params dynamic[] constructorArgs)
{
return ListSetup(constructorArgs);
}

public dynamic ArraySetup(Func<object[]> constructorArgsFactory)
{
return ListSetup((object)constructorArgsFactory);
}

/// <summary>
/// Alternative name for <see cref="List"/>
/// </summary>
/// <param name="contents">The contents.</param>
/// <returns></returns>
public dynamic Array(params dynamic[] contents)
{
return List(contents);
}

/// <summary>
/// Creates a Prototype object.
/// </summary>
/// <value>The object.</value>
public dynamic Object { get; private set; }

/// <summary>
/// Sets up object builder
/// </summary>
/// <param name="constructorArgs">The constructor args.</param>
/// <returns></returns>
public dynamic ObjectSetup(params dynamic[] constructorArgs)
{
_buildType["Object"] = new Activate<TObjectProtoType>(constructorArgs);
return this;
}

/// <summary>
/// Sets up object builder
/// </summary>
/// <param name="constructorArgsFactory"></param>
/// <returns></returns>
public dynamic ObjectSetup(Func<object[]> constructorArgsFactory)
{
return ObjectSetup((object) constructorArgsFactory);
}

/// <summary>
/// Trapoline for setting up Builders
/// </summary>
public dynamic Setup { get; private set; }


///<summary>
/// Trampoline for builder
///</summary>
public class BuilderTrampoline:DynamicObject,ICustomTypeProvider
{
ImpromptuBuilder<TObjectProtoType> _buider;

/// <summary>
/// Initializes a new instance of the <see cref="ImpromptuBuilder&lt;TObjectProtoType&gt;.BuilderTrampoline"/> class.
/// </summary>
/// <param name="builder">The builder.</param>
public BuilderTrampoline(ImpromptuBuilder<TObjectProtoType> builder)
{
_buider = builder;
}

public override bool TryInvoke(InvokeBinder binder, object[] args, out object result)
{
Activate tBuildType;
if (!_buider._buildType.TryGetValue("Object", out tBuildType))
tBuildType = null;

result = InvokeHelper(binder.CallInfo, args, tBuildType);
return true;
}
#if SILVERLIGHT5
public Type GetCustomType()
{
return this.GetDynamicCustomType();
}
#endif
}

/// <summary>
/// Trampoline for setup builder
/// </summary>
public class SetupTrampoline : DynamicObject, ICustomTypeProvider
{
ImpromptuBuilder<TObjectProtoType> _buider;

/// <summary>
/// Initializes a new instance of the <see cref="ImpromptuBuilder&lt;TObjectProtoType&gt;.SetupTrampoline"/> class.
/// </summary>
/// <param name="builder">The builder.</param>
public SetupTrampoline(ImpromptuBuilder<TObjectProtoType> builder){
_buider = builder;
}

public override bool TryInvoke(InvokeBinder binder, dynamic[] args, out object result)
{
if (binder.CallInfo.ArgumentNames.Count != binder.CallInfo.ArgumentCount)
throw new ArgumentException("Requires argument names for every argument");
var tArgs = args.Select(it => it is Type ? new Activate(it) : (Activate) it);
foreach (var tKeyPair in binder.CallInfo.ArgumentNames.Zip(tArgs, (n, a) => new KeyValuePair<string, Activate>(n, a)))
{
_buider._buildType[tKeyPair.Key]=tKeyPair.Value;
}
result = _buider;
return true;
}
#if SILVERLIGHT5
public Type GetCustomType()
{
return this.GetDynamicCustomType();
}
#endif
}

public override bool TrySetMember(SetMemberBinder binder, dynamic value){
if (value != null)
{
if (value is Type)
{
_buildType[binder.Name] = new Activate(value);
return true;
}

if (value is Activate)
{
_buildType[binder.Name] = value;
return true;
}
}
else
{
_buildType[binder.Name] = null;
return true;
}
return false;
}

public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
{
Type tType;

Activate tBuildType;
if(!_buildType.TryGetValue(binder.Name, out tBuildType))
tBuildType = null;

if (tBuildType == null && !_buildType.TryGetValue("Object", out tBuildType))
tBuildType = null;

result = InvokeHelper(binder.CallInfo, args,tBuildType);
if (TryTypeForName(binder.Name, out tType))
{
if (tType.IsInterface && result != null && !tType.IsAssignableFrom(result.GetType()))
{
result = Impromptu.DynamicActLike(result, tType);
}
}
return true;

}

private static object InvokeHelper(CallInfo callinfo, IList<object> args, Activate buildType =null)
{

bool tSetWithName = true;
object tArg = null;
if (callinfo.ArgumentNames.Count == 0 && callinfo.ArgumentCount == 1)
{
tArg =args[0];

if (Util.IsAnonymousType(tArg) || tArg is IEnumerable<KeyValuePair<string, object>>)
{
tSetWithName = false;
}
}

if (tSetWithName && callinfo.ArgumentNames.Count != callinfo.ArgumentCount)
throw new ArgumentException("Requires argument names for every argument");
object result;
if (buildType != null)
{
result = buildType.Create();
}
else{
try
{
result = Activator.CreateInstance<TObjectProtoType>();//Try first because faster but doens't work with optional parameters
}
catch (MissingMethodException)
{
result = Impromptu.InvokeConstructor(typeof (TObjectProtoType));
}

}
if(tSetWithName)
{
tArg = callinfo.ArgumentNames.Zip(args, (n, a) => new KeyValuePair<string, object>(n, a));
}

return Impromptu.InvokeSetAll(result, tArg);
}
}
}

Change log

00ae70447bcc by jayt <jay.tu...@ekonbenefits.com> on Aug 27, 2012   Diff
Fixed line endings and added git ingore
Go to: 
Project members, sign in to write a code review

Older revisions

8f5b7bc427cd by jayt <n...@none> on Aug 2, 2012   Diff
Fixed more missing binding code.
600a46899d29 by jayt <n...@none> on Jul 21, 2011   Diff
lots of warning removals mostly due to
documentation

--HG--
branch : dev
9dcf5078cbea by jayt <n...@none> on Jul 8, 2011   Diff
Edited down invokesetall, cleaned up
builder using invokesetall.

--HG--
branch : dev
All revisions of this file

File info

Size: 13744 bytes, 415 lines
Powered by Google Project Hosting