Enable nullable annotations for Volo.Abp.Uow

pull/17020/head
liangshiwei 2 years ago
parent e7e1dd1f44
commit d63f205526

@ -5,6 +5,8 @@
<PropertyGroup>
<TargetFrameworks>netstandard2.0;netstandard2.1;net7.0</TargetFrameworks>
<Nullable>enable</Nullable>
<WarningsAsErrors>Nullable</WarningsAsErrors>
<AssemblyName>Volo.Abp.Uow</AssemblyName>
<PackageId>Volo.Abp.Uow</PackageId>
<AssetTargetFallback>$(AssetTargetFallback);portable-net45+win8+wp8+wpa81;</AssetTargetFallback>

@ -12,7 +12,7 @@ public class AlwaysDisableTransactionsUnitOfWorkManager : IUnitOfWorkManager
_unitOfWorkManager = unitOfWorkManager;
}
public IUnitOfWork Current => _unitOfWorkManager.Current;
public IUnitOfWork? Current => _unitOfWorkManager.Current;
public IUnitOfWork Begin(AbpUnitOfWorkOptions options, bool requiresNew = false)
{

@ -6,21 +6,21 @@ namespace Volo.Abp.Uow;
[ExposeServices(typeof(IAmbientUnitOfWork), typeof(IUnitOfWorkAccessor))]
public class AmbientUnitOfWork : IAmbientUnitOfWork, ISingletonDependency
{
public IUnitOfWork UnitOfWork => _currentUow.Value;
public IUnitOfWork? UnitOfWork => _currentUow.Value;
private readonly AsyncLocal<IUnitOfWork> _currentUow;
private readonly AsyncLocal<IUnitOfWork?> _currentUow;
public AmbientUnitOfWork()
{
_currentUow = new AsyncLocal<IUnitOfWork>();
_currentUow = new AsyncLocal<IUnitOfWork?>();
}
public void SetUnitOfWork(IUnitOfWork unitOfWork)
public void SetUnitOfWork(IUnitOfWork? unitOfWork)
{
_currentUow.Value = unitOfWork;
}
public IUnitOfWork GetCurrentByChecking()
public IUnitOfWork? GetCurrentByChecking()
{
var uow = UnitOfWork;

@ -10,9 +10,9 @@ internal class ChildUnitOfWork : IUnitOfWork
{
public Guid Id => _parent.Id;
public IAbpUnitOfWorkOptions Options => _parent.Options;
public IAbpUnitOfWorkOptions? Options => _parent.Options;
public IUnitOfWork Outer => _parent.Outer;
public IUnitOfWork? Outer => _parent.Outer;
public bool IsReserved => _parent.IsReserved;
@ -20,10 +20,10 @@ internal class ChildUnitOfWork : IUnitOfWork
public bool IsCompleted => _parent.IsCompleted;
public string ReservationName => _parent.ReservationName;
public string? ReservationName => _parent.ReservationName;
public event EventHandler<UnitOfWorkFailedEventArgs> Failed;
public event EventHandler<UnitOfWorkEventArgs> Disposed;
public event EventHandler<UnitOfWorkFailedEventArgs> Failed = default!;
public event EventHandler<UnitOfWorkEventArgs> Disposed = default!;
public IServiceProvider ServiceProvider => _parent.ServiceProvider;
@ -37,11 +37,11 @@ internal class ChildUnitOfWork : IUnitOfWork
_parent = parent;
_parent.Failed += (sender, args) => { Failed.InvokeSafely(sender, args); };
_parent.Disposed += (sender, args) => { Disposed.InvokeSafely(sender, args); };
_parent.Failed += (sender, args) => { Failed.InvokeSafely(sender!, args); };
_parent.Disposed += (sender, args) => { Disposed.InvokeSafely(sender!, args); };
}
public void SetOuter(IUnitOfWork outer)
public void SetOuter(IUnitOfWork? outer)
{
_parent.SetOuter(outer);
}
@ -78,19 +78,19 @@ internal class ChildUnitOfWork : IUnitOfWork
public void AddOrReplaceLocalEvent(
UnitOfWorkEventRecord eventRecord,
Predicate<UnitOfWorkEventRecord> replacementSelector = null)
Predicate<UnitOfWorkEventRecord>? replacementSelector = null)
{
_parent.AddOrReplaceLocalEvent(eventRecord, replacementSelector);
}
public void AddOrReplaceDistributedEvent(
UnitOfWorkEventRecord eventRecord,
Predicate<UnitOfWorkEventRecord> replacementSelector = null)
Predicate<UnitOfWorkEventRecord>? replacementSelector = null)
{
_parent.AddOrReplaceDistributedEvent(eventRecord, replacementSelector);
}
public IDatabaseApi FindDatabaseApi(string key)
public IDatabaseApi? FindDatabaseApi(string key)
{
return _parent.FindDatabaseApi(key);
}
@ -105,7 +105,7 @@ internal class ChildUnitOfWork : IUnitOfWork
return _parent.GetOrAddDatabaseApi(key, factory);
}
public ITransactionApi FindTransactionApi(string key)
public ITransactionApi? FindTransactionApi(string key)
{
return _parent.FindTransactionApi(key);
}

