Skip to content

Commit 623d33e

Browse files
committed
Add TryResolve method that returns null on missing service.
1 parent c939a74 commit 623d33e

18 files changed

+994
-887
lines changed

src/ZeroIoC.Benchmarks/Program.cs

Lines changed: 122 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -6,166 +6,165 @@
66
using Grace.DependencyInjection.Lifestyle;
77
using Microsoft.Extensions.DependencyInjection;
88

9-
namespace ZeroIoC.Benchmarks
9+
namespace ZeroIoC.Benchmarks;
10+
11+
public interface IUserService
12+
{
13+
}
14+
15+
public class UserService : IUserService
1016
{
11-
public interface IUserService
17+
public UserService(Helper helper)
1218
{
1319
}
1420

15-
public class UserService : IUserService
16-
{
17-
public UserService(Helper helper)
18-
{
19-
}
21+
public Guid Id { get; } = Guid.NewGuid();
22+
}
2023

21-
public Guid Id { get; } = Guid.NewGuid();
22-
}
24+
public class Helper
25+
{
26+
}
2327

24-
public class Helper
25-
{
26-
}
28+
public class SingleHelper
29+
{
30+
}
2731

28-
public class SingleHelper
32+
public class SingleService(SingleHelper helper)
33+
{
34+
private readonly SingleHelper _helper = helper;
35+
}
36+
37+
public partial class Container : ZeroIoCContainer
38+
{
39+
protected override void Bootstrap(IZeroIoCContainerBootstrapper bootstrapper)
2940
{
41+
bootstrapper.AddTransient<Helper>();
42+
bootstrapper.AddTransient<IUserService, UserService>();
43+
bootstrapper.AddSingleton<SingleHelper>();
44+
bootstrapper.AddSingleton<SingleService>();
3045
}
46+
}
3147

32-
public class SingleService(SingleHelper helper)
48+
internal class Program
49+
{
50+
private static void Main(string[] args)
3351
{
34-
private readonly SingleHelper _helper = helper;
52+
BenchmarkRunner.Run<IoCStartupBenchmark>();
53+
// BenchmarkRunner.Run<IoCRuntimeBenchmark>();
3554
}
55+
}
3656

37-
public partial class Container : ZeroIoCContainer
57+
public class Creators
58+
{
59+
public static Container CreateZeroIoC()
3860
{
39-
protected override void Bootstrap(IZeroIoCContainerBootstrapper bootstrapper)
40-
{
41-
bootstrapper.AddTransient<Helper>();
42-
bootstrapper.AddTransient<IUserService, UserService>();
43-
bootstrapper.AddSingleton<SingleHelper>();
44-
bootstrapper.AddSingleton<SingleService>();
45-
}
61+
return new Container();
4662
}
4763

48-
internal class Program
64+
public static ServiceProvider CreateMicrosoft()
4965
{
50-
private static void Main(string[] args)
51-
{
52-
BenchmarkRunner.Run<IoCStartupBenchmark>();
53-
// BenchmarkRunner.Run<IoCRuntimeBenchmark>();
54-
}
66+
var services = new ServiceCollection();
67+
services.AddSingleton<SingleHelper>();
68+
services.AddSingleton<SingleService>();
69+
services.AddTransient<Helper>();
70+
services.AddTransient<IUserService, UserService>();
71+
72+
return services.BuildServiceProvider();
5573
}
5674

57-
public class Creators
75+
public static DependencyInjectionContainer CreateGrace()
5876
{
59-
public static Container CreateZeroIoC()
60-
{
61-
return new Container();
62-
}
63-
64-
public static ServiceProvider CreateMicrosoft()
77+
var grace = new DependencyInjectionContainer();
78+
grace.Configure(o =>
6579
{
66-
var services = new ServiceCollection();
67-
services.AddSingleton<SingleHelper>();
68-
services.AddSingleton<SingleService>();
69-
services.AddTransient<Helper>();
70-
services.AddTransient<IUserService, UserService>();
80+
o.Export<SingleHelper>().As<SingleHelper>().UsingLifestyle(new SingletonLifestyle());
81+
o.Export<SingleService>().As<SingleService>().UsingLifestyle(new SingletonLifestyle());
7182

72-
return services.BuildServiceProvider();
73-
}
83+
o.Export<Helper>().As<Helper>();
84+
o.Export<UserService>().As<IUserService>();
85+
});
7486

75-
public static DependencyInjectionContainer CreateGrace()
76-
{
77-
var grace = new DependencyInjectionContainer();
78-
grace.Configure(o =>
79-
{
80-
o.Export<SingleHelper>().As<SingleHelper>().UsingLifestyle(new SingletonLifestyle());
81-
o.Export<SingleService>().As<SingleService>().UsingLifestyle(new SingletonLifestyle());
87+
return grace;
88+
}
89+
}
8290

83-
o.Export<Helper>().As<Helper>();
84-
o.Export<UserService>().As<IUserService>();
85-
});
91+
[MemoryDiagnoser]
92+
[Orderer(SummaryOrderPolicy.FastestToSlowest)]
93+
public class IoCStartupBenchmark
94+
{
95+
[Benchmark]
96+
public void MicrosoftStartup()
97+
{
98+
var resolver = Creators.CreateMicrosoft();
99+
var userService = (IUserService)resolver.GetService(typeof(IUserService));
100+
var singleService = (SingleService)resolver.GetService(typeof(SingleService));
101+
}
86102

87-
return grace;
88-
}
103+
[Benchmark]
104+
public void ZeroStartup()
105+
{
106+
var resolver = Creators.CreateZeroIoC();
107+
var userService = (IUserService)resolver.Resolve(typeof(IUserService));
108+
var singleService = (SingleService)resolver.Resolve(typeof(SingleService));
89109
}
90110

