Datei: NDODLL/MetaclassBase.cs

Last Commit (9de9fa9)
1 -- File didn't exist --
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 is the base class for the class MetaClass, generated by the NDOEnhancer,
12 ····/// in order to support resolving constructor parameters.
13 ····/// </summary>
14 ····public abstract class MetaclassBase : IMetaClass2
15 ····{
16 ········private readonly Type t;
17 ········private ConstructorInfo constructor = null;
18 ········private bool isDefaultConstructor = false;
19
20 ········/// <summary>
21 ········/// Constructs a NDOMetaclass object
22 ········/// </summary>
23 ········/// <param name="t"></param>
24 ········public MetaclassBase( Type t )
25 ········{
26 ············this.t = t;
27 ········}
28
29 ········// <summary>
30 ········// Determines, if the underlying type has a default constructor.
31 ········// </summary>
32 ········//protected bool IsDefaultConstructor => isDefaultConstructor;
33
34 ········/// <inheritdoc/>
35 ········public virtual IPersistenceCapable CreateObject(INDOContainer configContainer)
36 ········{
37 ············if (isDefaultConstructor)
38 ················return CreateObject();
39
40 ············List<object> parameters = new List<object>();
41
42 ············if (this.constructor == null)
43 ············{
44 ················var registrations = configContainer.Registrations;
45 ················foreach (var constr in t.GetConstructors().OrderBy( c => c.GetParameters().Length ))
46 ················{
47 ····················bool canResolve = true;
48 ····················parameters.Clear();
49 ····················
50 ····················foreach (var p in constr.GetParameters())
51 ····················{
52 ························if (!registrations.Any(r=>r.Key == p.ParameterType))
53 ························{
54 ····························canResolve = false;
55 ····························break;
56 ························}
57 ····················}
58
59 ····················if (canResolve)
60 ····················{
61 ························this.constructor = constr;
62 ························isDefaultConstructor = constr.GetParameters().Length == 0;
63 ························break;
64 ····················}
65 ················}
66 ············}
67
68 ············if (constructor == null)
69 ················throw new NDOException( 116, $"Cant find a resolvable constructor for class '{t.FullName}'" );
70
71 ············foreach (var p in this.constructor.GetParameters())
72 ············{
73 ················var o = configContainer.Resolve( p.ParameterType );
74 ················parameters.Add( o );
75 ············}
76
77 ············return (IPersistenceCapable)this.constructor.Invoke( parameters.ToArray() );
78 ········}
79
80 ········/// <inheritdoc/>
81 ········public abstract IPersistenceCapable CreateObject();
82
83 ········/// <inheritdoc/>
84 ········public abstract int GetRelationOrdinal( string fieldName );
85
86 ····}
87 }
88