diff --git a/SampleApp/ITestClass.cs b/SampleApp/ITestClass.cs index 173e383..ef912d6 100644 --- a/SampleApp/ITestClass.cs +++ b/SampleApp/ITestClass.cs @@ -5,8 +5,19 @@ namespace SampleApp public interface ITestClass { + string Instance { get; set; } DateTime TestMethod(); DateTime TestMethodWithExpirationPolicy(); Task TestMethodAsync(); } + + public interface ICommon + { + string Instance { get; set; } + } + + public interface IDBMock + { + string Instance { get; set; } + } } diff --git a/SampleApp/Program.cs b/SampleApp/Program.cs index cd68dce..9345da6 100644 --- a/SampleApp/Program.cs +++ b/SampleApp/Program.cs @@ -13,7 +13,18 @@ namespace SampleApp { public class Program { - public static async Task Main(string[] args) + public static void Main(string[] args) + { + for (int i = 0; i <= 300; i++) + { + AddScopedTest(); + Console.WriteLine($"Run completed for index {i}"); + } + Console.WriteLine("All completed."); + Console.ReadLine(); + } + + private static async Task AddScopedTest() { // Configure the Service Provider var services = new ServiceCollection(); @@ -22,6 +33,8 @@ public static async Task Main(string[] args) services.AddOptions(); services.AddMemoryCache(); services.AddLogging(p => p.AddConsole(x => x.IncludeScopes = true).SetMinimumLevel(LogLevel.Trace)); + services.AddScoped(); + services.AddScoped(); services.EnableSimpleProxy(p => p .AddInterceptor() @@ -29,25 +42,24 @@ public static async Task Main(string[] args) .AddInterceptor() .WithOrderingStrategy()); - services.AddTransientWithProxy(); + services.AddScopedWithProxy(); // Get a Proxied Class and call a method var serviceProvider = services.BuildServiceProvider(); var testProxy = serviceProvider.GetService(); + System.Diagnostics.Debug.WriteLine($"Test Class Instance {testProxy.Instance}"); + for (int j = 0; j < 100; j++) + { + var testProxy1 = serviceProvider.GetService(); + System.Diagnostics.Debug.WriteLine($"Test Class Instance {testProxy1.Instance}"); - testProxy.TestMethod(); - - await testProxy.TestMethodAsync(); - - testProxy.TestMethodWithExpirationPolicy(); // set the cache - testProxy.TestMethodWithExpirationPolicy(); // execute just using the cache value - System.Threading.Thread.Sleep(25000); // time to expire the registry - testProxy.TestMethodWithExpirationPolicy(); // execute the method again + testProxy.TestMethod(); + testProxy.TestMethodWithExpirationPolicy(); // set the cache + } Console.WriteLine("====> All Test Methods Complete. Press a key. <===="); - Console.ReadLine(); } } } diff --git a/SampleApp/SampleApp.csproj b/SampleApp/SampleApp.csproj index a665cba..1092c75 100644 --- a/SampleApp/SampleApp.csproj +++ b/SampleApp/SampleApp.csproj @@ -8,8 +8,8 @@ - - + + diff --git a/SampleApp/TestClass.cs b/SampleApp/TestClass.cs index 9b8a665..58c8cab 100644 --- a/SampleApp/TestClass.cs +++ b/SampleApp/TestClass.cs @@ -12,20 +12,28 @@ namespace SampleApp /// public class TestClass : ITestClass { + private static int count = 0; + /// /// The Logger /// private readonly ILogger logger; + private readonly ICommon common; + /// /// Initialises a new instance of the /// /// Logger Factory - public TestClass(ILoggerFactory loggerFactory) + public TestClass(ILoggerFactory loggerFactory, ICommon common) { this.logger = loggerFactory.CreateLogger(); + this.common = common; + this.Instance = $"Test Class Instnce: {++count}. -- Common: {common.Instance} "; } + public string Instance { get; set; } + /// /// Test Method /// @@ -69,4 +77,30 @@ public Task TestMethodAsync() } } + public class Common : ICommon + { + private static int count = 0; + public IDBMock mock; + public Common(IDBMock dBMock) + { + this.mock = dBMock; + + Instance = $"Common Instance Id {++count} -- DBMOck Id: {mock.Instance }"; + } + public string Instance { get; set; } + } + + public class DBMOck : IDBMock + { + private static int count = 0; + + public DBMOck() + { + Instance = $"DB MOCK ID : {++count}"; + + } + + public string Instance { get; set; } + } + } diff --git a/SimpleProxy.Logging/SimpleProxy.Logging.csproj b/SimpleProxy.Logging/SimpleProxy.Logging.csproj index e4b7d6e..5ee8ab1 100644 --- a/SimpleProxy.Logging/SimpleProxy.Logging.csproj +++ b/SimpleProxy.Logging/SimpleProxy.Logging.csproj @@ -5,7 +5,7 @@ - + diff --git a/SimpleProxy.sln b/SimpleProxy.sln index ae71018..8f54b0e 100644 --- a/SimpleProxy.sln +++ b/SimpleProxy.sln @@ -1,26 +1,10 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 15 -VisualStudioVersion = 15.0.28307.489 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.31112.23 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SimpleProxy", "SimpleProxy\SimpleProxy.csproj", "{076C09AD-12A5-4CE0-BE41-B461606FED62}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SampleApp", "SampleApp\SampleApp.csproj", "{F1589C2B-CA8A-4B56-B964-C64C561ADB01}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SimpleProxy.Caching", "SimpleProxy.Caching\SimpleProxy.Caching.csproj", "{3B583E8E-6FB9-40E6-A294-35A26F9A9430}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SimpleProxy.Diagnostics", "SimpleProxy.Diagnostics\SimpleProxy.Diagnostics.csproj", "{D513380A-89DF-4074-B6C9-EA146AE7D8B5}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SimpleProxy.Logging", "SimpleProxy.Logging\SimpleProxy.Logging.csproj", "{65B0C87F-FA32-4EC3-8D67-15D1746924E2}" -EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SimpleProxy.UnitOfWork", "SimpleProxy.UnitOfWork\SimpleProxy.UnitOfWork.csproj", "{8E20AC91-9FBA-4975-90EE-46078318F4C8}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimpleProxy.Tests", "Unit Tests\SimpleProxy.Tests\SimpleProxy.Tests.csproj", "{4463FE87-A9DE-4076-B942-595C0B107924}" -EndProject -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Unit Tests", "Unit Tests", "{8E20A6AB-AC37-43E6-89F9-BF21FEB70339}" -EndProject -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Interceptors", "Interceptors", "{08F8CC95-6646-4F02-8A2C-C7884CA0DBD0}" -EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Core", "Core", "{4A4DE7A9-6C40-4F7F-9E49-0090BAA9B1E1}" EndProject Global @@ -33,41 +17,12 @@ Global {076C09AD-12A5-4CE0-BE41-B461606FED62}.Debug|Any CPU.Build.0 = Debug|Any CPU {076C09AD-12A5-4CE0-BE41-B461606FED62}.Release|Any CPU.ActiveCfg = Release|Any CPU {076C09AD-12A5-4CE0-BE41-B461606FED62}.Release|Any CPU.Build.0 = Release|Any CPU - {F1589C2B-CA8A-4B56-B964-C64C561ADB01}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {F1589C2B-CA8A-4B56-B964-C64C561ADB01}.Debug|Any CPU.Build.0 = Debug|Any CPU - {F1589C2B-CA8A-4B56-B964-C64C561ADB01}.Release|Any CPU.ActiveCfg = Release|Any CPU - {F1589C2B-CA8A-4B56-B964-C64C561ADB01}.Release|Any CPU.Build.0 = Release|Any CPU - {3B583E8E-6FB9-40E6-A294-35A26F9A9430}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {3B583E8E-6FB9-40E6-A294-35A26F9A9430}.Debug|Any CPU.Build.0 = Debug|Any CPU - {3B583E8E-6FB9-40E6-A294-35A26F9A9430}.Release|Any CPU.ActiveCfg = Release|Any CPU - {3B583E8E-6FB9-40E6-A294-35A26F9A9430}.Release|Any CPU.Build.0 = Release|Any CPU - {D513380A-89DF-4074-B6C9-EA146AE7D8B5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {D513380A-89DF-4074-B6C9-EA146AE7D8B5}.Debug|Any CPU.Build.0 = Debug|Any CPU - {D513380A-89DF-4074-B6C9-EA146AE7D8B5}.Release|Any CPU.ActiveCfg = Release|Any CPU - {D513380A-89DF-4074-B6C9-EA146AE7D8B5}.Release|Any CPU.Build.0 = Release|Any CPU - {65B0C87F-FA32-4EC3-8D67-15D1746924E2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {65B0C87F-FA32-4EC3-8D67-15D1746924E2}.Debug|Any CPU.Build.0 = Debug|Any CPU - {65B0C87F-FA32-4EC3-8D67-15D1746924E2}.Release|Any CPU.ActiveCfg = Release|Any CPU - {65B0C87F-FA32-4EC3-8D67-15D1746924E2}.Release|Any CPU.Build.0 = Release|Any CPU - {8E20AC91-9FBA-4975-90EE-46078318F4C8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {8E20AC91-9FBA-4975-90EE-46078318F4C8}.Debug|Any CPU.Build.0 = Debug|Any CPU - {8E20AC91-9FBA-4975-90EE-46078318F4C8}.Release|Any CPU.ActiveCfg = Release|Any CPU - {8E20AC91-9FBA-4975-90EE-46078318F4C8}.Release|Any CPU.Build.0 = Release|Any CPU - {4463FE87-A9DE-4076-B942-595C0B107924}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {4463FE87-A9DE-4076-B942-595C0B107924}.Debug|Any CPU.Build.0 = Debug|Any CPU - {4463FE87-A9DE-4076-B942-595C0B107924}.Release|Any CPU.ActiveCfg = Release|Any CPU - {4463FE87-A9DE-4076-B942-595C0B107924}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection GlobalSection(NestedProjects) = preSolution {076C09AD-12A5-4CE0-BE41-B461606FED62} = {4A4DE7A9-6C40-4F7F-9E49-0090BAA9B1E1} - {3B583E8E-6FB9-40E6-A294-35A26F9A9430} = {08F8CC95-6646-4F02-8A2C-C7884CA0DBD0} - {D513380A-89DF-4074-B6C9-EA146AE7D8B5} = {08F8CC95-6646-4F02-8A2C-C7884CA0DBD0} - {65B0C87F-FA32-4EC3-8D67-15D1746924E2} = {08F8CC95-6646-4F02-8A2C-C7884CA0DBD0} - {8E20AC91-9FBA-4975-90EE-46078318F4C8} = {08F8CC95-6646-4F02-8A2C-C7884CA0DBD0} - {4463FE87-A9DE-4076-B942-595C0B107924} = {8E20A6AB-AC37-43E6-89F9-BF21FEB70339} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {8F627740-E1C8-4756-8BA6-39E4135E9350} diff --git a/SimpleProxy/Extensions/ServiceCollectionExtensions.cs b/SimpleProxy/Extensions/ServiceCollectionExtensions.cs index 66ad411..62d7de4 100644 --- a/SimpleProxy/Extensions/ServiceCollectionExtensions.cs +++ b/SimpleProxy/Extensions/ServiceCollectionExtensions.cs @@ -48,13 +48,17 @@ public static IServiceCollection AddTransientWithProxy(thi var serviceProvider = services.BuildServiceProvider(); var proxyConfiguration = services.GetProxyConfiguration(); var proxyGenerator = serviceProvider.GetService(); - var proxyInstance = ActivatorUtilities.CreateInstance(serviceProvider); // Wrap the service with a Proxy instance and add it with Transient Scope services.AddTransient(typeof(TInterface), - p => new ProxyFactory(serviceProvider, proxyGenerator, proxyConfiguration) - .CreateProxy(ActivatorUtilities.CreateInstance(serviceProvider))); - + p => + { + return new ProxyFactory( + serviceProvider, + proxyGenerator, + proxyConfiguration) + .CreateProxy(ActivatorUtilities.CreateInstance(services.BuildServiceProvider())); + }); // Return the IServiceCollection for chaining configuration return services; } @@ -68,13 +72,20 @@ public static IServiceCollection AddTransientWithProxy(thi /// public static IServiceCollection AddScopedWithProxy(this IServiceCollection services) where TService : TInterface { + var serviceProvider = services.BuildServiceProvider(); var proxyConfiguration = services.GetProxyConfiguration(); var proxyGenerator = serviceProvider.GetService(); - var proxyInstance = ActivatorUtilities.CreateInstance(serviceProvider); - // Wrap the service with a Proxy instance and add it with Scoped Scope - services.AddScoped(typeof(TInterface), p => new ProxyFactory(serviceProvider, proxyGenerator, proxyConfiguration).CreateProxy(proxyInstance)); + services.AddScoped(typeof(TInterface), + p => + { + return new ProxyFactory( + serviceProvider, + proxyGenerator, + proxyConfiguration) + .CreateProxy(ActivatorUtilities.CreateInstance(services.BuildServiceProvider())); + }); // Return the IServiceCollection for chaining configuration return services; diff --git a/SimpleProxy/Internal/CoreInterceptor.cs b/SimpleProxy/Internal/CoreInterceptor.cs index 1e1fbce..0466f5f 100644 --- a/SimpleProxy/Internal/CoreInterceptor.cs +++ b/SimpleProxy/Internal/CoreInterceptor.cs @@ -41,13 +41,15 @@ public void InterceptSynchronous(IInvocation invocation) { var proceedWithInterception = InterceptBeforeProceed(invocation, out var invocationMetadataCollection, out var orderingStrategy); - if (!proceedWithInterception) { + if (!proceedWithInterception) + { invocation.Proceed(); return; } // Execute the Real Method - if (!invocationMetadataCollection.Any(p => p.InvocationIsBypassed)) { + if (!invocationMetadataCollection.Any(p => p.InvocationIsBypassed)) + { invocation.Proceed(); } @@ -63,7 +65,8 @@ private async Task InternalInterceptAsynchronousAsync(IInvocation invocation) { var proceedWithInterception = InterceptBeforeProceed(invocation, out var invocationMetadataCollection, out var orderingStrategy); - if (!proceedWithInterception) { + if (!proceedWithInterception) + { invocation.Proceed(); var task = (Task)invocation.ReturnValue; await task; @@ -71,7 +74,8 @@ private async Task InternalInterceptAsynchronousAsync(IInvocation invocation) } // Execute the Real Method - if (!invocationMetadataCollection.Any(p => p.InvocationIsBypassed)) { + if (!invocationMetadataCollection.Any(p => p.InvocationIsBypassed)) + { invocation.Proceed(); var task = (Task)invocation.ReturnValue; await task; @@ -91,7 +95,8 @@ private async Task InternalInterceptAsynchronousAsync(IInvocat TResult result; - if (!proceedWithInterception) { + if (!proceedWithInterception) + { invocation.Proceed(); var task = (Task)invocation.ReturnValue; result = await task; @@ -99,12 +104,14 @@ private async Task InternalInterceptAsynchronousAsync(IInvocat } // Execute the Real Method - if (!invocationMetadataCollection.Any(p => p.InvocationIsBypassed)) { + if (!invocationMetadataCollection.Any(p => p.InvocationIsBypassed)) + { invocation.Proceed(); var task = (Task)invocation.ReturnValue; result = await task; } - else { + else + { result = default; } @@ -119,7 +126,8 @@ private bool InterceptBeforeProceed(IInvocation invocation, out List invocationMetadataCollection, IOrderingStrategy orderingStrategy) { // Process the AFTER Interceptions - foreach (var invocationContext in orderingStrategy.OrderAfterInterception(invocationMetadataCollection)) { + foreach (var invocationContext in orderingStrategy.OrderAfterInterception(invocationMetadataCollection)) + { invocationContext.Interceptor.AfterInvoke(invocationContext, invocationContext.GetMethodReturnValue()); } } diff --git a/SimpleProxy/SimpleProxy.csproj b/SimpleProxy/SimpleProxy.csproj index 1bf7096..02831e1 100644 --- a/SimpleProxy/SimpleProxy.csproj +++ b/SimpleProxy/SimpleProxy.csproj @@ -2,17 +2,30 @@ netcoreapp3.1 - Simple Proxy is a simple extension library built to solve the problem of Aspect Orientated Programming in Net Core projects + Simple Proxy is a simple extension library built to solve the problem of Aspect Orientated Programming in Net Core projects. Robert Perry - https://github.com/f135ta/SimpleProxy + + 1.0.11 + false + SimpleProxyCp + SimpleProxyCp + SimpleProxyCp + SimpleProxyCp + LICENSE - - - - - + + + + + + + + + True + +