Skip to content

Commit

Permalink
Merge branch 'CustomAspect'
Browse files Browse the repository at this point in the history
  • Loading branch information
Asli Yigit authored and Asli Yigit committed Dec 8, 2023
2 parents b05cb37 + 9ef8582 commit 1376c71
Show file tree
Hide file tree
Showing 22 changed files with 144 additions and 602 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace NexusAop.CustomAspect
namespace NexusAop.Console.CustomAspect
{
public class CustomAspectAttribute : Attribute
public class CustomAspectAttribute : NexusAopAttribute
{
public Dictionary<string, object> Properties { get; }

Expand All @@ -22,5 +23,21 @@ public CustomAspectAttribute(params object[] propertyValues)
Properties[propertyName] = propertyValues[i + 1];
}
}

public override async Task ExecuteAsync(NexusAopContext context)
{
// User-defined logic before the target method
//System.Console.WriteLine("Before invoking the target method.");

// Proceed with the execution of the target method

var result=await context.NextAsync();

var setResult= await context.SetResultAsync();

// User-defined logic after the target method
//System.Console.WriteLine("After invoking the target method.");
// return Task.CompletedTask;
}
}
}
15 changes: 0 additions & 15 deletions samples/NexusAop.Console/Models/FooEntity.cs

This file was deleted.

14 changes: 0 additions & 14 deletions samples/NexusAop.Console/MyTestAop.cs

This file was deleted.

36 changes: 5 additions & 31 deletions samples/NexusAop.Console/Program.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
// See https://aka.ms/new-console-template for more information

using System;
using System.Diagnostics;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NexusAop.Cache;
using NexusAop.Console.Repository;
using NexusAop.Console.Service;
using NexusAop.CustomAspect;
using NexusAop.Extensions;

namespace NexusAop.Console
Expand All @@ -21,10 +15,8 @@ public static async Task Main(
string[] arg)
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddNexusAop();
//serviceCollection.AddSingletonWithAop<ITestService, TestService>();
serviceCollection.AddSingletonWithCustomAop<ITestService, TestService>();
serviceCollection.AddSingleton<IFooRepository, FooRepository>();

serviceCollection.AddLogging(configure =>
{
configure.AddConsole();
Expand All @@ -33,34 +25,16 @@ public static async Task Main(
var provider = serviceCollection.BuildServiceProvider();

var svc = provider.GetRequiredService<ITestService>();
svc.MyMethod();

//for (var i = 0; i < 3; i++)
//{
// var isGoodNumber = i % 2 == 0;
// var stopWatch = Stopwatch.StartNew();
// var result = await svc.GetFooAsync(isGoodNumber, CancellationToken.None);
// stopWatch.Stop();
// System.Console.WriteLine($"[Loop {i}] Method result is {result} with parameter {isGoodNumber} & total execution is {stopWatch.ElapsedMilliseconds} ms");
// await Task.Delay(TimeSpan.FromSeconds(2));
//}

svc.MyStringMethod();

//TestService myClass = new TestService(new FooRepository());
//Type type = myClass.GetType();
//var methodInfo = type.GetMethod("MyMethod");
svc.MyVoidMethod();

//var customAspectAttributes = methodInfo.GetCustomAttributes(typeof(CustomAspectAttribute), true);
await svc.MyMethodTask();

//if (customAspectAttributes.Length > 0)
//{
// CustomAspectAttribute aspect = (CustomAspectAttribute)customAspectAttributes[0];
await svc.MyMethodTaskReturnString();

// foreach (var property in aspect.Properties)
// {
// System.Console.WriteLine($"{property.Key}: {property.Value}");
// }
//}

}
}
Expand Down
30 changes: 0 additions & 30 deletions samples/NexusAop.Console/Repository/FooRepository.cs

This file was deleted.

15 changes: 0 additions & 15 deletions samples/NexusAop.Console/Repository/IFooRepository.cs

This file was deleted.

30 changes: 13 additions & 17 deletions samples/NexusAop.Console/Service/ITestService.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
using System.Threading;
using System.Threading.Tasks;
using NexusAop.Cache;
using NexusAop.Console.Models;
using NexusAop.CustomAspect;
using NexusAop.Console.CustomAspect;

namespace NexusAop.Console.Service
{
public interface ITestService
{
void SayHello(string name);

[CacheMethod(10)]
int GetBalance(
int id);

[CacheMethod(20)]
Task<FooEntity> GetFooAsync(
bool isGoodNumber,
CancellationToken cancellationToken = default);
{
[CustomAspect]
void MyVoidMethod();

[CustomAspect]
string MyStringMethod();

[CustomAspect]
Task MyMethodTask();

[CustomAspect]
Task<string> MyMethodTaskReturnString();

[CustomAspect("Property1", "Value1", "Property2", 42, "Property3", true)]
void MyMethod();
}
}
55 changes: 12 additions & 43 deletions samples/NexusAop.Console/Service/TestService.cs
Original file line number Diff line number Diff line change
@@ -1,59 +1,28 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using NexusAop.Console.Models;
using NexusAop.Console.Repository;
using NexusAop.CustomAspect;

namespace NexusAop.Console.Service
{
public class TestService : ITestService
{
private readonly IFooRepository _fooRepository;

public TestService(
IFooRepository fooRepository)
{
public string MyStringMethod()
{
_fooRepository = fooRepository;
//System.Console.WriteLine("My string method.");
return "My string method.";
}

public void SayHello(
string name)
public void MyVoidMethod()
{
if (string.IsNullOrEmpty(name))
{
throw new ArgumentNullException();
}

System.Console.WriteLine($"Hi {name}");

//System.Console.WriteLine("My void method.");
}

public int GetBalance(
int id)
public async Task MyMethodTask()
{
System.Console.WriteLine($"Checking id for {id}!");
var balance = new Random().Next(11111);
Task.Delay(balance).GetAwaiter().GetResult();
return balance;
//System.Console.WriteLine("MyMethod returns task.");
await Task.CompletedTask;
}

public async Task<FooEntity> GetFooAsync(
bool isGoodNumber,
CancellationToken cancellationToken = default)
{
var number = new Random().Next(9000);
await Task.Delay(number, cancellationToken);
var foo = await _fooRepository.GetByIdAsync(2, cancellationToken);
return new FooEntity()
{
Id = number,
Text = DateTime.Now.ToLongDateString()
};
}
public void MyMethod()
public async Task<string> MyMethodTaskReturnString()
{
System.Console.WriteLine("MyMethod has custom aspect properties.");
//System.Console.WriteLine("MyMethod returns Task<string>.");
return "thats ok";
}
}
}
21 changes: 0 additions & 21 deletions src/NexusAop/Cache/CacheMethodAttribute.cs

This file was deleted.

15 changes: 0 additions & 15 deletions src/NexusAop/Cache/INexusAopCacher.cs

This file was deleted.

40 changes: 0 additions & 40 deletions src/NexusAop/Cache/InmemoryNexusAopCacheService.cs

This file was deleted.

19 changes: 0 additions & 19 deletions src/NexusAop/CustomAspect/CustomAspectService.cs

This file was deleted.

Loading

0 comments on commit 1376c71

Please sign in to comment.