Skip to content

Commit

Permalink
Updated langversion, enabled nullable, updated packages
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel127 committed Mar 17, 2021
1 parent 7a73570 commit 19686ce
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 79 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Runtime.Serialization;
using System.Security.Permissions;

namespace QD.EntityFrameworkCore.UnitOfWork.Abstractions.Collections
{
Expand Down Expand Up @@ -37,7 +36,6 @@ private PageNotFoundException(SerializationInfo info, StreamingContext context)
}

/// <inheritdoc />
[SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
base.GetObjectData(info, context);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using QD.EntityFrameworkCore.UnitOfWork.Abstractions.Collections;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Linq.Expressions;
using System.Threading;
Expand Down Expand Up @@ -33,7 +32,7 @@ public interface IReadOnlyRepository<TEntity> where TEntity : class
/// <param name="orderBy">A function to order elements.</param>
/// <param name="disableTracking"><c>true</c> to disable changing tracking; otherwise, <c>false</c>. Default to <c>true</c>.</param>
/// <remarks>This method defaults to a read-only, no-tracking query.</remarks>
TEntity GetFirstOrDefault(Expression<Func<TEntity, bool>>? predicate = null, Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>>? orderBy = null, bool disableTracking = true);
TEntity? GetFirstOrDefault(Expression<Func<TEntity, bool>>? predicate = null, Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>>? orderBy = null, bool disableTracking = true);

/// <summary>
/// Gets the first or default entity based on a predicate, order by delegate and include delegate.
Expand All @@ -43,7 +42,7 @@ public interface IReadOnlyRepository<TEntity> where TEntity : class
/// <param name="orderBy">A function to order elements.</param>
/// <param name="disableTracking"><c>true</c> to disable changing tracking; otherwise, <c>false</c>. Default to <c>true</c>.</param>
/// <remarks>This method defaults to a read-only, no-tracking query.</remarks>
TResult GetFirstOrDefault<TResult>(Expression<Func<TEntity, TResult>> selector, Expression<Func<TEntity, bool>>? predicate = null, Func<IQueryable<TResult>, IOrderedQueryable<TResult>>? orderBy = null, bool disableTracking = true) where TResult : class;
TResult? GetFirstOrDefault<TResult>(Expression<Func<TEntity, TResult>> selector, Expression<Func<TEntity, bool>>? predicate = null, Func<IQueryable<TResult>, IOrderedQueryable<TResult>>? orderBy = null, bool disableTracking = true) where TResult : class;

/// <summary>
/// Gets the first or default entity based on a predicate, order by delegate and include delegate.
Expand Down Expand Up @@ -84,22 +83,22 @@ public interface IReadOnlyRepository<TEntity> where TEntity : class
/// </summary>
/// <param name="keyValues">The values of the primary key for the entity to be found.</param>
/// <returns>The found entity or null.</returns>
TEntity Find([MaybeNull] params object[] keyValues);
TEntity Find(params object[] keyValues);

/// <summary>
/// Finds an entity with the given primary key values. If found, is attached to the context and returned. If no entity is found, then null is returned.
/// </summary>
/// <param name="keyValues">The values of the primary key for the entity to be found.</param>
/// <returns>A <see cref="Task{TEntity}"/> that represents the asynchronous find operation. The task result contains the found entity or null.</returns>
ValueTask<TEntity> FindAsync([MaybeNull] params object[] keyValues);
ValueTask<TEntity> FindAsync(params object[] keyValues);

/// <summary>
/// Finds an entity with the given primary key values. If found, is attached to the context and returned. If no entity is found, then null is returned.
/// </summary>
/// <param name="keyValues">The values of the primary key for the entity to be found.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe while waiting for the task to complete.</param>
/// <returns>A <see cref="Task{TEntity}"/> that represents the asynchronous find operation. The task result contains the found entity or null.</returns>
ValueTask<TEntity> FindAsync([MaybeNull] object[] keyValues, CancellationToken cancellationToken);
ValueTask<TEntity> FindAsync(object[] keyValues, CancellationToken cancellationToken);
#endregion

#region Other
Expand Down
21 changes: 10 additions & 11 deletions QD.EntityFrameworkCore.UnitOfWork.Abstractions/IRepository.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using System.Threading.Tasks;

Expand All @@ -16,69 +15,69 @@ public interface IRepository<TEntity> : IReadOnlyRepository<TEntity> where TEnti
/// Inserts a new entity synchronously.
/// </summary>
/// <param name="entity">The entity to insert.</param>
void Insert([NotNull] TEntity entity);
void Insert(TEntity entity);

/// <summary>
/// Inserts a range of entities synchronously.
/// </summary>
/// <param name="entities">The entities to insert.</param>
void Insert([NotNull] IEnumerable<TEntity> entities);
void Insert(IEnumerable<TEntity> entities);

/// <summary>
/// Inserts a new entity asynchronously.
/// </summary>
/// <param name="entity">The entity to insert.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe while waiting for the task to complete.</param>
/// <returns>A <see cref="Task"/> that represents the asynchronous insert operation.</returns>
ValueTask<TEntity> InsertAsync([NotNull] TEntity entity, CancellationToken cancellationToken = default);
ValueTask<TEntity> InsertAsync(TEntity entity, CancellationToken cancellationToken = default);

/// <summary>
/// Inserts a range of entities asynchronously.
/// </summary>
/// <param name="entities">The entities to insert.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> to observe while waiting for the task to complete.</param>
/// <returns>A <see cref="Task"/> that represents the asynchronous insert operation.</returns>
Task InsertAsync([NotNull] IEnumerable<TEntity> entities, CancellationToken cancellationToken = default);
Task InsertAsync(IEnumerable<TEntity> entities, CancellationToken cancellationToken = default);
#endregion

#region Update
/// <summary>
/// Updates the specified entity.
/// </summary>
/// <param name="entity">The entity.</param>
void Update([NotNull] TEntity entity);
void Update(TEntity entity);

/// <summary>
/// Updates the specified entities.
/// </summary>
/// <param name="entities">The entities.</param>
void Update([NotNull] params TEntity[] entities);
void Update(params TEntity[] entities);

/// <summary>
/// Updates the specified entities.
/// </summary>
/// <param name="entities">The entities.</param>
void Update([NotNull] IEnumerable<TEntity> entities);
void Update(IEnumerable<TEntity> entities);
#endregion

#region Delete
/// <summary>
/// Deletes the entity by the specified primary key.
/// </summary>
/// <param name="keyValues">The primary key value.</param>
void Delete([MaybeNull] params object[] keyValues);
void Delete(params object[] keyValues);

/// <summary>
/// Deletes the specified entity.
/// </summary>
/// <param name="entity">The entity to delete.</param>
void Delete([NotNull] TEntity entity);
void Delete(TEntity entity);

/// <summary>
/// Deletes the specified entities.
/// </summary>
/// <param name="entities">The entities.</param>
void Delete([NotNull] IEnumerable<TEntity> entities);
void Delete(IEnumerable<TEntity> entities);
#endregion
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<LangVersion>8.0</LangVersion>
<LangVersion>9.0</LangVersion>
<Nullable>enable</Nullable>

<PackageLicenseFile>LICENSE.md</PackageLicenseFile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@
using QD.EntityFrameworkCore.UnitOfWork.UnitTests.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Threading.Tasks;
using Xunit;

Expand Down Expand Up @@ -109,17 +106,10 @@ protected virtual void Dispose(bool disposing)
getPagedDictionary.Should().ThrowExactly<TException>();
getPagedDictionaryAsync.Should().ThrowExactly<TException>();

if (exception is PageNotFoundException pageNotFoundException)
{
int totalPages = (int)Math.Ceiling((double)Math.DivRem(ProductsNumber, pageSize, out _));
pageNotFoundException.TotalPages.Should().Be(totalPages);
pageNotFoundException.PageNumber.Should().Be(pageNumber);

var clone = SerializationClone<PageNotFoundException>(pageNotFoundException);

clone.TotalPages.Should().Be(pageNotFoundException.TotalPages);
clone.PageNumber.Should().Be(pageNotFoundException.PageNumber);
}
if (exception is not PageNotFoundException pageNotFoundException) return;
int totalPages = (int)Math.Ceiling((double)Math.DivRem(ProductsNumber, pageSize, out _));
pageNotFoundException.TotalPages.Should().Be(totalPages);
pageNotFoundException.PageNumber.Should().Be(pageNumber);
}

