Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bump to .NET 8 #22

Merged
merged 1 commit into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions EntityRepository/EntityRepository.csproj
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<PackageId>Sidekick.EntityRepository</PackageId>
<Version>3.1.0</Version>
<Version>4.0.0</Version>
<Authors>Gustavo da Silva Oliveira</Authors>
<Company>sogustavo</Company>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
Expand All @@ -15,10 +15,18 @@
<PackageLicenseFile></PackageLicenseFile>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<RepositoryType>git</RepositoryType>
<PackageReadmeFile>README.md</PackageReadmeFile>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.14" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.1" />
</ItemGroup>

<ItemGroup>
<None Update="README.md">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion EntityRepository/Interfaces.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

Task<T> EntityAsync(Expression<Func<T, bool>> predicate = null, params string[] includes);

List<T> Entities(Expression<Func<T, bool>> predicate = null, params string[] includes);
IEnumerable<T> Entities(Expression<Func<T, bool>> predicate = null, params string[] includes);

Task<List<T>> EntitiesAsync(Expression<Func<T, bool>> predicate = null, params string[] includes);

Expand All @@ -29,9 +29,9 @@

public interface IRecord<T> where T : class
{
bool Add(T entity);

Check warning on line 32 in EntityRepository/Interfaces.cs

View workflow job for this annotation

GitHub Actions / Build and scan

All 'Add' method overloads should be adjacent. (https://rules.sonarsource.com/csharp/RSPEC-4136)

Check warning on line 32 in EntityRepository/Interfaces.cs

View workflow job for this annotation

GitHub Actions / Build and scan

All 'Add' method overloads should be adjacent. (https://rules.sonarsource.com/csharp/RSPEC-4136)

Task<bool> AddAsync(T entity, CancellationToken cancellationToken = default);

Check warning on line 34 in EntityRepository/Interfaces.cs

View workflow job for this annotation

GitHub Actions / Build and scan

All 'AddAsync' method overloads should be adjacent. (https://rules.sonarsource.com/csharp/RSPEC-4136)

bool Add(IEnumerable<T> entities);

Expand Down
45 changes: 19 additions & 26 deletions EntityRepository/Repository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,17 @@

namespace GenericRepository
{
public class Repository<T> : IRepository<T> where T : class
public class Repository<T>(DbContext context) : IRepository<T> where T : class
{
public readonly DbContext Context;
public readonly DbContext Context = context;

private readonly DbSet<T> _table;

public Repository(DbContext context)
{
Context = context;

_table = context.Set<T>();
}
private readonly DbSet<T> _table = context.Set<T>();

public virtual T Entity(Expression<Func<T, bool>> predicate = null, params string[] includes)
{
IQueryable<T> query = Queryable(includes);

if (predicate != null)
if (predicate is not null)
{
return query.FirstOrDefault(predicate);
}
Expand All @@ -37,60 +30,60 @@
{
IQueryable<T> query = Queryable(includes);

if (predicate != null)
if (predicate is not null)
{
return await query.FirstOrDefaultAsync(predicate);
}

return await query.FirstOrDefaultAsync();
}

public virtual List<T> Entities(Expression<Func<T, bool>> predicate = null, params string[] includes)
public virtual IEnumerable<T> Entities(Expression<Func<T, bool>> predicate = null, params string[] includes)
{
IQueryable<T> query = Queryable(includes);

if (predicate != null)
if (predicate is not null)
{
return query.Where(predicate).ToList();
}

return query.ToList();
}

public virtual async Task<List<T>> EntitiesAsync(Expression<Func<T, bool>> predicate = null, params string[] includes)
public virtual Task<List<T>> EntitiesAsync(Expression<Func<T, bool>> predicate = null, params string[] includes)
{
IQueryable<T> query = Queryable(includes);

if (predicate != null)
if (predicate is not null)
{
return query.Where(predicate).ToList();
return query.Where(predicate).ToListAsync();
}

return await query.ToListAsync();
return query.ToListAsync();
}

public virtual int Count(Expression<Func<T, bool>> predicate = null, params string[] includes)
{
IQueryable<T> query = Queryable(includes);

if (predicate != null)
if (predicate is not null)
{
return query.Count(predicate);
}

return query.Count();
}

public virtual async Task<int> CountAsync(Expression<Func<T, bool>> predicate = null, params string[] includes)
public virtual Task<int> CountAsync(Expression<Func<T, bool>> predicate = null, params string[] includes)
{
IQueryable<T> query = Queryable(includes);

if (predicate != null)
if (predicate is not null)
{
return await query.CountAsync(predicate);
return query.CountAsync(predicate);
}

return await query.CountAsync();
return query.CountAsync();
}

public virtual decimal Sum(Expression<Func<T, decimal>> selector, params string[] includes)
Expand All @@ -100,14 +93,14 @@
return query.Sum(selector);
}

public virtual async Task<decimal> SumAsync(Expression<Func<T, decimal>> selector = null, params string[] includes)
public virtual Task<decimal> SumAsync(Expression<Func<T, decimal>> selector = null, params string[] includes)

Check warning on line 96 in EntityRepository/Repository.cs

View workflow job for this annotation

GitHub Actions / Build and scan

Remove the default parameter value to match the signature of overridden method. (https://rules.sonarsource.com/csharp/RSPEC-1006)
{
IQueryable<T> query = Queryable(includes);

return await query.SumAsync(selector);
return query.SumAsync(selector);
}

public virtual bool Add(T entity)

Check warning on line 103 in EntityRepository/Repository.cs

View workflow job for this annotation

GitHub Actions / Build and scan

All 'Add' method overloads should be adjacent. (https://rules.sonarsource.com/csharp/RSPEC-4136)
{
Context.Set<T>().Add(entity);

Expand All @@ -116,7 +109,7 @@
return true;
}

public virtual async Task<bool> AddAsync(T entity, CancellationToken cancellationToken = default)

Check warning on line 112 in EntityRepository/Repository.cs

View workflow job for this annotation

GitHub Actions / Build and scan

All 'AddAsync' method overloads should be adjacent. (https://rules.sonarsource.com/csharp/RSPEC-4136)
{
await Context.Set<T>().AddAsync(entity, cancellationToken);

Expand Down Expand Up @@ -183,7 +176,7 @@
{
IQueryable<T> query = _table.AsQueryable();

if (includes != null)
if (includes is not null)
{
for (int i = 0; i < includes.Length; i++)
{
Expand Down
2 changes: 1 addition & 1 deletion ExtensionMethods/EnumerationExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static string Description(this Enum enumeration)
}
catch (Exception)
{
return enumeration.ToString();
return nameof(enumeration);
}
}
}
Expand Down
13 changes: 11 additions & 2 deletions ExtensionMethods/ExtensionMethods.csproj
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<PackageId>Sidekick.ExtensionMethods</PackageId>
<Version>3.2.0</Version>
<Version>4.0.0</Version>
<Authors>Gustavo da Silva Oliveira</Authors>
<Company>sogustavo</Company>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
Expand All @@ -14,11 +14,20 @@
<PackageProjectUrl>https://github.com/sogustavo/sidekick</PackageProjectUrl>
<PackageTags>extension-methods extensionmethods</PackageTags>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageReadmeFile>README.md</PackageReadmeFile>
<RepositoryType>git</RepositoryType>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.2.0" />
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
</ItemGroup>

<ItemGroup>
<None Update="README.md">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
</ItemGroup>

</Project>
58 changes: 26 additions & 32 deletions ExtensionMethods/StringExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,26 +42,24 @@ public static bool IsCNPJ(this string text)
return false;
}

int[] multiplier1 = new int[12] { 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2 };
int[] multiplier1 = [5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2];

int[] multiplier2 = new int[13] { 6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2 };

int sum = 0, rest;

string digit, CNPJ;
int[] multiplier2 = [6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2];

text = text.Trim();

text = text.Remove(".", "-", "/");

CNPJ = text.Substring(0, 12);
var cnpj = text[..12];

var sum = 0;

for (int i = 0; i < 12; i++)
{
sum += int.Parse(CNPJ[i].ToString()) * multiplier1[i];
sum += int.Parse(cnpj[i].ToString()) * multiplier1[i];
}

rest = sum % 11;
var rest = sum % 11;

if (rest < 2)
{
Expand All @@ -72,15 +70,15 @@ public static bool IsCNPJ(this string text)
rest = 11 - rest;
}

digit = rest.ToString();
var digit = rest.ToString();

CNPJ = CNPJ + digit;
cnpj += digit;

sum = 0;

for (int i = 0; i < 13; i++)
{
sum += int.Parse(CNPJ[i].ToString()) * multiplier2[i];
sum += int.Parse(cnpj[i].ToString()) * multiplier2[i];
}

rest = (sum % 11);
Expand All @@ -94,7 +92,7 @@ public static bool IsCNPJ(this string text)
rest = 11 - rest;
}

digit = digit + rest.ToString();
digit += rest.ToString();

return text.EndsWith(digit);
}
Expand All @@ -106,26 +104,24 @@ public static bool IsCPF(this string text)
return false;
}

int[] multiplier1 = new int[9] { 10, 9, 8, 7, 6, 5, 4, 3, 2 };

int[] multiplier2 = new int[10] { 11, 10, 9, 8, 7, 6, 5, 4, 3, 2 };

string CPF, digit;
int[] multiplier1 = [10, 9, 8, 7, 6, 5, 4, 3, 2];

int sum = 0, rest;
int[] multiplier2 = [11, 10, 9, 8, 7, 6, 5, 4, 3, 2];

text = text.Trim();

text = text.Remove(".", "-");

CPF = text.Substring(0, 9);
var cpf = text[..9];

var sum = 0;

for (int i = 0; i < 9; i++)
{
sum += int.Parse(CPF[i].ToString()) * multiplier1[i];
sum += int.Parse(cpf[i].ToString()) * multiplier1[i];
}

rest = sum % 11;
var rest = sum % 11;

if (rest < 2)
{
Expand All @@ -136,15 +132,15 @@ public static bool IsCPF(this string text)
rest = 11 - rest;
}

digit = rest.ToString();
var digit = rest.ToString();

CPF = CPF + digit;
cpf += digit;

sum = 0;

for (int i = 0; i < 10; i++)
{
sum += int.Parse(CPF[i].ToString()) * multiplier2[i];
sum += int.Parse(cpf[i].ToString()) * multiplier2[i];
}

rest = sum % 11;
Expand All @@ -158,7 +154,7 @@ public static bool IsCPF(this string text)
rest = 11 - rest;
}

digit = digit + rest.ToString();
digit += rest.ToString();

return text.EndsWith(digit);
}
Expand All @@ -170,22 +166,20 @@ public static bool IsPIS(this string text)
return false;
}

int[] multiplier = new int[10] { 3, 2, 9, 8, 7, 6, 5, 4, 3, 2 };

int sum = 0, rest;
int[] multiplier = [3, 2, 9, 8, 7, 6, 5, 4, 3, 2];

text = text.Trim();

text = text.Remove("-", ".").PadLeft(11, '0');

sum = 0;
var sum = 0;

for (int i = 0; i < 10; i++)
{
sum += int.Parse(text[i].ToString()) * multiplier[i];
}

rest = sum % 11;
var rest = sum % 11;

if (rest < 2)
{
Expand All @@ -201,7 +195,7 @@ public static bool IsPIS(this string text)

public static string AssemblyPath(this Assembly assembly)
{
return new Uri(assembly.CodeBase).LocalPath;
return new Uri(assembly.Location).LocalPath;
}

public static string Extension(this string filename)
Expand Down
6 changes: 3 additions & 3 deletions ExtensionMethods/TryParseExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
{
public static T? TryParse<T>(this object obj) where T : struct
{
if (obj == null)
if (obj is null)
{
return null;
}
Expand All @@ -16,15 +16,15 @@

TypeConverter tc = TypeDescriptor.GetConverter(typeof(T));

if (tc != null)
if (tc is not null)
{
try
{
string value = obj.ToString();
var value = obj.ToString();

result = (T)tc.ConvertFromString(value);
}
catch (Exception) { }

Check warning on line 27 in ExtensionMethods/TryParseExtension.cs

View workflow job for this annotation

GitHub Actions / Build and scan

Handle the exception or explain in a comment why it can be ignored. (https://rules.sonarsource.com/csharp/RSPEC-2486)

Check warning on line 27 in ExtensionMethods/TryParseExtension.cs

View workflow job for this annotation

GitHub Actions / Build and scan

Either remove or fill this block of code. (https://rules.sonarsource.com/csharp/RSPEC-108)

Check warning on line 27 in ExtensionMethods/TryParseExtension.cs

View workflow job for this annotation

GitHub Actions / Build and scan

Handle the exception or explain in a comment why it can be ignored. (https://rules.sonarsource.com/csharp/RSPEC-2486)

Check warning on line 27 in ExtensionMethods/TryParseExtension.cs

View workflow job for this annotation

GitHub Actions / Build and scan

Either remove or fill this block of code. (https://rules.sonarsource.com/csharp/RSPEC-108)
}

return result;
Expand Down
Loading
Loading