91-
[MemoryDiagnoser]
92-
[Orderer(SummaryOrderPolicy.FastestToSlowest)]
93-
public class IoCStartupBenchmark
111+
[Benchmark]
112+
public void GraceStartup()
94113
{
95-
[Benchmark]
96-
public void MicrosoftStartup()
97-
{
98-
var resolver = Creators.CreateMicrosoft();
99-
var userService = (IUserService)resolver.GetService(typeof(IUserService));
100-
var singleService = (SingleService)resolver.GetService(typeof(SingleService));
101-
}
114+
var resolver = Creators.CreateGrace();
115+
var userService = (IUserService)resolver.Locate(typeof(IUserService));
116+
var singleService = (SingleService)resolver.Locate(typeof(SingleService));
117+
}
118+
}
102119

103-
[Benchmark]
104-
public void ZeroStartup()
105-
{
106-
var resolver = Creators.CreateZeroIoC();
107-
var userService = (IUserService)resolver.Resolve(typeof(IUserService));
108-
var singleService = (SingleService)resolver.Resolve(typeof(SingleService));
109-
}
120+
[MemoryDiagnoser]
121+
[Orderer(SummaryOrderPolicy.FastestToSlowest)]
122+
public class IoCRuntimeBenchmark
123+
{
124+
private readonly DependencyInjectionContainer _grace;
125+
private readonly Container _zeroioc;
126+
private readonly ServiceProvider _serviceProvider;
110127

111-
[Benchmark]
112-
public void GraceStartup()
113-
{
114-
var resolver = Creators.CreateGrace();
115-
var userService = (IUserService)resolver.Locate(typeof(IUserService));
116-
var singleService = (SingleService)resolver.Locate(typeof(SingleService));
117-
}
128+
public IoCRuntimeBenchmark()
129+
{
130+
_grace = Creators.CreateGrace();
131+
_serviceProvider = Creators.CreateMicrosoft();
132+
_zeroioc = Creators.CreateZeroIoC();
118133
}
119134

120-
[MemoryDiagnoser]
121-
[Orderer(SummaryOrderPolicy.FastestToSlowest)]
122-
public class IoCRuntimeBenchmark
135+
[Benchmark]
136+
public IUserService MicrosoftTransient()
123137
{
124-
private readonly DependencyInjectionContainer _grace;
125-
private readonly Container _zeroioc;
126-
private readonly ServiceProvider _serviceProvider;
127-
128-
public IoCRuntimeBenchmark()
129-
{
130-
_grace = Creators.CreateGrace();
131-
_serviceProvider = Creators.CreateMicrosoft();
132-
_zeroioc = Creators.CreateZeroIoC();
133-
}
134-
135-
[Benchmark]
136-
public IUserService MicrosoftTransient()
137-
{
138-
return (IUserService)_serviceProvider.GetService(typeof(IUserService));
139-
}
138+
return (IUserService)_serviceProvider.GetService(typeof(IUserService));
139+
}
140140

141-
[Benchmark]
142-
public IUserService ZeroTransient()
143-
{
144-
return (IUserService)_zeroioc.Resolve(typeof(IUserService));
145-
}
141+
[Benchmark]
142+
public IUserService ZeroTransient()
143+
{
144+
return (IUserService)_zeroioc.Resolve(typeof(IUserService));
145+
}
146146

147-
[Benchmark]
148-
public IUserService GraceTransient()
149-
{
150-
return (IUserService)_grace.Locate(typeof(IUserService));
151-
}
147+
[Benchmark]
148+
public IUserService GraceTransient()
149+
{
150+
return (IUserService)_grace.Locate(typeof(IUserService));
151+
}
152152

153-
[Benchmark]
154-
public SingleService MicrosoftSingleton()
155-
{
156-
return (SingleService)_serviceProvider.GetService(typeof(SingleService));
157-
}
153+
[Benchmark]
154+
public SingleService MicrosoftSingleton()
155+
{
156+
return (SingleService)_serviceProvider.GetService(typeof(SingleService));
157+
}
158158

159-
[Benchmark]
160-
public SingleService ZeroSingleton()
161-
{
162-
return (SingleService)_zeroioc.Resolve(typeof(SingleService));
163-
}
159+
[Benchmark]
160+
public SingleService ZeroSingleton()
161+
{
162+
return (SingleService)_zeroioc.Resolve(typeof(SingleService));
163+
}
164164

165-
[Benchmark]
166-
public SingleService GraceSingleton()
167-
{
168-
return (SingleService)_grace.Locate(typeof(SingleService));
169-
}
165+
[Benchmark]
166+
public SingleService GraceSingleton()
167+
{
168+
return (SingleService)_grace.Locate(typeof(SingleService));
170169
}
171170
}

