Files
mixtape/old/zero.Core/Collections/EntityCollection/EntityCollection.cs
T

146 lines
3.6 KiB
C#
Raw Normal View History

2021-11-18 16:00:20 +01:00
namespace zero.Core;
using FluentValidation.Results;
using Raven.Client.Documents.Indexes;
using Raven.Client.Documents.Linq;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using zero.Core.Database;
using zero.Core.Entities;
using zero.Core.Validation;
2021-11-19 12:03:36 +01:00
2021-11-19 13:31:54 +01:00
public abstract partial class EntityCollection<T> : IEntityCollection<T> where T : ZeroIdEntity, new()
2021-11-18 16:00:20 +01:00
{
/// <inheritdoc />
public Guid Guid { get; private set; } = Guid.NewGuid();
/// <inheritdoc />
public IZeroDocumentSession Session => Context.Store.Session();
protected record EntityCollectionOptions(bool IncludeInactive);
protected IZeroContext Context { get; private set; }
2021-11-19 13:31:54 +01:00
protected EntityCollectionOptions Options { get; set; }
2021-11-18 16:00:20 +01:00
2021-11-19 13:31:54 +01:00
protected IInterceptorRunner<T> Interceptors { get; private set; }
2021-11-18 16:00:20 +01:00
2021-11-19 13:31:54 +01:00
public EntityCollection(ICollectionContext<T> collectionContext)
2021-11-18 16:00:20 +01:00
{
2021-11-19 13:31:54 +01:00
Context = collectionContext.Context;
Interceptors = collectionContext.Interceptors;
2021-11-18 16:00:20 +01:00
Options = new(true);
}
2021-11-19 13:31:54 +01:00
/// <summary>
/// Get new instance of an entity
/// </summary>
public virtual Task<T> Empty()
{
return Task.FromResult(new T());
}
2021-11-18 16:00:20 +01:00
/// <inheritdoc />
public virtual async Task<ValidationResult> Validate(T model)
{
ZeroValidator<T> validator = new();
ValidationRules(validator);
return await validator.ValidateAsync(model);
}
2021-11-19 12:03:36 +01:00
/// <summary>
/// Create rules for validation
/// </summary>
protected virtual void ValidationRules(ZeroValidator<T> validator) { }
2021-11-18 16:00:20 +01:00
/// <summary>
/// Do only return the model when it is set to active or inactive entities are included with IncludeInactive()
/// </summary>
protected virtual T WhenActive(T model)
{
return model != null && (Options.IncludeInactive || (model is ZeroEntity ? (model as ZeroEntity).IsActive : true)) ? model : default;
}
}
2021-11-19 13:31:54 +01:00
public interface IEntityCollection<T> where T : ZeroIdEntity, new()
2021-11-18 16:00:20 +01:00
{
2021-11-19 13:31:54 +01:00
/// <summary>
/// Get new instance of an entity
/// </summary>
Task<T> Empty();
2021-11-18 16:00:20 +01:00
/// <summary>
/// Get an entity by Id
/// </summary>
Task<T> Load(string id, string changeVector = null);
/// <summary>
/// Get entities by ids
/// </summary>
2021-11-19 13:31:54 +01:00
Task<Dictionary<string, T>> Load(IEnumerable<string> ids);
2021-11-18 16:00:20 +01:00
/// <summary>
/// Get entities by query
/// </summary>
Task<ListResult<T>> Load(ListQuery<T> query);
/// <summary>
/// Get entities by query (by using the specified index)
/// </summary>
Task<ListResult<T>> Load<TIndex>(ListQuery<T> query) where TIndex : AbstractCommonApiForIndexes, new();
/// <summary>
/// Get all entities from this collection.
/// Warning: Don't use this method for large collections. Stream the results instead.
/// </summary>
Task<List<T>> LoadAll();
/// <summary>
/// Stream the collection
/// </summary>
IAsyncEnumerable<T> Stream();
/// <summary>
/// Stream the collection
/// </summary>
IAsyncEnumerable<T> Stream(Func<IRavenQueryable<T>, IRavenQueryable<T>> expression);
/// <summary>
/// Validates an entity in this collection
/// </summary>
Task<ValidationResult> Validate(T model);
2021-11-19 12:03:36 +01:00
/// <summary>
/// Updates or creates an entity with an optional validator
/// </summary>
Task<EntityResult<T>> Save(T model);
/// <summary>
/// Deletes an entity
/// </summary>
Task<EntityResult<T>> Delete(T model);
/// <summary>
/// Deletes entities
/// </summary>
Task<int> Delete(IEnumerable<T> models);
/// <summary>
/// Deletes an entity by Id
/// </summary>
Task<EntityResult<T>> Delete(string id);
/// <summary>
/// Deletes entities by Id
/// </summary>
Task<int> Delete(IEnumerable<string> ids);
2021-11-18 16:00:20 +01:00
}