-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added dotnet core support and refactor code
- Loading branch information
1 parent
480478d
commit d4fae7a
Showing
11 changed files
with
324 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using ServiceAnt.Infrastructure.Dependency; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace ServiceAnt.IocInstaller.DotNetCore | ||
{ | ||
/// <summary> | ||
/// IocResolver | ||
/// </summary> | ||
public class IocResolver : IIocResolver | ||
{ | ||
private readonly IServiceProvider _container; | ||
|
||
/// <summary> | ||
/// Constructor | ||
/// </summary> | ||
/// <param name="container">the container of castle</param> | ||
public IocResolver(IServiceProvider container) | ||
{ | ||
_container = container; | ||
} | ||
|
||
/// <summary> | ||
/// Releases a pre-resolved object. See Resolve methods. | ||
/// </summary> | ||
/// <param name="obj">Object to be released</param> | ||
public void Release(object obj) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Gets an object from IOC container. | ||
/// Returning object must be Released (see <see cref="Release"/>) after usage. | ||
/// </summary> | ||
/// <typeparam name="T">Type of the object to cast</typeparam> | ||
/// <param name="type">Type of the object to resolve</param> | ||
/// <returns>The object instance</returns> | ||
public T Resolve<T>(Type type) | ||
{ | ||
return (T)_container.GetService(type); | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/ServiceAnt.IocInstaller.DotNetCore/ServiceAnt.IocInstaller.DotNetCore.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<Description>The IOC installer of ServiceAnt for dotnet core</Description> | ||
<PackageTags>$(PackageTags)</PackageTags> | ||
<TargetFramework>netcoreapp2.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\ServiceAnt\ServiceAnt.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="2.0.0" /> | ||
</ItemGroup> | ||
</Project> |
78 changes: 78 additions & 0 deletions
78
src/ServiceAnt.IocInstaller.DotNetCore/ServiceCollectionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using ServiceAnt.Handler.Request; | ||
using ServiceAnt.Subscription; | ||
using System; | ||
using System.Reflection; | ||
using System.Linq; | ||
using ServiceAnt.Base; | ||
using ServiceAnt.Request.Handler; | ||
using ServiceAnt.Infrastructure.Dependency; | ||
using ServiceAnt.IocInstaller.DotNetCore; | ||
using ServiceAnt; | ||
|
||
namespace Microsoft.Extensions.DependencyInjection | ||
{ | ||
public static class ServiceCollectionExtensions | ||
{ | ||
private static System.Reflection.Assembly[] _handlerAssemblies; | ||
|
||
public static void AddServiceAnt(this IServiceCollection @this, params Assembly[] handlerAssemblies) | ||
{ | ||
_handlerAssemblies = handlerAssemblies; | ||
|
||
var subcriptionsManager = new InMemorySubscriptionsManager(); | ||
var requestManager = new InMemoryRequestHandlerManager(); | ||
|
||
RegisterHandlers(subcriptionsManager, requestManager); | ||
@this.AddSingleton<ISubscriptionManager>(subcriptionsManager); | ||
@this.AddSingleton<IRequestHandlerManager>(requestManager); | ||
|
||
@this.AddTransient<IIocResolver>(serviceProvider => | ||
{ | ||
return new IocResolver(serviceProvider); | ||
}); | ||
@this.AddSingleton<IServiceBus, InProcessServiceBus>(); | ||
|
||
|
||
var allHandlerTypes = _handlerAssemblies.SelectMany(p => p.ExportedTypes).Where(p=>typeof(IHandler).IsAssignableFrom(p)); | ||
foreach (var aHandlerType in allHandlerTypes) | ||
{ | ||
@this.AddTransient(aHandlerType); | ||
} | ||
} | ||
|
||
private static void RegisterHandlers(ISubscriptionManager subcriptionsManager, IRequestHandlerManager requestManager) | ||
{ | ||
foreach (var aHandlerAssembly in _handlerAssemblies) | ||
{ | ||
var handlerTypes = aHandlerAssembly.GetTypes().Where(p => typeof(IHandler).IsAssignableFrom(p) && !p.IsInterface); | ||
|
||
foreach (var aHandler in handlerTypes) | ||
{ | ||
RegisterHandlerType(subcriptionsManager, requestManager, aHandler); | ||
} | ||
} | ||
} | ||
|
||
private static void RegisterHandlerType(ISubscriptionManager subcriptionsManager, IRequestHandlerManager requestManager, Type aHandlerType) | ||
{ | ||
var interfaces = aHandlerType.GetInterfaces(); | ||
foreach (var aInterface in interfaces) | ||
{ | ||
if (!typeof(IHandler).IsAssignableFrom(aInterface)) | ||
{ | ||
continue; | ||
} | ||
|
||
var genericArgs = aInterface.GetGenericArguments(); | ||
if (genericArgs.Length == 1) | ||
{ | ||
if (typeof(IRequestHandler).IsAssignableFrom(aInterface)) | ||
requestManager.AddRequestHandler(genericArgs[0], new IocHandlerFactory(aHandlerType, genericArgs[0])); | ||
else | ||
subcriptionsManager.AddSubscription(genericArgs[0], new IocHandlerFactory(aHandlerType, genericArgs[0])); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.