Skip to content
This repository was archived by the owner on Apr 18, 2020. It is now read-only.

Commit e2a01cc

Browse files
authored
Merge pull request #14 from osoykan/dev
dev to master
2 parents da8dc0e + 99353f1 commit e2a01cc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+14122
-17
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Autofac.Extras.IocManager [![Build status](https://ci.appveyor.com/api/projects/status/udvakwrxb3nhb25d?svg=true)](https://ci.appveyor.com/project/osoykan/autofac-extras-iocmanager)
1+
Autofac.Extras.IocManager [![Build status](https://ci.appveyor.com/api/projects/status/udvakwrxb3nhb25d?svg=true)](https://ci.appveyor.com/project/osoykan/autofac-extras-iocmanager) [![Coverage Status](https://coveralls.io/repos/github/osoykan/Autofac.Extras.IocManager/badge.svg?branch=dev)](https://coveralls.io/github/osoykan/Autofac.Extras.IocManager?branch=dev)
22
=====================
33

44

appveyor.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@ test:
2626
- test\Autofac.Extras.IocManager.Tests\bin\Debug\net452\win7-x64\Autofac.Extras.IocManager.Tests.dll
2727
- test\Autofac.Extras.IocManager.Tests\bin\Debug\net452\win7-x64\Autofac.Extras.IocManager.DynamicProxy.Tests.dll
2828
after_test:
29-
- tools\coverage\opencover\OpenCover.Console.exe -register:user -target:"tools\xunitrunner\xunit.console.x86.exe" -targetdir:"test\Autofac.Extras.IocManager.Tests\bin\Debug\net461\win7-x64" -targetargs:"Autofac.Extras.IocManager.Tests.dll Autofac.Extras.IocManager.DynamicProxy.Tests.dll" -filter:"+[Autofac.Extras.IocManager*]* -[Autofac.Extras.IocManager.T*]*" -excludebyattribute:*.ExcludeFromCodeCoverage* -output:xunit_opencovertests.xml -coverbytest:*.Tests.dll
29+
- tools\coverage\opencover\OpenCover.Console.exe -register:user -target:"tools\xunitrunner\xunit.console.x86.exe" -targetargs:"test\Autofac.Extras.IocManager.Tests\bin\Debug\net452\win7-x64\Autofac.Extras.IocManager.Tests.dll test\Autofac.Extras.IocManager.DynamicProxy.Tests\bin\Debug\net452\win7-x64\Autofac.Extras.IocManager.DynamicProxy.Tests.dll -noshadow" -filter:"+[Autofac.Extras.IocManager*]* -[Autofac.Extras.IocManager.T*]* -[Autofac.Extras.IocManager.DynamicProxy.T*]*" -output:xunit_opencovertests.xml -coverbytest:*.Tests.dll
3030
- tools\coverage\coveralls.net\csmacnz.Coveralls.exe --opencover -i xunit_opencovertests.xml --repoToken %COVERALLS_REPO_TOKEN% --commitBranch %APPVEYOR_REPO_BRANCH%
3131
deploy:
3232
- provider: NuGet
33+
on:
34+
branch: master
3335
api_key:
3436
secure: liSTqN52OJQy9fZJk6gubSgYBAy4EXcEPCDSCLJ/3T9cZufzpTwvBlGPLX+iP2n9
3537
artifacts:

src/Autofac.Extras.IocManager.DynamicProxy/project.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
2-
"version" : "2.0.7-*",
2+
"version" : "2.1.0-*",
33

44
"dependencies": {
5-
"Autofac": "4.3.0",
6-
"Autofac.Extras.DynamicProxy": "4.1.0",
7-
"Autofac.Extras.IocManager": "2.0.7",
5+
"Autofac": "4.4.0",
6+
"Autofac.Extras.DynamicProxy": "4.2.1",
7+
"Autofac.Extras.IocManager": "2.1.0",
88
"Castle.Core": "4.0.0"
99
},
1010

src/Autofac.Extras.IocManager/IServiceRegistration.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,39 @@ void Register<TService, TImplementation>(
123123
where TImplementation : class, TService
124124
where TService : class;
125125

126+
/// <summary>
127+
/// Registers if absent.
128+
/// </summary>
129+
/// <typeparam name="TService">The type of the service.</typeparam>
130+
/// <typeparam name="TImplementation">The type of the implementation.</typeparam>
131+
/// <param name="lifetime">The lifetime.</param>
132+
void RegisterIfAbsent<TService, TImplementation>(Lifetime lifetime = Lifetime.Transient)
133+
where TService : class
134+
where TImplementation : class, TService;
135+
136+
/// <summary>
137+
/// Registers if absent.
138+
/// </summary>
139+
/// <param name="serviceType">Type of the service.</param>
140+
/// <param name="implementationType">Type of the implementation.</param>
141+
/// <param name="lifetime">The lifetime.</param>
142+
void RegisterIfAbsent(Type serviceType, Type implementationType, Lifetime lifetime = Lifetime.Transient);
143+
144+
/// <summary>
145+
/// Registers if absent.
146+
/// </summary>
147+
/// <typeparam name="TService">The type of the service.</typeparam>
148+
/// <param name="lifetime">The lifetime.</param>
149+
void RegisterIfAbsent<TService>(Lifetime lifetime = Lifetime.Transient)
150+
where TService : class;
151+
152+
/// <summary>
153+
/// Registers if absent.
154+
/// </summary>
155+
/// <param name="type">The type.</param>
156+
/// <param name="lifetime">The lifetime.</param>
157+
void RegisterIfAbsent(Type type, Lifetime lifetime = Lifetime.Transient);
158+
126159
/// <summary>
127160
/// Registers the specified factory.
128161
/// </summary>

src/Autofac.Extras.IocManager/ServiceRegistration.cs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Reflection;
33

44
using Autofac.Builder;
5+
using Autofac.Features.ResolveAnything;
56
using Autofac.Features.Scanning;
67

78
namespace Autofac.Extras.IocManager
@@ -259,6 +260,68 @@ public void Register<TService, TImplementation>(
259260
}
260261
}
261262

263+
/// <summary>
264+
/// Registers if absent.
265+
/// </summary>
266+
/// <typeparam name="TService">The type of the service.</typeparam>
267+
/// <typeparam name="TImplementation">The type of the implementation.</typeparam>
268+
/// <param name="lifetime">The lifetime.</param>
269+
public void RegisterIfAbsent<TService, TImplementation>(
270+
Lifetime lifetime = Lifetime.Transient)
271+
where TService : class
272+
where TImplementation : class, TService
273+
{
274+
RegisterIfAbsent(typeof(TService), typeof(TImplementation), lifetime);
275+
}
276+
277+
/// <summary>
278+
/// Registers if absent.
279+
/// </summary>
280+
/// <param name="serviceType">Type of the service.</param>
281+
/// <param name="implementationType">Type of the implementation.</param>
282+
/// <param name="lifetime">The lifetime.</param>
283+
public void RegisterIfAbsent(Type serviceType, Type implementationType, Lifetime lifetime = Lifetime.Transient)
284+
{
285+
IRegistrationBuilder<object, ConcreteReflectionActivatorData, SingleRegistrationStyle> registration = _containerBuilder
286+
.RegisterType(implementationType)
287+
.InjectPropertiesAsAutowired()
288+
.AsSelf()
289+
.IfNotRegistered(serviceType);
290+
291+
_containerBuilder.Register(c => c.Resolve(implementationType))
292+
.As(serviceType)
293+
.InjectPropertiesAsAutowired()
294+
.OnActivating(args =>
295+
{
296+
object instance = _decoratorService.Decorate(args.Instance, new ResolverContext(new Resolver(args.Context)));
297+
args.ReplaceInstance(instance);
298+
})
299+
.IfNotRegistered(serviceType);
300+
301+
registration.ApplyLifeStyle(lifetime);
302+
}
303+
304+
305+
public void RegisterIfAbsent<TService>(Lifetime lifetime = Lifetime.Transient) where TService : class
306+
{
307+
RegisterIfAbsent(typeof(TService), lifetime);
308+
}
309+
310+
public void RegisterIfAbsent(Type type, Lifetime lifetime = Lifetime.Transient)
311+
{
312+
IRegistrationBuilder<object, ConcreteReflectionActivatorData, SingleRegistrationStyle> registration = _containerBuilder
313+
.RegisterType(type)
314+
.InjectPropertiesAsAutowired()
315+
.AsSelf()
316+
.OnActivating(args =>
317+
{
318+
object instance = _decoratorService.Decorate(type, args.Instance, new ResolverContext(new Resolver(args.Context)));
319+
args.ReplaceInstance(instance);
320+
}).IfNotRegistered(type);
321+
322+
registration.ApplyLifeStyle(lifetime);
323+
}
324+
262325
/// <summary>
263326
/// Registers the specified factory.
264327
/// </summary>
@@ -339,6 +402,7 @@ public void RegisterType(
339402
object instance = _decoratorService.Decorate(args.Instance, new ResolverContext(new Resolver(args.Context)));
340403
args.ReplaceInstance(instance);
341404
});
405+
342406
registration.ApplyLifeStyle(lifetime);
343407

344408
if (keepDefault)

src/Autofac.Extras.IocManager/project.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
2-
"version": "2.0.7-*",
2+
"version": "2.1.0-*",
33

44
"dependencies": {
5-
"Autofac": "4.3.0",
6-
"FluentAssemblyScanner": "1.0.5"
5+
"Autofac": "4.4.0",
6+
"FluentAssemblyScanner": "1.0.7"
77
},
88

99
"frameworks": {

test/Autofac.Extras.IocManager.DynamicProxy.Tests/project.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
"dependencies": {
77
"Autofac.Extras.IocManager.TestBase": "1.0.0",
8-
"Autofac.Extras.IocManager.DynamicProxy": "2.0.7"
8+
"Autofac.Extras.IocManager.DynamicProxy": "2.1.0"
99
},
1010

1111
"frameworks": {

test/Autofac.Extras.IocManager.TestBase/project.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@
44
"testRunner": "xunit",
55

66
"dependencies": {
7-
"Autofac": "4.3.0",
8-
"Autofac.Extras.DynamicProxy": "4.1.0",
9-
"Autofac.Extras.IocManager": "2.0.7-*",
7+
"Autofac": "4.4.0",
8+
"Autofac.Extras.DynamicProxy": "4.2.1",
9+
"Autofac.Extras.IocManager": "2.1.0-*",
1010
"AutoFixture.AutoMoq": "3.50.2",
1111
"AutoFixture.AutoNSubstitute": "3.50.2",
1212
"dotnet-test-xunit": "2.2.0-preview2-build1029",
13-
"NSubstitute": "1.10.0",
13+
"NSubstitute": "2.0.2",
1414
"Shouldly": "2.8.2",
15-
"xunit": "2.2.0-beta4-build3444",
16-
"xunit.extensibility.execution": "2.2.0-beta4-build3444",
17-
"xunit.runner.visualstudio": "2.2.0-beta4-build1194"
15+
"xunit": "2.2.0",
16+
"xunit.extensibility.execution": "2.2.0",
17+
"xunit.runner.visualstudio": "2.2.0"
1818
},
1919

2020
"frameworks" : {
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
using Autofac.Extras.IocManager.TestBase;
2+
using Autofac.Features.ResolveAnything;
3+
4+
using Shouldly;
5+
6+
using Xunit;
7+
8+
namespace Autofac.Extras.IocManager.Tests
9+
{
10+
public class IfNotRegistered_Tests : TestBaseWithIocBuilder
11+
{
12+
[Fact]
13+
public void if_not_registered_should_work()
14+
{
15+
Building(builder =>
16+
{
17+
builder.RegisterServices(r => r.UseBuilder(cb =>
18+
{
19+
cb.RegisterType<ServiceA>().As<IService>();
20+
cb.RegisterType<ServiceB>().As<IService>().IfNotRegistered(typeof(ServiceA));
21+
}));
22+
});
23+
24+
LocalIocManager.IsRegistered<ServiceB>().ShouldBe(false);
25+
}
26+
27+
[Fact]
28+
public void RegisterIfAbsent_should_work_with_generic_T()
29+
{
30+
Building(builder =>
31+
{
32+
builder.RegisterServices(r =>
33+
{
34+
r.Register<IService, ServiceA>();
35+
r.RegisterIfAbsent<IService, ServiceB>();
36+
});
37+
});
38+
39+
LocalIocManager.IsRegistered(typeof(ServiceB)).ShouldBe(false);
40+
LocalIocManager.Resolve<IService>().ShouldBeAssignableTo<ServiceA>();
41+
}
42+
43+
[Fact]
44+
public void RegisterIfAbsent_should_work_with_type_parameters_()
45+
{
46+
Building(builder =>
47+
{
48+
builder.RegisterServices(r =>
49+
{
50+
r.Register<IService, ServiceA>();
51+
r.RegisterIfAbsent(typeof(IService), typeof(ServiceB));
52+
});
53+
});
54+
55+
LocalIocManager.IsRegistered(typeof(ServiceB)).ShouldBe(false);
56+
LocalIocManager.Resolve<IService>().ShouldBeAssignableTo<ServiceA>();
57+
}
58+
59+
[Fact]
60+
public void RegisterIfAbsent_should_work_with_type_parameters_propertyinjection_should_not_be_null()
61+
{
62+
Building(builder =>
63+
{
64+
builder.RegisterServices(r =>
65+
{
66+
r.RegisterIfAbsent(typeof(MyClass));
67+
r.RegisterIfAbsent(typeof(IService), typeof(ServiceB));
68+
r.RegisterIfAbsent<MyClass>();
69+
});
70+
});
71+
72+
LocalIocManager.IsRegistered(typeof(ServiceB)).ShouldBe(true);
73+
LocalIocManager.Resolve<IService>().ShouldBeAssignableTo<ServiceB>();
74+
LocalIocManager.Resolve<IService>().MyProperty.ShouldNotBeNull();
75+
}
76+
77+
[Fact]
78+
public void RegisterIfAbsent_should_work_with_TGeneric_propertyinjection_should_not_be_null()
79+
{
80+
Building(builder =>
81+
{
82+
builder.RegisterServices(r =>
83+
{
84+
r.RegisterType(typeof(MyClass));
85+
r.RegisterIfAbsent<IService, ServiceB>();
86+
r.RegisterIfAbsent<MyClass>();
87+
});
88+
});
89+
90+
LocalIocManager.IsRegistered(typeof(ServiceB)).ShouldBe(true);
91+
LocalIocManager.Resolve<IService>().ShouldBeAssignableTo<ServiceB>();
92+
LocalIocManager.Resolve<IService>().MyProperty.ShouldNotBeNull();
93+
}
94+
95+
[Fact]
96+
public void RegisterIfAbsent_should_work_on_propertyInjection()
97+
{
98+
Building(builder =>
99+
{
100+
builder.RegisterServices(r =>
101+
{
102+
r.RegisterIfAbsent<IService, ServiceA>();
103+
r.RegisterIfAbsent(typeof(IService), typeof(ServiceB));
104+
r.RegisterType(typeof(MyClass));
105+
});
106+
});
107+
108+
LocalIocManager.IsRegistered(typeof(ServiceB)).ShouldBe(false);
109+
LocalIocManager.Resolve<IService>().ShouldBeAssignableTo<ServiceA>();
110+
111+
var instanceA = LocalIocManager.Resolve<IService>();
112+
instanceA.MyProperty.ShouldNotBeNull();
113+
}
114+
115+
[Fact]
116+
public void IfNotRegistered_PropertyInjection_should_work()
117+
{
118+
var builder = new ContainerBuilder();
119+
builder.RegisterType<MyClass>().AsSelf().IfNotRegistered(typeof(MyClass));
120+
builder.RegisterType<ServiceA>().As<IService>().IfNotRegistered(typeof(ServiceA)).AsSelf().PropertiesAutowired();
121+
IContainer resolver = builder.Build();
122+
123+
var serviceAInstance = resolver.Resolve<ServiceA>();
124+
serviceAInstance.MyProperty.ShouldNotBeNull();
125+
}
126+
127+
[Fact]
128+
public void IfNotRegistered_PropertyInjection_should_work_single()
129+
{
130+
var builder = new ContainerBuilder();
131+
builder.RegisterType<MyClass>().AsSelf().IfNotRegistered(typeof(MyClass));
132+
builder.RegisterType<ServiceA>().AsSelf().IfNotRegistered(typeof(ServiceA)).PropertiesAutowired();
133+
IContainer resolver = builder.Build();
134+
135+
var serviceAInstance = resolver.Resolve<ServiceA>();
136+
serviceAInstance.MyProperty.ShouldNotBeNull();
137+
}
138+
139+
internal class ServiceA : IService
140+
{
141+
public MyClass MyProperty { get; set; }
142+
}
143+
144+
internal interface IService
145+
{
146+
MyClass MyProperty { get; set; }
147+
}
148+
149+
internal class ServiceB : IService
150+
{
151+
public MyClass MyProperty { get; set; }
152+
}
153+
154+
internal class MyClass
155+
{
156+
}
157+
}
158+
}

test/Autofac.Extras.IocManager.Tests/IocBuilder_Tests.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ public void IocBuilder_ShouldWork()
2121
myDependencyFromLocalIocManager.ShouldBeSameAs(myDependencyFromResolver);
2222
}
2323

24+
[Fact]
25+
public void ResolverContext_should_not_be_null()
26+
{
27+
IResolver resolver = Building(builder => { });
28+
29+
var resolverContext = new ResolverContext(resolver);
30+
resolverContext.Resolver.ShouldNotBeNull();
31+
}
32+
2433
internal interface IMyDependency
2534
{
2635
}

0 commit comments

Comments
 (0)