2020-12-09 22:43:49 +11:00
|
|
|
using System;
|
2017-05-12 14:49:44 +02:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2020-09-17 09:42:55 +02:00
|
|
|
using Microsoft.Extensions.Logging;
|
2020-12-09 22:43:49 +11:00
|
|
|
using NPoco;
|
2021-10-29 10:14:52 +02:00
|
|
|
using Umbraco.Cms.Core;
|
2021-02-09 10:22:42 +01:00
|
|
|
using Umbraco.Cms.Core.Cache;
|
|
|
|
|
using Umbraco.Cms.Core.Models.Entities;
|
|
|
|
|
using Umbraco.Cms.Core.Persistence;
|
|
|
|
|
using Umbraco.Cms.Core.Persistence.Querying;
|
|
|
|
|
using Umbraco.Cms.Core.Scoping;
|
2021-02-12 13:36:50 +01:00
|
|
|
using Umbraco.Cms.Infrastructure.Persistence.Querying;
|
2022-01-13 17:44:11 +00:00
|
|
|
using Umbraco.Cms.Infrastructure.Scoping;
|
2021-02-09 11:26:22 +01:00
|
|
|
using Umbraco.Extensions;
|
2017-05-12 14:49:44 +02:00
|
|
|
|
2021-02-12 13:36:50 +01:00
|
|
|
namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement
|
2017-05-12 14:49:44 +02:00
|
|
|
{
|
|
|
|
|
/// <summary>
|
2021-10-29 10:14:52 +02:00
|
|
|
/// Provides a base class to all <see cref="IEntity" /> based repositories.
|
2017-05-12 14:49:44 +02:00
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="TId">The type of the entity's unique identifier.</typeparam>
|
2020-12-09 22:43:49 +11:00
|
|
|
/// <typeparam name="TEntity">The type of the entity managed by this repository.</typeparam>
|
|
|
|
|
public abstract class EntityRepositoryBase<TId, TEntity> : RepositoryBase, IReadWriteQueryRepository<TId, TEntity>
|
2018-01-10 12:48:51 +01:00
|
|
|
where TEntity : class, IEntity
|
2017-05-12 14:49:44 +02:00
|
|
|
{
|
2022-02-24 09:24:56 +01:00
|
|
|
private static RepositoryCachePolicyOptions? s_defaultOptions;
|
|
|
|
|
private IRepositoryCachePolicy<TEntity, TId>? _cachePolicy;
|
|
|
|
|
private IQuery<TEntity>? _hasIdQuery;
|
2017-05-12 14:49:44 +02:00
|
|
|
|
2020-12-09 22:43:49 +11:00
|
|
|
/// <summary>
|
2021-10-29 10:14:52 +02:00
|
|
|
/// Initializes a new instance of the <see cref="EntityRepositoryBase{TId, TEntity}" /> class.
|
2020-12-09 22:43:49 +11:00
|
|
|
/// </summary>
|
2021-10-29 10:14:52 +02:00
|
|
|
protected EntityRepositoryBase(IScopeAccessor scopeAccessor, AppCaches appCaches,
|
|
|
|
|
ILogger<EntityRepositoryBase<TId, TEntity>> logger)
|
|
|
|
|
: base(scopeAccessor, appCaches) =>
|
2017-12-07 16:45:25 +01:00
|
|
|
Logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
|
|
|
|
|
2020-12-09 22:43:49 +11:00
|
|
|
/// <summary>
|
2021-10-29 10:14:52 +02:00
|
|
|
/// Gets the logger
|
2020-12-09 22:43:49 +11:00
|
|
|
/// </summary>
|
|
|
|
|
protected ILogger<EntityRepositoryBase<TId, TEntity>> Logger { get; }
|
2017-12-07 16:45:25 +01:00
|
|
|
|
2020-12-09 22:43:49 +11:00
|
|
|
/// <summary>
|
2021-10-29 10:14:52 +02:00
|
|
|
/// Gets the isolated cache for the <see cref="TEntity" />
|
2020-12-09 22:43:49 +11:00
|
|
|
/// </summary>
|
2019-01-18 07:56:38 +01:00
|
|
|
protected IAppPolicyCache GlobalIsolatedCache => AppCaches.IsolatedCaches.GetOrCreate<TEntity>();
|
2017-12-15 16:29:14 +01:00
|
|
|
|
2017-05-12 14:49:44 +02:00
|
|
|
/// <summary>
|
2021-10-29 10:14:52 +02:00
|
|
|
/// Gets the isolated cache.
|
2017-05-12 14:49:44 +02:00
|
|
|
/// </summary>
|
2017-12-15 16:29:14 +01:00
|
|
|
/// <remarks>Depends on the ambient scope cache mode.</remarks>
|
2019-01-18 07:56:38 +01:00
|
|
|
protected IAppPolicyCache IsolatedCache
|
2017-05-12 14:49:44 +02:00
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2017-12-12 15:04:13 +01:00
|
|
|
switch (AmbientScope.RepositoryCacheMode)
|
2017-05-12 14:49:44 +02:00
|
|
|
{
|
|
|
|
|
case RepositoryCacheMode.Default:
|
2019-01-17 11:19:06 +01:00
|
|
|
return AppCaches.IsolatedCaches.GetOrCreate<TEntity>();
|
2017-05-12 14:49:44 +02:00
|
|
|
case RepositoryCacheMode.Scoped:
|
2019-01-17 11:01:23 +01:00
|
|
|
return AmbientScope.IsolatedCaches.GetOrCreate<TEntity>();
|
2017-06-23 18:54:42 +02:00
|
|
|
case RepositoryCacheMode.None:
|
2019-01-17 11:01:23 +01:00
|
|
|
return NoAppCache.Instance;
|
2017-05-12 14:49:44 +02:00
|
|
|
default:
|
|
|
|
|
throw new Exception("oops: cache mode.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-09 22:43:49 +11:00
|
|
|
/// <summary>
|
2021-10-29 10:14:52 +02:00
|
|
|
/// Gets the default <see cref="RepositoryCachePolicyOptions" />
|
2020-12-09 22:43:49 +11:00
|
|
|
/// </summary>
|
|
|
|
|
protected virtual RepositoryCachePolicyOptions DefaultOptions => s_defaultOptions ?? (s_defaultOptions
|
2021-10-29 10:14:52 +02:00
|
|
|
= new RepositoryCachePolicyOptions(() =>
|
|
|
|
|
{
|
|
|
|
|
// get count of all entities of current type (TEntity) to ensure cached result is correct
|
|
|
|
|
// create query once if it is needed (no need for locking here) - query is static!
|
|
|
|
|
IQuery<TEntity> query = _hasIdQuery ??
|
|
|
|
|
(_hasIdQuery = AmbientScope.SqlContext.Query<TEntity>().Where(x => x.Id != 0));
|
|
|
|
|
return PerformCount(query);
|
|
|
|
|
}));
|
2017-05-12 14:49:44 +02:00
|
|
|
|
2020-12-09 22:43:49 +11:00
|
|
|
/// <summary>
|
2021-10-29 10:14:52 +02:00
|
|
|
/// Gets the repository cache policy
|
2020-12-09 22:43:49 +11:00
|
|
|
/// </summary>
|
2017-12-15 16:29:14 +01:00
|
|
|
protected IRepositoryCachePolicy<TEntity, TId> CachePolicy
|
2017-05-12 14:49:44 +02:00
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2019-01-17 11:19:06 +01:00
|
|
|
if (AppCaches == AppCaches.NoCache)
|
2020-12-09 22:43:49 +11:00
|
|
|
{
|
2017-12-15 16:29:14 +01:00
|
|
|
return NoCacheRepositoryCachePolicy<TEntity, TId>.Instance;
|
2020-12-09 22:43:49 +11:00
|
|
|
}
|
2017-05-12 14:49:44 +02:00
|
|
|
|
|
|
|
|
// create the cache policy using IsolatedCache which is either global
|
2017-07-20 11:21:28 +02:00
|
|
|
// or scoped depending on the repository cache mode for the current scope
|
2017-12-15 16:29:14 +01:00
|
|
|
|
2017-12-12 15:04:13 +01:00
|
|
|
switch (AmbientScope.RepositoryCacheMode)
|
2017-05-12 14:49:44 +02:00
|
|
|
{
|
|
|
|
|
case RepositoryCacheMode.Default:
|
|
|
|
|
case RepositoryCacheMode.Scoped:
|
2017-12-15 16:29:14 +01:00
|
|
|
// return the same cache policy in both cases - the cache policy is
|
|
|
|
|
// supposed to pick either the global or scope cache depending on the
|
|
|
|
|
// scope cache mode
|
|
|
|
|
return _cachePolicy ?? (_cachePolicy = CreateCachePolicy());
|
2017-06-23 18:54:42 +02:00
|
|
|
case RepositoryCacheMode.None:
|
2017-12-15 16:29:14 +01:00
|
|
|
return NoCacheRepositoryCachePolicy<TEntity, TId>.Instance;
|
2017-05-12 14:49:44 +02:00
|
|
|
default:
|
|
|
|
|
throw new Exception("oops: cache mode.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-09 22:43:49 +11:00
|
|
|
/// <summary>
|
2021-10-29 10:14:52 +02:00
|
|
|
/// Adds or Updates an entity of type TEntity
|
2020-12-09 22:43:49 +11:00
|
|
|
/// </summary>
|
2021-10-29 10:14:52 +02:00
|
|
|
/// <remarks>This method is backed by an <see cref="IAppPolicyCache" /> cache</remarks>
|
2020-03-28 15:20:17 +01:00
|
|
|
public virtual void Save(TEntity entity)
|
2017-05-12 14:49:44 +02:00
|
|
|
{
|
|
|
|
|
if (entity.HasIdentity == false)
|
2020-12-09 22:43:49 +11:00
|
|
|
{
|
2017-12-15 16:29:14 +01:00
|
|
|
CachePolicy.Create(entity, PersistNewItem);
|
2020-12-09 22:43:49 +11:00
|
|
|
}
|
2017-05-12 14:49:44 +02:00
|
|
|
else
|
2020-12-09 22:43:49 +11:00
|
|
|
{
|
2017-12-15 16:29:14 +01:00
|
|
|
CachePolicy.Update(entity, PersistUpdatedItem);
|
2020-12-09 22:43:49 +11:00
|
|
|
}
|
2017-05-12 14:49:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2021-10-29 10:14:52 +02:00
|
|
|
/// Deletes the passed in entity
|
2017-05-12 14:49:44 +02:00
|
|
|
/// </summary>
|
|
|
|
|
public virtual void Delete(TEntity entity)
|
2020-12-09 22:43:49 +11:00
|
|
|
=> CachePolicy.Delete(entity, PersistDeletedItem);
|
2017-05-12 14:49:44 +02:00
|
|
|
|
|
|
|
|
/// <summary>
|
2021-10-29 10:14:52 +02:00
|
|
|
/// Gets an entity by the passed in Id utilizing the repository's cache policy
|
2017-05-12 14:49:44 +02:00
|
|
|
/// </summary>
|
2022-02-24 09:24:56 +01:00
|
|
|
public TEntity? Get(TId? id)
|
2020-12-09 22:43:49 +11:00
|
|
|
=> CachePolicy.Get(id, PerformGet, PerformGetAll);
|
2017-05-12 14:49:44 +02:00
|
|
|
|
|
|
|
|
/// <summary>
|
2021-10-29 10:14:52 +02:00
|
|
|
/// Gets all entities of type TEntity or a list according to the passed in Ids
|
2017-05-12 14:49:44 +02:00
|
|
|
/// </summary>
|
2022-02-28 13:14:02 +01:00
|
|
|
public IEnumerable<TEntity> GetMany(params TId[]? ids)
|
2017-05-12 14:49:44 +02:00
|
|
|
{
|
2020-12-09 22:43:49 +11:00
|
|
|
// ensure they are de-duplicated, easy win if people don't do this as this can cause many excess queries
|
2022-02-24 09:24:56 +01:00
|
|
|
ids = ids?.Distinct()
|
2020-12-09 22:43:49 +11:00
|
|
|
|
|
|
|
|
// don't query by anything that is a default of T (like a zero)
|
2019-01-27 01:17:32 -05:00
|
|
|
// TODO: I think we should enabled this in case accidental calls are made to get all with invalid ids
|
2020-12-09 22:43:49 +11:00
|
|
|
// .Where(x => Equals(x, default(TId)) == false)
|
2017-05-12 14:49:44 +02:00
|
|
|
.ToArray();
|
|
|
|
|
|
2018-12-07 12:03:33 +11:00
|
|
|
// can't query more than 2000 ids at a time... but if someone is really querying 2000+ entities,
|
|
|
|
|
// the additional overhead of fetching them in groups is minimal compared to the lookup time of each group
|
2022-02-24 09:24:56 +01:00
|
|
|
if (ids?.Length <= Constants.Sql.MaxParameterCount)
|
2017-05-12 14:49:44 +02:00
|
|
|
{
|
2022-02-28 13:14:02 +01:00
|
|
|
return CachePolicy.GetAll(ids, PerformGetAll) ?? Enumerable.Empty<TEntity>();
|
2017-05-12 14:49:44 +02:00
|
|
|
}
|
|
|
|
|
|
2018-12-07 12:03:33 +11:00
|
|
|
var entities = new List<TEntity>();
|
2021-10-29 10:14:52 +02:00
|
|
|
foreach (IEnumerable<TId> group in ids.InGroupsOf(Constants.Sql.MaxParameterCount))
|
2018-12-07 12:03:33 +11:00
|
|
|
{
|
2022-02-24 09:24:56 +01:00
|
|
|
var groups = CachePolicy.GetAll(group.ToArray(), PerformGetAll);
|
|
|
|
|
if (groups is not null)
|
|
|
|
|
{
|
|
|
|
|
entities.AddRange(groups);
|
|
|
|
|
}
|
2018-12-07 12:03:33 +11:00
|
|
|
}
|
2020-12-09 22:43:49 +11:00
|
|
|
|
2018-12-07 12:03:33 +11:00
|
|
|
return entities;
|
2017-05-12 14:49:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2021-10-29 10:14:52 +02:00
|
|
|
/// Gets a list of entities by the passed in query
|
2017-05-12 14:49:44 +02:00
|
|
|
/// </summary>
|
2022-04-29 08:19:34 +02:00
|
|
|
public IEnumerable<TEntity> Get(IQuery<TEntity> query)
|
2022-02-28 13:14:02 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
|
|
// ensure we don't include any null refs in the returned collection!
|
2022-04-29 15:02:36 +02:00
|
|
|
return PerformGetByQuery(query)
|
2022-02-28 13:14:02 +01:00
|
|
|
.WhereNotNull();
|
|
|
|
|
}
|
2017-05-12 14:49:44 +02:00
|
|
|
|
|
|
|
|
/// <summary>
|
2021-10-29 10:14:52 +02:00
|
|
|
/// Returns a boolean indicating whether an entity with the passed Id exists
|
2017-05-12 14:49:44 +02:00
|
|
|
/// </summary>
|
|
|
|
|
public bool Exists(TId id)
|
2020-12-09 22:43:49 +11:00
|
|
|
=> CachePolicy.Exists(id, PerformExists, PerformGetAll);
|
2017-05-12 14:49:44 +02:00
|
|
|
|
|
|
|
|
/// <summary>
|
2021-10-29 10:14:52 +02:00
|
|
|
/// Returns an integer with the count of entities found with the passed in query
|
2017-05-12 14:49:44 +02:00
|
|
|
/// </summary>
|
|
|
|
|
public int Count(IQuery<TEntity> query)
|
2020-12-09 22:43:49 +11:00
|
|
|
=> PerformCount(query);
|
2021-10-29 10:14:52 +02:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Get the entity id for the <see cref="TEntity" />
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected virtual TId GetEntityId(TEntity entity)
|
|
|
|
|
=> (TId)(object)entity.Id;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Create the repository cache policy
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected virtual IRepositoryCachePolicy<TEntity, TId> CreateCachePolicy()
|
|
|
|
|
=> new DefaultRepositoryCachePolicy<TEntity, TId>(GlobalIsolatedCache, ScopeAccessor, DefaultOptions);
|
|
|
|
|
|
2022-02-24 09:24:56 +01:00
|
|
|
protected abstract TEntity? PerformGet(TId? id);
|
2021-10-29 10:14:52 +02:00
|
|
|
|
2022-04-29 08:19:34 +02:00
|
|
|
protected abstract IEnumerable<TEntity> PerformGetAll(params TId[]? ids);
|
2021-10-29 10:14:52 +02:00
|
|
|
|
2022-04-29 08:19:34 +02:00
|
|
|
protected abstract IEnumerable<TEntity> PerformGetByQuery(IQuery<TEntity> query);
|
2021-10-29 10:14:52 +02:00
|
|
|
|
|
|
|
|
protected abstract void PersistNewItem(TEntity item);
|
|
|
|
|
|
|
|
|
|
protected abstract void PersistUpdatedItem(TEntity item);
|
|
|
|
|
|
|
|
|
|
// TODO: obsolete, use QueryType instead everywhere like GetBaseQuery(QueryType queryType);
|
|
|
|
|
protected abstract Sql<ISqlContext> GetBaseQuery(bool isCount);
|
|
|
|
|
|
|
|
|
|
protected abstract string GetBaseWhereClause();
|
|
|
|
|
|
|
|
|
|
protected abstract IEnumerable<string> GetDeleteClauses();
|
|
|
|
|
|
|
|
|
|
protected virtual bool PerformExists(TId id)
|
|
|
|
|
{
|
|
|
|
|
Sql<ISqlContext> sql = GetBaseQuery(true);
|
|
|
|
|
sql.Where(GetBaseWhereClause(), new { id });
|
|
|
|
|
var count = Database.ExecuteScalar<int>(sql);
|
|
|
|
|
return count == 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected virtual int PerformCount(IQuery<TEntity> query)
|
|
|
|
|
{
|
|
|
|
|
Sql<ISqlContext> sqlClause = GetBaseQuery(true);
|
|
|
|
|
var translator = new SqlTranslator<TEntity>(sqlClause, query);
|
|
|
|
|
Sql<ISqlContext> sql = translator.Translate();
|
|
|
|
|
|
|
|
|
|
return Database.ExecuteScalar<int>(sql);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected virtual void PersistDeletedItem(TEntity entity)
|
|
|
|
|
{
|
|
|
|
|
IEnumerable<string> deletes = GetDeleteClauses();
|
|
|
|
|
foreach (var delete in deletes)
|
|
|
|
|
{
|
|
|
|
|
Database.Execute(delete, new { id = GetEntityId(entity) });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
entity.DeleteDate = DateTime.Now;
|
|
|
|
|
}
|
2017-05-12 14:49:44 +02:00
|
|
|
}
|
2017-07-20 11:21:28 +02:00
|
|
|
}
|