Skip to content

Commit 9368a99

Browse files
author
John Simons
committed
Added lambdas support for Unity container, closes #572
Conflicts: src/impl/ObjectBuilder/ObjectBuilder.Tests/When_building_components.cs src/impl/ObjectBuilder/ObjectBuilder.Tests/When_registering_components.cs src/impl/ObjectBuilder/ObjectBuilder.Unity/UnityObjectBuilder.cs
1 parent 273853b commit 9368a99

File tree

3 files changed

+15
-12
lines changed

3 files changed

+15
-12
lines changed

src/impl/ObjectBuilder/ObjectBuilder.Tests/When_building_components.cs

+4-6
Original file line numberDiff line numberDiff line change
@@ -38,23 +38,21 @@ public void Lambda_uow_components_should_resolve_from_main_container()
3838
{
3939
ForAllBuilders((builder) =>
4040
Assert.NotNull(builder.Build(typeof(LambdaComponentUoW))),
41-
typeof(WindsorObjectBuilder), typeof(UnityObjectBuilder));
41+
typeof(WindsorObjectBuilder));
4242
}
4343

4444
[Test]
4545
public void Lambda_singlecall_components_should_yield_unique_instances()
4646
{
4747
ForAllBuilders((builder) =>
48-
Assert.AreNotEqual(builder.Build(typeof(SingleCallLambdaComponent)), builder.Build(typeof(SingleCallLambdaComponent))),
49-
typeof(UnityObjectBuilder));
48+
Assert.AreNotEqual(builder.Build(typeof(SingleCallLambdaComponent)), builder.Build(typeof(SingleCallLambdaComponent))));
5049
}
5150

5251
[Test]
5352
public void Lambda_singleton_components_should_yield_the_same_instance()
5453
{
5554
ForAllBuilders((builder) =>
56-
Assert.AreEqual(builder.Build(typeof(SingletonLambdaComponent)), builder.Build(typeof(SingletonLambdaComponent))),
57-
typeof(UnityObjectBuilder));
55+
Assert.AreEqual(builder.Build(typeof(SingletonLambdaComponent)), builder.Build(typeof(SingletonLambdaComponent))));
5856
}
5957

6058
[Test]
@@ -121,4 +119,4 @@ public ComponentCreatedByFactory Create()
121119
public class ComponentCreatedByFactory
122120
{
123121
}
124-
}
122+
}

src/impl/ObjectBuilder/ObjectBuilder.Tests/When_registering_components.cs

+2-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ namespace ObjectBuilder.Tests
99
using System.Collections.Generic;
1010
using NServiceBus.ObjectBuilder.CastleWindsor;
1111
using NServiceBus.ObjectBuilder.Spring;
12-
using NServiceBus.ObjectBuilder.Unity;
1312

1413
[TestFixture]
1514
public class When_registering_components : BuilderFixture
@@ -36,7 +35,7 @@ public void Should_support_lambdas_that_uses_other_components_registered_later()
3635
builder.Configure(() => new StaticFactory(), DependencyLifecycle.SingleInstance);
3736

3837
Assert.NotNull(builder.Build(typeof(ComponentCreatedByFactory)));
39-
},typeof(UnityObjectBuilder));
38+
});
4039
}
4140

4241
[Test]
@@ -304,4 +303,4 @@ public enum SomeEnum
304303
{
305304
X
306305
}
307-
}
306+
}

src/impl/ObjectBuilder/ObjectBuilder.Unity/UnityObjectBuilder.cs

+9-3
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public void Configure(Type concreteComponent, DependencyLifecycle dependencyLife
9393
if (HasComponent(concreteComponent))
9494
return;
9595

96-
IEnumerable<Type> interfaces = GetAllServiceTypesFor(concreteComponent);
96+
var interfaces = GetAllServiceTypesFor(concreteComponent);
9797

9898
foreach (Type t in interfaces)
9999
{
@@ -107,12 +107,18 @@ public void Configure(Type concreteComponent, DependencyLifecycle dependencyLife
107107
DefaultInstances.Add(t);
108108
}
109109
}
110-
111110
}
112111

113112
public void Configure<T>(Func<T> componentFactory, DependencyLifecycle dependencyLifecycle)
114113
{
115-
throw new NotSupportedException("UnityObjectBuilder does not support lambda expressions.");
114+
var componentType = typeof (T);
115+
116+
if (HasComponent(componentType))
117+
return;
118+
119+
container.RegisterType<T>(GetLifetimeManager(dependencyLifecycle),
120+
new InjectionFactory(unityContainer => componentFactory()));
121+
DefaultInstances.Add(componentType);
116122
}
117123

118124
public void ConfigureProperty(Type concreteComponent, string property, object value)

0 commit comments

Comments
 (0)