Datei: NDODLL/NDOMetaclass.cs
Last Commit (9de9fa9)
1 | using System; |
2 | using System.Linq; |
3 | using System.Reflection; |
4 | using System.Collections.Generic; |
5 | using System.Text; |
6 | using NDO.Configuration; |
7 | |
8 | namespace NDO |
9 | { |
10 | ····/// <summary> |
11 | /// This class overrides the built in Metaclass, generated by the NDOEnhancer, |
12 | ····/// in order to support resolving constructor parameters. |
13 | ····/// </summary> |
14 | class NDOMetaclass : IMetaClass |
15 | ····{ |
16 | ········private readonly Type t; |
17 | ········private readonly IMetaClass innerClass; |
18 | ········private readonly INDOContainer configContainer; |
19 | ········private ConstructorInfo constructor = null; |
20 | ········private bool isDefaultConstructor = false; |
21 | |
22 | public NDOMetaclass( Type t, IMetaClass innerClass, INDOContainer configContainer) |
23 | ········{ |
24 | ············this.t = t; |
25 | ············this.innerClass = innerClass; |
26 | ············this.configContainer = configContainer; |
27 | ········} |
28 | |
29 | public IPersistenceCapable CreateObject( ) |
30 | ········{ |
31 | if ( isDefaultConstructor) |
32 | ················return innerClass.CreateObject(); |
33 | |
34 | ············List<object> parameters = new List<object>(); |
35 | |
36 | ············if (this.constructor == null) |
37 | ············{ |
38 | ················var registrations = this.configContainer.Registrations; |
39 | ················foreach (var constr in t.GetConstructors()) |
40 | ················{ |
41 | ····················bool canResolve = true; |
42 | ····················parameters.Clear(); |
43 | ···················· |
44 | ····················foreach (var p in constr.GetParameters()) |
45 | ····················{ |
46 | ························if (!registrations.Any(r=>r.Key == p.ParameterType)) |
47 | ························{ |
48 | ····························canResolve = false; |
49 | ····························break; |
50 | ························} |
51 | ····················} |
52 | |
53 | if ( canResolve) |
54 | { |
55 | ························this.constructor = constr; |
56 | ························isDefaultConstructor = constr.GetParameters().Length == 0; |
57 | ························break; |
58 | ····················} |
59 | ················} |
60 | ············} |
61 | |
62 | ············if (constructor == null) |
63 | ················throw new NDOException( 116, $"Cant find a resolvable constructor for class '{t.FullName}'" ); |
64 | |
65 | ············foreach (var p in this.constructor.GetParameters()) |
66 | ············{ |
67 | ················var o = this.configContainer.Resolve( p.ParameterType ); |
68 | ················parameters.Add( o ); |
69 | ············} |
70 | |
71 | ············return (IPersistenceCapable)this.constructor.Invoke( parameters.ToArray() ); |
72 | ········} |
73 | |
74 | ········public int GetRelationOrdinal( string fieldName ) |
75 | ········{ |
76 | ············return innerClass.GetRelationOrdinal( fieldName ); |
77 | ········} |
78 | ····} |
79 | } |
80 |
New Commit (a99de3b)
1 | using System; |
2 | using System.Linq; |
3 | using System.Reflection; |
4 | using System.Collections.Generic; |
5 | using System.Text; |
6 | using NDO.Configuration; |
7 | |
8 | namespace NDO |
9 | { |
10 | ····/// <summary> |
11 | /// This class wraps the MetaClass generated by old versions of the NDOEnhancer, |
12 | ····/// in order to support resolving constructor parameters. |
13 | ····/// </summary> |
14 | class NDOMetaclass : MetaclassBase |
15 | ····{ |
16 | ········private readonly IMetaClass innerClass; |
17 | |
18 | public NDOMetaclass( Type t, IMetaClass innerClass) : base( t) |
19 | ········{ |
20 | ············this.innerClass = innerClass; |
21 | ········} |
22 | |
23 | /// <inheritdoc/> |
24 | ········public override IPersistenceCapable CreateObject() |
25 | ········{ |
26 | //if ( IsDefaultConstructor) |
27 | ············return innerClass.CreateObject(); |
28 | //throw new Exception( "Can't use CreateObject( ) if the constructor expects parameters. Use CreateObject( INDOContainer configContainer) instead. " ) ; |
29 | ········} |
30 | |
31 | /// <inheritdoc/> |
32 | public override int GetRelationOrdinal( string fieldName ) |
33 | ········{ |
34 | ············return innerClass.GetRelationOrdinal( fieldName ); |
35 | ········} |
36 | ····} |
37 | } |
38 |