Skip to content

Commit

Permalink
Merge pull request #13 from freshncp/v2.x
Browse files Browse the repository at this point in the history
反射调用改为委托调用;拦截器类型改为对象传入
  • Loading branch information
berkaroad authored Jun 14, 2020
2 parents ba446a9 + eeceaf0 commit bcf538b
Show file tree
Hide file tree
Showing 43 changed files with 1,097 additions and 526 deletions.
14 changes: 9 additions & 5 deletions .github/workflows/dotnetcore.yml
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
name: .NET Core

on: [push]
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v2
- name: Setup .NET Core
uses: actions/setup-dotnet@v1
with:
dotnet-version: 2.2.108
dotnet-version: 3.1.101
- name: build
run: make build
- name: test
run: make test
- name: build
run: make rebuild
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -330,5 +330,5 @@ ASALocalRun/
.mfractor/

# Custom
nuget/*/lib/
.vscode/
.DS_Store
.vscode
34 changes: 8 additions & 26 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,30 +1,12 @@
all: test pack
test:
dotnet test `pwd`/src/DynamicProxyTests/
dotnet test `pwd`/src/Larva.DynamicProxy.Tests/

pack: rebuild
rm -rf `pwd`/nuget/.DS_Store
rm -rf `pwd`/nuget/*/.DS_Store
rm -rf `pwd`/nuget/*/*/.DS_Store
rm -rf `pwd`/nuget/*/lib/*/*.pdb
rm -rf `pwd`/nuget/*/lib/*/*.json
nuget pack -OutputDirectory `pwd`/packages/ `pwd`/nuget/Larva.DynamicProxy/Larva.DynamicProxy.nuspec
pack: build
mkdir -p `pwd`/packages
dotnet pack -c Release `pwd`/src/Larva.DynamicProxy/
mv `pwd`/src/Larva.DynamicProxy/bin/Release/*.nupkg `pwd`/packages/

rebuild: clean build

clean:
rm -rf `pwd`/nuget/.DS_Store
rm -rf `pwd`/nuget/*/.DS_Store
rm -rf `pwd`/nuget/*/*/.DS_Store
rm -rf `pwd`/nuget/*/lib/*

build: build-1_6 build-2_0 build-netFramework

build-1_6:
dotnet build -c Release -f 'netstandard1.6' -o `pwd`/nuget/Larva.DynamicProxy/lib/netstandard1.6/ `pwd`/src/Larva.DynamicProxy/

build-2_0:
dotnet build -c Release -f 'netstandard2.0' -o `pwd`/nuget/Larva.DynamicProxy/lib/netstandard2.0/ `pwd`/src/Larva.DynamicProxy/

build-netFramework:
msbuild `pwd`/src/Larva.DynamicProxy/Larva.DynamicProxy.csproj -r -noConLog -t:Rebuild -p:Configuration=Release -p:TargetFramework=net45 -p:OutputPath=`pwd`/nuget/Larva.DynamicProxy/lib/net45/
build:
dotnet build -c Release `pwd`/src/Larva.DynamicProxy/
dotnet build -c Release `pwd`/src/Larva.DynamicProxy.Tests/
24 changes: 11 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Larva.DynamicProxy

dotnet 动态代理类,用于AOP。可以结合IoC框架。此动态代理仅支持通过实现接口来创建代理类。

- 基于对象,返回指定接口的代理类对象,此代理类引用原始对象;
Expand All @@ -9,28 +10,25 @@ dotnet 动态代理类,用于AOP。可以结合IoC框架。此动态代理仅

- StandardInterceptor 支持拦截Task异步方法。

## 兼容性

- netstandard 1.6

- netstandard 2.0

- dotNetFramework 4.5 及以上

## 安装Nuget包
## 安装

```
Install-Package Larva.DynamicProxy
```sh
dotnet add package Larva.DynamicProxy
```

## 调用示例

示例参见:[DynamicProxyTests](https://github.com/freshncp/Larva.DynamicProxy/tree/master/src/DynamicProxyTests)
示例参见:[Larva.DynamicProxy.Tests](src/Larva.DynamicProxy.Tests)

## 更新历史

### 2.0.0-beta1 (更新日期:2020/06/14)

```plain
1)重构,取消反射调用,改为委托调用;
2)拦截器,由类型改为对象传入。
```

### 1.0.7 (更新日期:2019/12/17)

```plain
Expand Down
29 changes: 0 additions & 29 deletions nuget/Larva.DynamicProxy/Larva.DynamicProxy.nuspec

This file was deleted.

Binary file added package.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 0 additions & 11 deletions src/DynamicProxyTests/Application/IUserLoginService.cs

This file was deleted.

7 changes: 0 additions & 7 deletions src/DynamicProxyTests/Application/UserDto.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Threading.Tasks;

namespace DynamicProxyTests.Application
namespace Larva.DynamicProxy.Tests.Application
{

public class AnotherUserLoginService : IUserLoginService
Expand All @@ -9,8 +9,10 @@ private AnotherUserLoginService()
{
}

public bool Login(string userName, string password, out bool accountExists, ref int retryCount, out UserDto userDto)
public bool Login(string userName, string password, int sault, out bool accountExists, ref int retryCount, out UserDto userDto)
{
UserName = userName;
Sault = sault;
accountExists = true;
++retryCount;
userDto = null;
Expand All @@ -19,7 +21,12 @@ public bool Login(string userName, string password, out bool accountExists, ref

public Task<bool> LoginAsync(string userName, string password)
{
UserName = userName;
return Task.FromResult(false);
}

public string UserName { get; private set;}

public int Sault { get; set; }
}
}
15 changes: 15 additions & 0 deletions src/Larva.DynamicProxy.Tests/Application/IUserLoginService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Threading.Tasks;

namespace Larva.DynamicProxy.Tests.Application
{
public interface IUserLoginService
{
bool Login(string userName, string password, int sault, out bool accountExists, ref int retryCount, out UserDto userDto);

Task<bool> LoginAsync(string userName, string password);

string UserName { get; }

int Sault { get; set; }
}
}
12 changes: 12 additions & 0 deletions src/Larva.DynamicProxy.Tests/Application/UserDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Larva.DynamicProxy.Tests.Application
{
public class UserDto
{
public string RealName { get; set; }

public override string ToString()
{
return $"UserDto:RealName={RealName}";
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using DynamicProxyTests.Repositories;
using Larva.DynamicProxy.Tests.Repositories;
using System.Threading.Tasks;

namespace DynamicProxyTests.Application
namespace Larva.DynamicProxy.Tests.Application
{
public class UserLoginService : IUserLoginService
{
Expand All @@ -12,17 +12,24 @@ public UserLoginService(IUserLoginRepository userLoginRepository)
_userLoginRepository = userLoginRepository;
}

public bool Login(string userName, string password, out bool accountExists, ref int retryCount, out UserDto userDto)
public bool Login(string userName, string password, int sault, out bool accountExists, ref int retryCount, out UserDto userDto)
{
UserName = userName;
Sault = sault;
accountExists = true;
++retryCount;
userDto = new UserDto { RealName = userName };
return _userLoginRepository.Validate(userName, password);
return _userLoginRepository.Validate(userName, password, sault);
}

public async Task<bool> LoginAsync(string userName, string password)
{
UserName = userName;
return await _userLoginRepository.ValidateAsync(userName, password);
}

public string UserName { get; private set;}

public int Sault { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using System;
using DynamicProxyTests.Application;
using DynamicProxyTests.Interceptors;
using DynamicProxyTests.Repositories;
using Larva.DynamicProxy.Tests.Application;
using Larva.DynamicProxy.Tests.Interceptors;
using Larva.DynamicProxy.Tests.Repositories;
using Xunit;

namespace DynamicProxyTests
namespace Larva.DynamicProxy.Tests
{
public class DynamicProxyFactoryTest
{
Expand All @@ -13,15 +13,24 @@ public void TestCreateProxyT()
{
var userLoginService = Larva.DynamicProxy.DynamicProxyFactory.CreateProxy<IUserLoginService>(
new UserLoginService(new UserLoginRepository()),
new Type[] {
typeof(UserLoginCounterInterceptor),
typeof(PerformanceCounterInterceptor)
new IInterceptor[] {
new UserLoginCounterInterceptor(),
new PerformanceCounterInterceptor()
});
Assert.Equal($"{typeof(UserLoginService).Name}__DynamicProxyByInstance", userLoginService.GetType().Name);

int retryCount = 1;
userLoginService.Login("jack", "123456", out bool accountExists, ref retryCount, out UserDto userDto);
userLoginService.LoginAsync("rose", "123456")
var result1 = userLoginService.Login("jack", "123456", 996, out bool accountExists, ref retryCount, out UserDto userDto);
Assert.True(result1);
Assert.True(accountExists);
Assert.Equal(2, retryCount);
Assert.Equal("jack", userDto.RealName);
Assert.Equal("jack", userLoginService.UserName);
Assert.Equal(996, userLoginService.Sault);

var result2 = userLoginService.LoginAsync("rose", "123456")
.ConfigureAwait(false).GetAwaiter().GetResult();
Assert.True(result2);
}

[Fact]
Expand All @@ -30,13 +39,13 @@ public void TestCreateProxy()
var userLoginService = (IUserLoginService)Larva.DynamicProxy.DynamicProxyFactory.CreateProxy(
typeof(IUserLoginService),
new UserLoginService(new UserLoginRepository()),
new Type[] {
typeof(UserLoginCounterInterceptor),
typeof(PerformanceCounterInterceptor)
new IInterceptor[] {
new UserLoginCounterInterceptor(),
new PerformanceCounterInterceptor()
});
Assert.Equal($"{typeof(UserLoginService).Name}__DynamicProxyByInstance", userLoginService.GetType().Name);
int retryCount = 1;
userLoginService.Login("jack", "123456", out bool accountExists, ref retryCount, out UserDto userDto);
userLoginService.Login("jack", "123456", 996, out bool accountExists, ref retryCount, out UserDto userDto);
userLoginService.LoginAsync("rose", "123456")
.ConfigureAwait(false).GetAwaiter().GetResult();
}
Expand All @@ -47,9 +56,9 @@ public void TestCreateProxyType()
var userLoginServiceType = Larva.DynamicProxy.DynamicProxyFactory.CreateProxyType(
typeof(IUserLoginService),
typeof(UserLoginService),
new Type[] {
typeof(UserLoginCounterInterceptor),
typeof(PerformanceCounterInterceptor)
new IInterceptor[] {
new UserLoginCounterInterceptor(),
new PerformanceCounterInterceptor()
});
Assert.Equal($"{typeof(UserLoginService).Name}__DynamicProxyByNewObj", userLoginServiceType.Name);
var userLoginService = (IUserLoginService)Activator.CreateInstance(
Expand All @@ -58,22 +67,23 @@ public void TestCreateProxyType()
new UserLoginRepository()
});
int retryCount = 1;
userLoginService.Login("jack", "123456", out bool accountExists, ref retryCount, out UserDto userDto);
userLoginService.Login("jack", "123456", 996, out bool accountExists, ref retryCount, out UserDto userDto);
userLoginService.LoginAsync("rose", "123456")
.ConfigureAwait(false).GetAwaiter().GetResult();
}

[Fact]
public void TestCreateProxyTypeWhenNoPublicConstructor()
{
Assert.Throws<Larva.DynamicProxy.InvalidProxiedTypeException>(() => {
Assert.Throws<Larva.DynamicProxy.InvalidProxiedTypeException>(() =>
{
var userLoginServiceType = Larva.DynamicProxy.DynamicProxyFactory.CreateProxyType(
typeof(IUserLoginService),
typeof(AnotherUserLoginService),
new Type[] {
typeof(UserLoginCounterInterceptor),
typeof(PerformanceCounterInterceptor)
});
new IInterceptor[] {
new UserLoginCounterInterceptor(),
new PerformanceCounterInterceptor()
});
});
}
}
Expand Down
Loading

0 comments on commit bcf538b

Please sign in to comment.