Repository Implementations
Data Source
Library
Abstraction Patterns
Business Data Logic
using System;
using Deveel.Data;
namespace Foo {
public interface IData {
string? Id { get; }
byte[] Content { get; }
string ContentType { get; }
}
public interface IDataRepository<TData> : IRepository<TData> where TData : class, IData {
Task<string> GetContentTypeAsync(TData data, CancellationToken cancellationToken = default);
Task<byte[]> GetContentAsync(TData data, CancellationToken cancellationToken = default);
Task SetContentAsync(TData data, string contentType, byte[] content, CancellationTyoken cancellationToken = default);
}
public class DataManager<TData> : EntityManager<TData> where TData : class, IData {
public DataManager(IDataRepository<TData> repository, IEntityValidator<TData>? validator = null, IServiceProvider? services = null. ILoggerFactory? loggerFactory = null)
: base(repository, validator, null, null, services, loggerFactory) {
}
protected IDataRepository<TData> DataRepository => (IDataRepository<TData>)base.Repository;
public Task<OperationResult> SetContentAsync(TData data, string contentType, byte[] content, CancellationToken? cancellationToken = null) {
ThrowIfDisposed();
try {
var existingContentType = await DataRepository.GetContentTypeAsync(data, GetCancellationToken(cancellationToken));
var existingContent = await DataRepository.GetContentAsync(data, GetCancellationToken(cancellationToken));
if ((existingContentType != null && existingContentType == contentType) &&
(existingContent != null && existingContent == content)) {
return OperationResult.NotModified;
}
await DataRepository.SetContentAsync(data, contentType, content, GetCancellationToken(cancellationToken);
// this will invoke the validation before invoking the
// Update method of the repository
return await UpdateAsync(data);
} catch (Exception ex) {
Logger.LogError(ex, "Could not set the content");
return Fail("DATA_ERROR");
}
}
}
}Last updated