@ -2,5 +2,5 @@
public interface IAmbientUnitOfWork : IUnitOfWorkAccessor
{
IUnitOfWork GetCurrentByChecking();
IUnitOfWork? GetCurrentByChecking();
}

@ -6,8 +6,7 @@ namespace Volo.Abp.Uow;
public interface IDatabaseApiContainer : IServiceProviderAccessor
{
[CanBeNull]
IDatabaseApi FindDatabaseApi([NotNull] string key);
IDatabaseApi? FindDatabaseApi([NotNull] string key);
void AddDatabaseApi([NotNull] string key, [NotNull] IDatabaseApi api);

@ -5,8 +5,7 @@ namespace Volo.Abp.Uow;
public interface ITransactionApiContainer
{
[CanBeNull]
ITransactionApi FindTransactionApi([NotNull] string key);
ITransactionApi? FindTransactionApi([NotNull] string key);
void AddTransactionApi([NotNull] string key, [NotNull] ITransactionApi api);

@ -17,9 +17,9 @@ public interface IUnitOfWork : IDatabaseApiContainer, ITransactionApiContainer,
event EventHandler<UnitOfWorkEventArgs> Disposed;
IAbpUnitOfWorkOptions Options { get; }
IAbpUnitOfWorkOptions? Options { get; }
IUnitOfWork Outer { get; }
IUnitOfWork? Outer { get; }
bool IsReserved { get; }
@ -27,9 +27,9 @@ public interface IUnitOfWork : IDatabaseApiContainer, ITransactionApiContainer,
bool IsCompleted { get; }
string ReservationName { get; }
string? ReservationName { get; }
void SetOuter([CanBeNull] IUnitOfWork outer);
void SetOuter(IUnitOfWork? outer);
void Initialize([NotNull] AbpUnitOfWorkOptions options);
@ -45,11 +45,11 @@ public interface IUnitOfWork : IDatabaseApiContainer, ITransactionApiContainer,
void AddOrReplaceLocalEvent(
UnitOfWorkEventRecord eventRecord,
Predicate<UnitOfWorkEventRecord> replacementSelector = null
Predicate<UnitOfWorkEventRecord>? replacementSelector = null
);
void AddOrReplaceDistributedEvent(
UnitOfWorkEventRecord eventRecord,
Predicate<UnitOfWorkEventRecord> replacementSelector = null
Predicate<UnitOfWorkEventRecord>? replacementSelector = null
);
}

@ -4,8 +4,7 @@ namespace Volo.Abp.Uow;
public interface IUnitOfWorkAccessor
{
[CanBeNull]
IUnitOfWork UnitOfWork { get; }
IUnitOfWork? UnitOfWork { get; }
void SetUnitOfWork([CanBeNull] IUnitOfWork unitOfWork);
void SetUnitOfWork(IUnitOfWork? unitOfWork);
}

@ -4,8 +4,7 @@ namespace Volo.Abp.Uow;
public interface IUnitOfWorkManager
{
[CanBeNull]
IUnitOfWork Current { get; }
IUnitOfWork? Current { get; }
[NotNull]
IUnitOfWork Begin([NotNull] AbpUnitOfWorkOptions options, bool requiresNew = false);

@ -21,9 +21,9 @@ public class UnitOfWork : IUnitOfWork, ITransientDependency
public Guid Id { get; } = Guid.NewGuid();
public IAbpUnitOfWorkOptions Options { get; private set; }
public IAbpUnitOfWorkOptions? Options { get; private set; }
public IUnitOfWork Outer { get; private set; }
public IUnitOfWork? Outer { get; private set; }
public bool IsReserved { get; set; }
@ -31,14 +31,14 @@ public class UnitOfWork : IUnitOfWork, ITransientDependency
public bool IsCompleted { get; private set; }
public string ReservationName { get; set; }
public string? ReservationName { get; set; }
protected List<Func<Task>> CompletedHandlers { get; } = new List<Func<Task>>();
protected List<UnitOfWorkEventRecord> DistributedEvents { get; } = new List<UnitOfWorkEventRecord>();
protected List<UnitOfWorkEventRecord> LocalEvents { get; } = new List<UnitOfWorkEventRecord>();
public event EventHandler<UnitOfWorkFailedEventArgs> Failed;
public event EventHandler<UnitOfWorkEventArgs> Disposed;
public event EventHandler<UnitOfWorkFailedEventArgs> Failed = default!;
public event EventHandler<UnitOfWorkEventArgs> Disposed = default!;
public IServiceProvider ServiceProvider { get; }
protected IUnitOfWorkEventPublisher UnitOfWorkEventPublisher { get; }
@ -50,7 +50,7 @@ public class UnitOfWork : IUnitOfWork, ITransientDependency
private readonly Dictionary<string, ITransactionApi> _transactionApis;
private readonly AbpUnitOfWorkDefaultOptions _defaultOptions;
private Exception _exception;
private Exception? _exception;
private bool _isCompleting;
private bool _isRolledback;
@ -90,7 +90,7 @@ public class UnitOfWork : IUnitOfWork, ITransientDependency
IsReserved = true;
}
public virtual void SetOuter(IUnitOfWork outer)
public virtual void SetOuter(IUnitOfWork? outer)
{
Outer = outer;
}
@ -104,9 +104,9 @@ public class UnitOfWork : IUnitOfWork, ITransientDependency
foreach (var databaseApi in GetAllActiveDatabaseApis())
{
if (databaseApi is ISupportsSavingChanges)
if (databaseApi is ISupportsSavingChanges supportsSavingChangesDatabaseApi)
{
await (databaseApi as ISupportsSavingChanges).SaveChangesAsync(cancellationToken);
await supportsSavingChangesDatabaseApi.SaveChangesAsync(cancellationToken);
}
}
}
@ -181,7 +181,7 @@ public class UnitOfWork : IUnitOfWork, ITransientDependency
await RollbackAllAsync(cancellationToken);
}
public virtual IDatabaseApi FindDatabaseApi(string key)
public virtual IDatabaseApi? FindDatabaseApi(string key)
{
return _databaseApis.GetOrDefault(key);
}
@ -207,7 +207,7 @@ public class UnitOfWork : IUnitOfWork, ITransientDependency
return _databaseApis.GetOrAdd(key, factory);
}
public virtual ITransactionApi FindTransactionApi(string key)
public virtual ITransactionApi? FindTransactionApi(string key)
{
Check.NotNull(key, nameof(key));
@ -242,14 +242,14 @@ public class UnitOfWork : IUnitOfWork, ITransientDependency
public virtual void AddOrReplaceLocalEvent(
UnitOfWorkEventRecord eventRecord,
Predicate<UnitOfWorkEventRecord> replacementSelector = null)
Predicate<UnitOfWorkEventRecord>? replacementSelector = null)
{
AddOrReplaceEvent(LocalEvents, eventRecord, replacementSelector);
}
public virtual void AddOrReplaceDistributedEvent(
UnitOfWorkEventRecord eventRecord,
Predicate<UnitOfWorkEventRecord> replacementSelector = null)
Predicate<UnitOfWorkEventRecord>? replacementSelector = null)
{
AddOrReplaceEvent(DistributedEvents, eventRecord, replacementSelector);
}
@ -257,7 +257,7 @@ public class UnitOfWork : IUnitOfWork, ITransientDependency
public virtual void AddOrReplaceEvent(
List<UnitOfWorkEventRecord> eventRecords,
UnitOfWorkEventRecord eventRecord,
Predicate<UnitOfWorkEventRecord> replacementSelector = null)
Predicate<UnitOfWorkEventRecord>? replacementSelector = null)
{
if (replacementSelector == null)
{
@ -340,11 +340,11 @@ public class UnitOfWork : IUnitOfWork, ITransientDependency
{
foreach (var databaseApi in GetAllActiveDatabaseApis())
{
if (databaseApi is ISupportsRollback)
if (databaseApi is ISupportsRollback supportsRollbackDatabaseApi)
{
try
{
await (databaseApi as ISupportsRollback).RollbackAsync(cancellationToken);
await supportsRollbackDatabaseApi.RollbackAsync(cancellationToken);
}
catch { }
}
@ -352,11 +352,11 @@ public class UnitOfWork : IUnitOfWork, ITransientDependency
foreach (var transactionApi in GetAllActiveTransactionApis())
{
if (transactionApi is ISupportsRollback)
if (transactionApi is ISupportsRollback supportsRollbackTransactionApi)
{
try
{
await (transactionApi as ISupportsRollback).RollbackAsync(cancellationToken);
await supportsRollbackTransactionApi.RollbackAsync(cancellationToken);
}
catch { }
}

@ -13,8 +13,7 @@ public class UnitOfWorkFailedEventArgs : UnitOfWorkEventArgs
/// Can be null if there is no exception, but <see cref="IUnitOfWork.CompleteAsync"/> is not called.
/// Can be null if another exception occurred during the UOW.
/// </summary>
[CanBeNull]
public Exception Exception { get; }
public Exception? Exception { get; }
/// <summary>
/// True, if the unit of work is manually rolled back.
@ -24,7 +23,7 @@ public class UnitOfWorkFailedEventArgs : UnitOfWorkEventArgs
/// <summary>
/// Creates a new <see cref="UnitOfWorkFailedEventArgs"/> object.
/// </summary>
public UnitOfWorkFailedEventArgs([NotNull] IUnitOfWork unitOfWork, [CanBeNull] Exception exception, bool isRolledback)
public UnitOfWorkFailedEventArgs([NotNull] IUnitOfWork unitOfWork, Exception? exception, bool isRolledback)
: base(unitOfWork)
{
Exception = exception;

@ -23,7 +23,7 @@ public static class UnitOfWorkHelper
return false;
}
public static bool IsUnitOfWorkMethod([NotNull] MethodInfo methodInfo, [CanBeNull] out UnitOfWorkAttribute unitOfWorkAttribute)
public static bool IsUnitOfWorkMethod([NotNull] MethodInfo methodInfo, out UnitOfWorkAttribute? unitOfWorkAttribute)
{
Check.NotNull(methodInfo, nameof(methodInfo));
@ -57,7 +57,7 @@ public static class UnitOfWorkHelper
return false;
}
public static UnitOfWorkAttribute GetUnitOfWorkAttributeOrNull(MethodInfo methodInfo)
public static UnitOfWorkAttribute? GetUnitOfWorkAttributeOrNull(MethodInfo methodInfo)
{
var attrs = methodInfo.GetCustomAttributes(true).OfType<UnitOfWorkAttribute>().ToArray();
if (attrs.Length > 0)
@ -65,12 +65,15 @@ public static class UnitOfWorkHelper
return attrs[0];
}
attrs = methodInfo.DeclaringType.GetTypeInfo().GetCustomAttributes(true).OfType<UnitOfWorkAttribute>().ToArray();
if (attrs.Length > 0)
if (methodInfo.DeclaringType != null)
{
return attrs[0];
attrs = methodInfo.DeclaringType.GetTypeInfo().GetCustomAttributes(true).OfType<UnitOfWorkAttribute>().ToArray();
if (attrs.Length > 0)
{
return attrs[0];
}
}
return null;
}

@ -52,7 +52,7 @@ public class UnitOfWorkInterceptor : AbpInterceptor, ITransientDependency
}
}
private AbpUnitOfWorkOptions CreateOptions(IServiceProvider serviceProvider, IAbpMethodInvocation invocation, [CanBeNull] UnitOfWorkAttribute unitOfWorkAttribute)
private AbpUnitOfWorkOptions CreateOptions(IServiceProvider serviceProvider, IAbpMethodInvocation invocation, UnitOfWorkAttribute? unitOfWorkAttribute)
{
var options = new AbpUnitOfWorkOptions();

@ -10,7 +10,7 @@ public class UnitOfWorkManager : IUnitOfWorkManager, ISingletonDependency
[Obsolete("This will be removed in next versions.")]
public static AsyncLocal<bool> DisableObsoleteDbContextCreationWarning { get; } = new AsyncLocal<bool>();
public IUnitOfWork Current => _ambientUnitOfWork.GetCurrentByChecking();
public IUnitOfWork? Current => _ambientUnitOfWork.GetCurrentByChecking();
private readonly IServiceScopeFactory _serviceScopeFactory;
private readonly IAmbientUnitOfWork _ambientUnitOfWork;

Loading…
Cancel
Save