Skip to content

Commit

Permalink
Added generic repository key equality constraint. Added ThrowHelper.
Browse files Browse the repository at this point in the history
  • Loading branch information
Felix-CodingClimber committed Jan 16, 2024
1 parent 6777ed0 commit 8edfd03
Show file tree
Hide file tree
Showing 30 changed files with 271 additions and 136 deletions.
16 changes: 8 additions & 8 deletions samples/DotNetElements.CrudExample/Components/Pages/Crud.razor
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ else

if (createdTag.IsFail)
{
snackbar.Add($"Failed to create tag.\n{createdTag.Message}", Severity.Error);
snackbar.Add($"Failed to create tag.\n{createdTag.ErrorMessage}", Severity.Error);
return;
}

Expand Down Expand Up @@ -242,7 +242,7 @@ else

if (updatedTag.IsFail)
{
snackbar.Add($"Failed to update tag.\n{updatedTag.Message}", Severity.Error);
snackbar.Add($"Failed to update tag.\n{updatedTag.ErrorMessage}", Severity.Error);
return;
}

Expand All @@ -263,7 +263,7 @@ else

if(deleteResult.IsFail)
{
snackbar.Add($"Failed to delete tag.\n{deleteResult.Message}", Severity.Error);
snackbar.Add($"Failed to delete tag.\n{deleteResult.ErrorMessage}", Severity.Error);
return;
}

Expand All @@ -285,7 +285,7 @@ else

if(details.IsFail)
{
snackbar.Add($"Failed to fetch tag details.\n{details.Message}", Severity.Error);
snackbar.Add($"Failed to fetch tag details.\n{details.ErrorMessage}", Severity.Error);
return;
}

Expand Down Expand Up @@ -317,7 +317,7 @@ else

if (createdBlogPost.IsFail)
{
snackbar.Add($"Failed to create blog post.\n{createdBlogPost.Message}", Severity.Error);
snackbar.Add($"Failed to create blog post.\n{createdBlogPost.ErrorMessage}", Severity.Error);
return;
}

Expand Down Expand Up @@ -350,7 +350,7 @@ else

if (updatedBlogPost.IsFail)
{
snackbar.Add($"Failed to update blog post.\n{updatedBlogPost.Message}", Severity.Error);
snackbar.Add($"Failed to update blog post.\n{updatedBlogPost.ErrorMessage}", Severity.Error);
return;
}

Expand All @@ -370,7 +370,7 @@ else

if(deleteResult.IsFail)
{
snackbar.Add($"Failed to delete blog post.\n{deleteResult.Message}", Severity.Error);
snackbar.Add($"Failed to delete blog post.\n{deleteResult.ErrorMessage}", Severity.Error);
return;
}

Expand All @@ -391,7 +391,7 @@ else