#region Paged Array
Expand Down Expand Up @@ -385,29 +375,5 @@ public void Experiment()
};
pages.Should().NotBeNull();
}

#region Utils

private static Stream Serialize(object source)
{
IFormatter formatter = new BinaryFormatter();
Stream stream = new MemoryStream();
formatter.Serialize(stream, source);
return stream;
}

private static T Deserialize<T>(Stream stream)
{
IFormatter formatter = new BinaryFormatter();
stream.Position = 0;
return (T)formatter.Deserialize(stream);
}

private static T SerializationClone<T>(object source)
{
return Deserialize<T>(Serialize(source));
}

#endregion
}
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<LangVersion>8.0</LangVersion>
<TargetFramework>net5.0</TargetFramework>
<LangVersion>9.0</LangVersion>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="5.10.3" />
<PackageReference Include="FluentAssertions.Analyzers" Version="0.11.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.1.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="5.0.4" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="1.2.0" />
<PackageReference Include="coverlet.msbuild" Version="2.8.0">
<PackageReference Include="coverlet.collector" Version="3.0.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="coverlet.msbuild" Version="3.0.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
6 changes: 3 additions & 3 deletions QD.EntityFrameworkCore.UnitOfWork.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29920.165
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QD.EntityFrameworkCore.UnitOfWork", "QD.EntityFrameworkCore.UnitOfWork\QD.EntityFrameworkCore.UnitOfWork.csproj", "{3F536368-2233-41BD-9988-2BDA9605BC1A}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "QD.EntityFrameworkCore.UnitOfWork", "QD.EntityFrameworkCore.UnitOfWork\QD.EntityFrameworkCore.UnitOfWork.csproj", "{3F536368-2233-41BD-9988-2BDA9605BC1A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QD.EntityFrameworkCore.UnitOfWork.Abstractions", "QD.EntityFrameworkCore.UnitOfWork.Abstractions\QD.EntityFrameworkCore.UnitOfWork.Abstractions.csproj", "{72FA6610-4D2F-4397-958E-B9CC6F5DFE92}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "QD.EntityFrameworkCore.UnitOfWork.Abstractions", "QD.EntityFrameworkCore.UnitOfWork.Abstractions\QD.EntityFrameworkCore.UnitOfWork.Abstractions.csproj", "{72FA6610-4D2F-4397-958E-B9CC6F5DFE92}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{4B3D0D9B-2285-4D18-8A6C-308767161CAC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QD.EntityFrameworkCore.UnitOfWork.UnitTests", "QD.EntityFrameworkCore.UnitOfWork.UnitTests\QD.EntityFrameworkCore.UnitOfWork.UnitTests.csproj", "{2FA29983-1FD9-4E36-AAB0-BBF02142E485}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "QD.EntityFrameworkCore.UnitOfWork.UnitTests", "QD.EntityFrameworkCore.UnitOfWork.UnitTests\QD.EntityFrameworkCore.UnitOfWork.UnitTests.csproj", "{2FA29983-1FD9-4E36-AAB0-BBF02142E485}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<LangVersion>8.0</LangVersion>
<LangVersion>9.0</LangVersion>
<Nullable>enable</Nullable>

<PackageLicenseFile>LICENSE.md</PackageLicenseFile>
Expand All @@ -15,11 +15,11 @@
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<CodeAnalysisRuleSet>QD.EntityFrameworkCore.UnitOfWork-1.ruleset</CodeAnalysisRuleSet>
<CodeAnalysisRuleSet>QD.EntityFrameworkCore.UnitOfWork.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<CodeAnalysisRuleSet>QD.EntityFrameworkCore.UnitOfWork-1.ruleset</CodeAnalysisRuleSet>
<CodeAnalysisRuleSet>QD.EntityFrameworkCore.UnitOfWork.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>

<ItemGroup>
Expand All @@ -30,7 +30,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="3.1.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="5.0.4" />
</ItemGroup>

<ItemGroup>
Expand Down
6 changes: 3 additions & 3 deletions QD.EntityFrameworkCore.UnitOfWork/ReadOnlyRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public virtual IQueryable<TEntity> GetAll(Expression<Func<TEntity, bool>>? predi
}

/// <inheritdoc />
public virtual TEntity GetFirstOrDefault(Expression<Func<TEntity, bool>>? predicate = null, Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>>? orderBy = null, bool disableTracking = true)
public virtual TEntity? GetFirstOrDefault(Expression<Func<TEntity, bool>>? predicate = null, Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>>? orderBy = null, bool disableTracking = true)
{
IQueryable<TEntity> query = DbSet;
if (disableTracking)
Expand All @@ -59,7 +59,7 @@ public virtual TEntity GetFirstOrDefault(Expression<Func<TEntity, bool>>? predic
}

/// <inheritdoc />
public virtual TResult GetFirstOrDefault<TResult>(Expression<Func<TEntity, TResult>> selector, Expression<Func<TEntity, bool>>? predicate = null, Func<IQueryable<TResult>, IOrderedQueryable<TResult>>? orderBy = null, bool disableTracking = true) where TResult : class
public virtual TResult? GetFirstOrDefault<TResult>(Expression<Func<TEntity, TResult>> selector, Expression<Func<TEntity, bool>>? predicate = null, Func<IQueryable<TResult>, IOrderedQueryable<TResult>>? orderBy = null, bool disableTracking = true) where TResult : class
{
IQueryable<TEntity> query = DbSet;
if (disableTracking)
Expand Down Expand Up @@ -112,7 +112,7 @@ public virtual TEntity Find(params object[] keyValues)
}

/// <inheritdoc />
public virtual ValueTask<TEntity> FindAsync(params object[] keyValues)
public virtual ValueTask<TEntity> FindAsync([NotNull] params object[] keyValues)
{
return DbSet.FindAsync(keyValues);
}
Expand Down

0 comments on commit 19686ce

Please sign in to comment.