efcore2下修复iasyncenumerable的current报错问题

This commit is contained in:
xuejiaming 2021-09-29 14:42:50 +08:00
parent 3aec0399d0
commit 85f854577d
4 changed files with 74 additions and 85 deletions

View File

@ -1,8 +1,8 @@
:start :start
::定义版本 ::定义版本
set EFCORE2=2.2.0.21 set EFCORE2=2.2.0.22-efcore2.1.0
set EFCORE3=3.2.0.21 set EFCORE3=3.2.0.22
set EFCORE5=5.2.0.21 set EFCORE5=5.2.0.22
::删除所有bin与obj下的文件 ::删除所有bin与obj下的文件
@echo off @echo off

View File

@ -0,0 +1,42 @@

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ShardingCore.Sharding.Enumerators.StreamMergeAsync.EFCore2x
{
/*
* @Author: xjm
* @Description:
* @Date: 2021/9/29 14:11:30
* @Ver: 1.0
* @Email: 326308290@qq.com
*/
#if EFCORE2
public class EFCore2TryCurrentAsyncEnumerator<T>:IAsyncEnumerator<T>
{
private readonly IAsyncEnumerator<T> _asyncEnumerator;
private bool currentMoved=false;
public EFCore2TryCurrentAsyncEnumerator(IAsyncEnumerator<T> asyncEnumerator)
{
_asyncEnumerator = asyncEnumerator;
}
public void Dispose()
{
_asyncEnumerator?.Dispose();
}
public async Task<bool> MoveNext(CancellationToken cancellationToken)
{
var moveNext = await _asyncEnumerator.MoveNext(cancellationToken);
currentMoved = moveNext;
return moveNext;
}
public T Current => currentMoved ? _asyncEnumerator.Current : default;
}
#endif
}

View File

@ -6,12 +6,6 @@ using System.Threading.Tasks;
namespace ShardingCore.Sharding.Enumerators namespace ShardingCore.Sharding.Enumerators
{ {
/*
* @Author: xjm
* @Description:
* @Date: Saturday, 14 August 2021 21:25:50
* @Email: 326308290@qq.com
*/
public class StreamMergeAsyncEnumerator<T> : IStreamMergeAsyncEnumerator<T> public class StreamMergeAsyncEnumerator<T> : IStreamMergeAsyncEnumerator<T>
{ {
private readonly IAsyncEnumerator<T> _asyncSource; private readonly IAsyncEnumerator<T> _asyncSource;
@ -22,6 +16,7 @@ namespace ShardingCore.Sharding.Enumerators
{ {
if (_syncSource != null) if (_syncSource != null)
throw new ArgumentNullException(nameof(_syncSource)); throw new ArgumentNullException(nameof(_syncSource));
_asyncSource = asyncSource; _asyncSource = asyncSource;
skip = true; skip = true;
} }
@ -60,18 +55,38 @@ namespace ShardingCore.Sharding.Enumerators
return await _asyncSource.MoveNextAsync(); return await _asyncSource.MoveNextAsync();
} }
public T Current => GetCurrent(); public void Dispose()
public T ReallyCurrent => GetReallyCurrent(); {
_syncSource?.Dispose();
}
#endif
public bool MoveNext()
{
if (skip)
{
skip = false;
return null != _syncSource.Current;
}
return _syncSource.MoveNext();
}
public bool HasElement() public bool HasElement()
{ {
if (_asyncSource != null) return null != _asyncSource.Current; if (_asyncSource != null) return null != _asyncSource.Current;
if (_syncSource != null) return null != _syncSource.Current; if (_syncSource != null) return null != _syncSource.Current;
return false; return false;
} }
public void Dispose()
public void Reset()
{ {
_syncSource?.Dispose(); throw new NotImplementedException();
} }
object IEnumerator.Current => Current;
public T Current => GetCurrent();
public T ReallyCurrent => GetReallyCurrent();
public T GetCurrent() public T GetCurrent()
{ {
if (skip) if (skip)
@ -86,26 +101,6 @@ namespace ShardingCore.Sharding.Enumerators
if (_syncSource != null) return _syncSource.Current; if (_syncSource != null) return _syncSource.Current;
return default; return default;
} }
public bool MoveNext()
{
if (skip)
{
skip = false;
return null != _syncSource.Current;
}
return _syncSource.MoveNext();
}
#endif
public void Reset()
{
throw new NotImplementedException();
}
object IEnumerator.Current => Current;
#if EFCORE2 #if EFCORE2
public void Dispose() public void Dispose()
{ {
@ -118,61 +113,11 @@ namespace ShardingCore.Sharding.Enumerators
if (skip) if (skip)
{ {
skip = false; skip = false;
return null != SourceCurrent(); return null != _asyncSource.Current;
} }
return await _asyncSource.MoveNext(cancellationToken); return await _asyncSource.MoveNext(cancellationToken);
} }
public T Current => GetCurrent();
public T ReallyCurrent => GetReallyCurrent();
public bool HasElement()
{
return null != SourceCurrent();
}
private T SourceCurrent()
{
try
{
if (tryGetCurrentError)
return default;
if (_asyncSource!= null)
return _asyncSource.Current;
if (_syncSource != null)
return _syncSource.Current;
return default;
}
catch (Exception e)
{
tryGetCurrentError = true;
return default;
}
}
private bool tryGetCurrentError = false;
public T GetCurrent()
{
if (skip)
return default;
if (_asyncSource != null) return SourceCurrent();
if (_syncSource != null) return _syncSource.Current;
return default;
}
public T GetReallyCurrent()
{
if (_asyncSource != null) return SourceCurrent();
if (_syncSource != null) return _syncSource.Current;
return default;
}
public bool MoveNext()
{
if (skip)
{
skip = false;
return null != _syncSource.Current;
}
return _syncSource.MoveNext();
}
#endif #endif
} }

View File

@ -10,6 +10,7 @@ using ShardingCore.Exceptions;
using ShardingCore.Extensions; using ShardingCore.Extensions;
using ShardingCore.Sharding.Enumerators; using ShardingCore.Sharding.Enumerators;
using ShardingCore.Sharding.Enumerators.StreamMergeAsync; using ShardingCore.Sharding.Enumerators.StreamMergeAsync;
using ShardingCore.Sharding.Enumerators.StreamMergeAsync.EFCore2x;
#if EFCORE2 #if EFCORE2
using Microsoft.EntityFrameworkCore.Extensions.Internal; using Microsoft.EntityFrameworkCore.Extensions.Internal;
#endif #endif
@ -66,7 +67,8 @@ namespace ShardingCore.Sharding.StreamMergeEngines.EnumeratorStreamMergeEngines.
return enumator; return enumator;
#endif #endif
#if EFCORE2 #if EFCORE2
var enumator = newQueryable.AsAsyncEnumerable().GetEnumerator(); var enumator =
new EFCore2TryCurrentAsyncEnumerator<TEntity>(newQueryable.AsAsyncEnumerable().GetEnumerator());
await enumator.MoveNext(); await enumator.MoveNext();
return enumator; return enumator;
#endif #endif