My favorites | Sign in
Project Home Downloads Issues Source
Checkout   Browse   Changes    
 
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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;

namespace Konstruktor
{
interface IBuilder
{
object build(Type t, IScope scope);
}

public sealed partial class Builder : IBuilder
{
readonly Dictionary<Type, Func<IScope, object>> _explicitGenerators = new Dictionary<Type, Func<IScope, object>>();
readonly Dictionary<Type, Type> _interfaceToImplementation = new Dictionary<Type, Type>();
readonly Dictionary<Type, MethodInfo> _generatorMethods = new Dictionary<Type, MethodInfo>();
readonly Dictionary<Type, List<Func<IScope, object>>> _constructorArguments = new Dictionary<Type,List<Func<IScope, object>>>();
readonly Dictionary<Type, Type[]> _preferredConstructor = new Dictionary<Type, Type[]>();

#if DEBUG
bool _frozen;
#endif

public void generator<GeneratedT>(Func<IScope, GeneratedT> constructor)
{
_explicitGenerators.Add(typeof(GeneratedT), scope => constructor(scope));
}

[Obsolete("Use forInterface<>()")]
public void iface<InterfaceT, ImplementationT>()
{
var iType = typeof (InterfaceT);
var cType = typeof (ImplementationT);
Debug.Assert(iType.IsAssignableFrom(cType));
iface(iType, cType);
}

public ForInterface<InterfaceT> forInterface<InterfaceT>()
where InterfaceT:class
{
return new ForInterface<InterfaceT>(this);
}

public sealed class ForInterface<InterfaceT>
where InterfaceT:class
{
readonly Builder _builder;

public ForInterface(Builder builder)
{
_builder = builder;
}

public void use<ImplementationT>()
where ImplementationT : InterfaceT
{
_builder.iface(typeof (InterfaceT), typeof (ImplementationT));
}
}

public void iface(Type interfaceType, Type implementationType)
{
#if DEBUG
Debug.Assert(!_frozen);
#endif
if (!interfaceType.IsInterface)
throw new ArgumentException("must be an interface", "interfaceType");

if (implementationType.IsInterface)
throw new ArgumentException("must be an implementation type", "implementationType");

if (interfaceType.IsGenericTypeDefinition != implementationType.IsGenericTypeDefinition)
throw new ArgumentException("none or both must be open generic types", "interfaceType, implementationType");

if (interfaceType.IsGenericTypeDefinition && interfaceType.GetGenericArguments().Length != implementationType.GetGenericArguments().Length)
throw new ArgumentException("number of generic arguments do not match", "interfaceType, implementationType");

if (!interfaceType.IsGenericTypeDefinition && !interfaceType.IsAssignableFrom(implementationType))
throw new ArgumentException("interface is not implemented by the implementation", "interfaceType, implementationType");

if (_interfaceToImplementation.ContainsKey(interfaceType))
this.Debug("Reregistering implementation type {0} for interface {1}".fmt(implementationType.Name, interfaceType.Name));

_interfaceToImplementation[interfaceType] = implementationType;
}

public void generators<FactoryT>()
{
generators(typeof(FactoryT));
}

public void generators(Type type)
{
#if DEBUG
Debug.Assert(!_frozen);
#endif
var methods = type.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
var factoryMethods = from m in methods where m.hasAttribute<GeneratorMethodAttribute>() select m;

foreach (var fm in factoryMethods)
{
var returnType = fm.ReturnType;
var typeDefinition = returnType.IsGenericType ? returnType.GetGenericTypeDefinition() : returnType;
_generatorMethods.Add(typeDefinition, fm);
}
}

public void constructorArgument<TypeT, ArgT>(int index, Func<IScope, ArgT> argument)
{
#if DEBUG
Debug.Assert(!_frozen);
#endif
Debug.Assert(index >= 0);

var type = typeof(TypeT);
List<Func<IScope, object>> arguments;
if (!_constructorArguments.TryGetValue(type, out arguments))
_constructorArguments.Add(type, arguments = new List<Func<IScope, object>>());

while (arguments.Count <= index)
arguments.Add(null);

arguments[index] = (IScope scope) => argument(scope);
}


public void preferConstructor<TypeT>(params Type[] constructorTypes)
{
_preferredConstructor[typeof (TypeT)] = constructorTypes;
}

public void scanAssembly(Assembly assembly)
{
var defaultAttr = typeof (DefaultImplementationAttribute);

foreach (var implementationType in assembly.GetTypes())
{
var attrs = (DefaultImplementationAttribute[]) implementationType.GetCustomAttributes(defaultAttr, false);
foreach (var attr in attrs)
{
var interfaceTypes = attr.InterfaceTypes;
if (interfaceTypes.Length == 0)
{
registerImplementation(implementationType);
continue;
}

foreach (var interfaceType in interfaceTypes)
{
Debug.Assert(interfaceType.IsAssignableFrom(implementationType));
iface(interfaceType, implementationType);
}
}
}
}

void registerImplementation(Type implementationType)
{
foreach (var interfaceType in getImmediateInterfaces(implementationType))
{
iface(interfaceType, implementationType);
}
}

// http://stackoverflow.com/questions/5318685/get-only-direct-interface-instead-of-all

static IEnumerable<Type> getImmediateInterfaces(Type implementationType)
{
var allInterfaces = implementationType.GetInterfaces();
return
allInterfaces.Except
(allInterfaces.SelectMany(t => t.GetInterfaces()));
}

public IScope beginScope()
{
#if DEBUG
_frozen = true;
#endif
return new LifetimeScope(this);
}

}
}

Change log

r44 by paramatrix on Jun 12, 2011   Diff
avoid name clashes with other libraries
that are being used
Go to: 
Project members, sign in to write a code review

Older revisions

r43 by paramatrix on Mar 31, 2011   Diff
DefaultImplementation attribute now
supports multiple parameters
r39 by paramatrix on Mar 31, 2011   Diff
removed redundant locking of
dictionaries, concrete =>
implementation, new feature:
DefaultImplementation and
Builder.scanAssembly()
r38 by paramatrix on Mar 30, 2011   Diff
renamed concrete => implementation,
added fluent interface to specify
interface => implementation mapping.
All revisions of this file

File info

Size: 5789 bytes, 186 lines
Powered by Google Project Hosting