if(details.IsFail)
{
snackbar.Add($"Failed to fetch blog post details.\n{details.Message}", Severity.Error);
snackbar.Add($"Failed to fetch blog post details.\n{details.ErrorMessage}", Severity.Error);
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/DotNetElements.Core/Core/Contracts.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace DotNetElements.Core;

public interface IHasKey<TKey>
where TKey : notnull
where TKey : notnull, IEquatable<TKey>
{
TKey Id { get; }

Expand Down
16 changes: 8 additions & 8 deletions src/DotNetElements.Core/Core/EntityBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
namespace DotNetElements.Core;

public interface IEntity<TKey> : IHasKey<TKey>
where TKey : notnull;
where TKey : notnull, IEquatable<TKey>;

public interface ICreationAuditedEntity<TKey> : IEntity<TKey>
where TKey : notnull
where TKey : notnull, IEquatable<TKey>
{
Guid CreatorId { get; }

Expand All @@ -16,7 +16,7 @@ public interface ICreationAuditedEntity<TKey> : IEntity<TKey>
}

public interface IAuditedEntity<TKey> : ICreationAuditedEntity<TKey>
where TKey : notnull
where TKey : notnull, IEquatable<TKey>
{
Guid? LastModifierId { get; }

Expand Down Expand Up @@ -55,21 +55,21 @@ public interface IUpdatable<TFrom>

public interface IRelatedEntity<TSelf, TKey>
where TSelf : IEntity<TKey>, IRelatedEntity<TSelf, TKey>
where TKey : notnull
where TKey : notnull, IEquatable<TKey>
{
static abstract TSelf CreateRefById(TKey id);
}

public abstract class Entity { }

public abstract class Entity<TKey> : Entity, IEntity<TKey>
where TKey : notnull
where TKey : notnull, IEquatable<TKey>
{
public TKey Id { get; protected set; } = default!;
}

public class CreationAuditedEntity<TKey> : Entity<TKey>, ICreationAuditedEntity<TKey>
where TKey : notnull
where TKey : notnull, IEquatable<TKey>
{
public Guid CreatorId { get; private set; }

Expand All @@ -86,7 +86,7 @@ public void SetCreationAudited(Guid creatorId, DateTimeOffset creationTime)
}

public class AuditedEntity<TKey> : CreationAuditedEntity<TKey>, IAuditedEntity<TKey>
where TKey : notnull
where TKey : notnull, IEquatable<TKey>
{
public Guid? LastModifierId { get; private set; }

Expand All @@ -100,7 +100,7 @@ public void SetModificationAudited(Guid lastModifierId, DateTimeOffset lastModif
}

public class PersistentEntity<TKey> : CreationAuditedEntity<TKey>, IDeletionAuditedEntity
where TKey : notnull
where TKey : notnull, IEquatable<TKey>
{
public bool IsDeleted { get; private set; }

Expand Down
2 changes: 1 addition & 1 deletion src/DotNetElements.Core/Core/EntityHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public static class EntityHelper
public static void UpdateRelatedEntities<TEntity, TModel, TKey>(List<TEntity> oldCollection, IEnumerable<TModel> newCollection, IAttachRelatedEntity attachRelatedEntity)
where TEntity : Entity<TKey>, IRelatedEntity<TEntity, TKey>
where TModel : Model<TKey>
where TKey : notnull
where TKey : notnull, IEquatable<TKey>
{
oldCollection.RemoveAll(existingTag => !newCollection.Any(newTag => newTag.Id.Equals(existingTag.Id)));
var addedModels = newCollection.Where(newTag => !oldCollection.Any(existingTag => existingTag.Id.Equals(newTag.Id)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static IServiceCollection RegisterModules(this IServiceCollection service

public static IServiceCollection AddManagedRepository<TManagedRepository, TRepository, TEntity, TKey>(this IServiceCollection services)
where TEntity : Entity<TKey>
where TKey : notnull
where TKey : notnull, IEquatable<TKey>
where TRepository : IRepository<TEntity, TKey>
where TManagedRepository : ManagedRepository<TRepository, TEntity, TKey>
{
Expand Down
2 changes: 1 addition & 1 deletion src/DotNetElements.Core/Core/IAttachRelatedEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ public interface IAttachRelatedEntity
{
TRelatedEntity AttachById<TRelatedEntity, TKey>(TKey id)
where TRelatedEntity : Entity<TKey>, IRelatedEntity<TRelatedEntity, TKey>
where TKey : notnull;
where TKey : notnull, IEquatable<TKey>;
}
2 changes: 1 addition & 1 deletion src/DotNetElements.Core/Core/IManagedRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

public interface IManagedRepository<TRepository, TEntity, TEditModel, TKey> : IRepository<TEntity, TKey>
where TEntity : Entity<TKey>
where TKey : notnull
where TKey : notnull, IEquatable<TKey>
where TRepository : IRepository<TEntity, TKey>
{ }
2 changes: 1 addition & 1 deletion src/DotNetElements.Core/Core/IReadOnlyRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace DotNetElements.Core;

public interface IReadOnlyRepository<TEntity, TKey>
where TEntity : Entity<TKey>
where TKey : notnull
where TKey : notnull, IEquatable<TKey>
{
Task<IReadOnlyList<TEntity>> GetAllAsync(CancellationToken cancellationToken = default);

Expand Down
2 changes: 1 addition & 1 deletion src/DotNetElements.Core/Core/IRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace DotNetElements.Core;

public interface IRepository<TEntity, TKey> : IReadOnlyRepository<TEntity, TKey>
where TEntity : Entity<TKey>
where TKey : notnull
where TKey : notnull, IEquatable<TKey>
{
Task ClearTable();

Expand Down
2 changes: 1 addition & 1 deletion src/DotNetElements.Core/Core/IScopedRepositoryFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

public interface IScopedRepositoryFactory<TRepository, TEntity, TKey>
where TEntity : Entity<TKey>
where TKey : notnull
where TKey : notnull, IEquatable<TKey>
where TRepository : IRepository<TEntity, TKey>
{
ScopedRepository<TRepository, TEntity, TKey> Create();
Expand Down
2 changes: 1 addition & 1 deletion src/DotNetElements.Core/Core/ManagedRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace DotNetElements.Core;

public abstract class ManagedRepository<TRepository, TEntity, TKey> : IRepository<TEntity, TKey>
where TEntity : Entity<TKey>
where TKey : notnull
where TKey : notnull, IEquatable<TKey>
where TRepository : IRepository<TEntity, TKey>
{
private readonly IScopedRepositoryFactory<TRepository, TEntity, TKey> repositoryFactory;
Expand Down
10 changes: 5 additions & 5 deletions src/DotNetElements.Core/Core/ModelBase.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
namespace DotNetElements.Core;

public interface IModel<TKey> : IHasKey<TKey>
where TKey : notnull;
where TKey : notnull, IEquatable<TKey>;

public abstract class Model<TKey> : IModel<TKey>
where TKey : notnull
where TKey : notnull, IEquatable<TKey>
{
public TKey Id { get; private init; }

Expand All @@ -15,7 +15,7 @@ protected Model(TKey id)
}

public abstract class VersionedModel<TKey> : Model<TKey>, IHasVersionReadOnly
where TKey : notnull
where TKey : notnull, IEquatable<TKey>
{
public Guid Version { get; protected set; }

Expand All @@ -26,15 +26,15 @@ protected VersionedModel(TKey id, Guid version) : base(id)
}

public abstract class EditModel<TKey> : Model<TKey>
where TKey : notnull
where TKey : notnull, IEquatable<TKey>
{
protected EditModel() : base(default!) { }

protected EditModel(TKey id) : base(id) { }
}

public abstract class VersionedEditModel<TKey> : VersionedModel<TKey>
where TKey : notnull
where TKey : notnull, IEquatable<TKey>
{
protected VersionedEditModel(Guid version) : base(default!, version) { }

Expand Down
2 changes: 1 addition & 1 deletion src/DotNetElements.Core/Core/ReadOnlyRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace DotNetElements.Core;
public abstract class ReadOnlyRepository<TDbContext, TEntity, TKey> : IReadOnlyRepository<TEntity, TKey>
where TDbContext : DbContext
where TEntity : Entity<TKey>
where TKey : notnull
where TKey : notnull, IEquatable<TKey>
{
protected TDbContext DbContext { get; private init; }
protected DbSet<TEntity> Entities { get; private init; }
Expand Down
8 changes: 5 additions & 3 deletions src/DotNetElements.Core/Core/Repository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace DotNetElements.Core;
public abstract class Repository<TDbContext, TEntity, TKey> : ReadOnlyRepository<TDbContext, TEntity, TKey>, IRepository<TEntity, TKey>, IAttachRelatedEntity
where TDbContext : DbContext
where TEntity : Entity<TKey>
where TKey : notnull
where TKey : notnull, IEquatable<TKey>
{
protected ICurrentUserProvider CurrentUserProvider { get; private init; }
protected TimeProvider TimeProvider { get; private init; }
Expand Down Expand Up @@ -61,7 +61,7 @@ public virtual async Task<CrudResult<TSelf>> CreateOrUpdateAsync<TSelf>(TKey id,
auditedEntity.SetCreationAudited(CurrentUserProvider.GetCurrentUserId(), TimeProvider.GetUtcNow());

var createdEntity = DbContext.Set<TSelf>().Attach(entity);

await DbContext.SaveChangesAsync();

return createdEntity.Entity;
Expand Down Expand Up @@ -96,6 +96,8 @@ public virtual async Task<CrudResult<TUpdatableEntity>> UpdateAsync<TUpdatableEn
where TUpdatableEntity : Entity<TKey>, IUpdatable<TFrom>
where TFrom : notnull
{
ThrowHelper.ThrowIfDefault(id);

IQueryable<TUpdatableEntity> query = DbContext.Set<TUpdatableEntity>();

RelatedEntitiesAttribute? relatedEntities = typeof(TUpdatableEntity).GetCustomAttribute<RelatedEntitiesAttribute>();
Expand Down Expand Up @@ -175,7 +177,7 @@ public virtual async Task ClearTable()
// todo protected would be better!
public TRelatedEntity AttachById<TRelatedEntity, TRelatedEntityKey>(TRelatedEntityKey id)
where TRelatedEntity : Entity<TRelatedEntityKey>, IRelatedEntity<TRelatedEntity, TRelatedEntityKey>
where TRelatedEntityKey : notnull
where TRelatedEntityKey : notnull, IEquatable<TRelatedEntityKey>
{
return DbContext.Set<TRelatedEntity>().Attach(TRelatedEntity.CreateRefById(id)).Entity;
}
Expand Down
Loading

0 comments on commit 8edfd03

Please sign in to comment.