Skip to content

Commit

Permalink
change base class Entity to a interface IEntity
Browse files Browse the repository at this point in the history
  • Loading branch information
Barsonax committed May 9, 2024
1 parent 6ba8601 commit b027db4
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 119 deletions.
79 changes: 0 additions & 79 deletions CleanAspCore.Api.Tests/Data/EntityTests.cs

This file was deleted.

2 changes: 1 addition & 1 deletion CleanAspCore/Data/Extensions/DbSetExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace CleanAspCore.Data.Extensions;
public static class DbSetExtensions
{
public static EntityEntry<T>? AddIfNotExists<T>(this DbSet<T> dbSet, T entity)
where T : Entity
where T : class, IEntity
{
return !dbSet.Any(x => x.Id == entity.Id) ? dbSet.Add(entity) : null;
}
Expand Down
4 changes: 3 additions & 1 deletion CleanAspCore/Data/Models/Department.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
namespace CleanAspCore.Data.Models;

public class Department : Entity
public class Department : IEntity
{
public required Guid Id { get; init; }
public required string Name { get; init; }
public required string City { get; init; }

}
3 changes: 2 additions & 1 deletion CleanAspCore/Data/Models/Employee.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
namespace CleanAspCore.Data.Models;

public class Employee : Entity
public class Employee : IEntity
{
public required Guid Id { get; init; }
public required string FirstName { get; set; }
public required string LastName { get; set; }
public required EmailAddress Email { get; set; }
Expand Down
35 changes: 0 additions & 35 deletions CleanAspCore/Data/Models/Entity.cs

This file was deleted.

6 changes: 6 additions & 0 deletions CleanAspCore/Data/Models/IEntity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace CleanAspCore.Data.Models;

public interface IEntity
{
public Guid Id { get; init; }
}
3 changes: 2 additions & 1 deletion CleanAspCore/Data/Models/Job.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace CleanAspCore.Data.Models;

public class Job : Entity
public class Job : IEntity
{
public required Guid Id { get; init; }
public required string Name { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace CleanAspCore.Extensions.FluentValidation;
public static class FluentValidationExtensions
{
public static IRuleBuilderOptions<TModel, Guid> EntityShouldExist<TModel, TEntity>(this IRuleBuilder<TModel, Guid> rule, DbSet<TEntity> entities)
where TEntity : Entity =>
where TEntity : class, IEntity =>
rule.MustAsync((id, t) => entities.AnyAsync(y => y.Id == id, t))
.WithMessage((x, y) => $"{typeof(TEntity).Name} with id {y} does not exist.");

Expand Down

0 comments on commit b027db4

Please sign in to comment.