-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add startupable #16
base: main
Are you sure you want to change the base?
Add startupable #16
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace Agoda.IoC.Core | ||
{ | ||
public interface IStartupable | ||
{ | ||
void Start(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using System; | ||
using Agoda.IoC.Core; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Agoda.IoC.NetCore | ||
{ | ||
public class NetCoreComponentResolver : IComponentResolver | ||
{ | ||
private readonly IServiceProvider _serviceProvider; | ||
|
||
public NetCoreComponentResolver(IServiceProvider serviceProvider) | ||
{ | ||
_serviceProvider = serviceProvider; | ||
} | ||
|
||
public T Resolve<T>() | ||
{ | ||
return _serviceProvider.GetService<T>(); | ||
|
||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
using System.Diagnostics; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace Agoda.IoC.NetCore | ||
{ | ||
|
@@ -25,6 +26,18 @@ public static IServiceCollection AutoWireAssembly( | |
return rtn; | ||
} | ||
|
||
public static IServiceProvider UseAgodaIoCStartupable(this IServiceProvider app) | ||
{ | ||
foreach (var startupable in app.GetServices<IStartupable>()) | ||
{ | ||
if (startupable == null) | ||
throw new Exception( | ||
"No service found that inherit from IStartupable but UseAgodaIoCStartupable was called"); | ||
startupable.Start(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe for a future iteration however it would be nice if you could specify the degree of parallelism. |
||
} | ||
return app; | ||
} | ||
|
||
public static IServiceCollection AutoWireAssembly<T>( | ||
this IServiceCollection services, Assembly[] assemblies, | ||
ServiceLifetime serviceLifetime, | ||
|
@@ -77,6 +90,13 @@ public static IServiceCollection AutoWireAssembly<T>( | |
{ | ||
services.Add(serviceDescriptor); | ||
} | ||
|
||
if (reg.IsStartupable) | ||
{ | ||
services.Add(new ServiceDescriptor(typeof(IStartupable), | ||
x => x.GetService(reg.FromType), | ||
ServiceLifetime.Singleton)); | ||
} | ||
} | ||
return services; | ||
} | ||
|
@@ -167,20 +187,4 @@ private static bool Validate(List<RegistrationContext> registrations, ContainerR | |
return isValid; | ||
} | ||
} | ||
|
||
public class NetCoreComponentResolver : IComponentResolver | ||
{ | ||
private readonly IServiceProvider _serviceProvider; | ||
|
||
public NetCoreComponentResolver(IServiceProvider serviceProvider) | ||
{ | ||
_serviceProvider = serviceProvider; | ||
} | ||
|
||
public T Resolve<T>() | ||
{ | ||
return _serviceProvider.GetService<T>(); | ||
|
||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,4 +10,23 @@ public class ReplaceServiceTwoWork : IReplaceService | |
|
||
public string DoWork { get; set; } = nameof(ReplaceServiceTwoWork); | ||
} | ||
|
||
public interface IServiceThatStartsUp | ||
{ | ||
int Somedata { get; set; } | ||
} | ||
|
||
[RegisterSingleton(For = typeof(IServiceThatStartsUp))] | ||
public class ServiceThatStartsUp : IServiceThatStartsUp, IStartupable | ||
{ | ||
public ServiceThatStartsUp() | ||
{ | ||
Somedata = 0; | ||
} | ||
public int Somedata { get; set; } | ||
public void Start() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ideally, it would be nice if the startup process could run asynchronously. I would expect that most of the implementation will communicate with other services etc. therefore it might be better to start returning a Task from the start. |
||
{ | ||
Somedata++; | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: Not sure if GetServices returns null or an empty list.