src/ZeroIoC.Core/ExceptionHelper.cs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
namespace ZeroIoC.Core
1+
namespace ZeroIoC;
2+
3+
internal static class ExceptionHelper
24
{
3-
internal static class ExceptionHelper
5+
public static void ScopedWithoutScopeException(string fullName)
46
{
5-
public static void ScopedWithoutScopeException(string fullName)
6-
{
7-
throw new ScopedWithoutScopeException($"Type {fullName} is registred as scoped, but you are trying to create it without scope.");
8-
}
7+
throw new ScopedWithoutScopeException($"Type {fullName} is registred as scoped, but you are trying to create it without scope.");
8+
}
99

10-
public static void ServiceIsNotRegistered(string fullName)
11-
{
12-
throw new ServiceIsNotRegistered($"Type {fullName} is missing in resolver.");
13-
}
10+
public static void ServiceIsNotRegistered(string fullName)
11+
{
12+
throw new ServiceIsNotRegistered($"Type {fullName} is missing in resolver.");
1413
}
1514
}
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
using System;
22

3-
namespace ZeroIoC
3+
namespace ZeroIoC;
4+
5+
public class ScopedWithoutScopeException : Exception
46
{
5-
public class ScopedWithoutScopeException : Exception
7+
public ScopedWithoutScopeException(string message)
8+
: base(message)
69
{
7-
public ScopedWithoutScopeException(string message)
8-
: base(message)
9-
{
1010

11-
}
1211
}
1312
}
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
using System;
22

3-
namespace ZeroIoC
3+
namespace ZeroIoC;
4+
5+
public class ServiceIsNotRegistered : Exception
46
{
5-
public class ServiceIsNotRegistered : Exception
7+
public ServiceIsNotRegistered(string message)
8+
: base(message)
69
{
7-
public ServiceIsNotRegistered(string message)
8-
: base(message)
9-
{
1010

11-
}
1211
}
1312
}

0 commit comments

Comments
 (0)