2017-07-20 11:21:28 +02:00
|
|
|
|
using System;
|
2015-01-13 13:25:36 +11:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
2020-09-17 09:42:55 +02:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2016-04-12 15:11:07 +02:00
|
|
|
|
using NPoco;
|
2016-05-18 23:34:56 +02:00
|
|
|
|
using Umbraco.Core.Cache;
|
2017-12-12 15:04:13 +01:00
|
|
|
|
using Umbraco.Core.Exceptions;
|
2018-01-15 11:32:30 +01:00
|
|
|
|
using Umbraco.Core.Models.Entities;
|
2015-01-13 13:25:36 +11:00
|
|
|
|
using Umbraco.Core.Persistence.Querying;
|
2017-12-12 15:04:13 +01:00
|
|
|
|
using Umbraco.Core.Scoping;
|
2015-01-13 13:25:36 +11:00
|
|
|
|
|
2017-12-07 16:45:25 +01:00
|
|
|
|
namespace Umbraco.Core.Persistence.Repositories.Implement
|
2015-01-13 13:25:36 +11:00
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Simple abstract ReadOnly repository used to simply have PerformGet and PeformGetAll with an underlying cache
|
|
|
|
|
|
/// </summary>
|
2016-04-12 15:11:07 +02:00
|
|
|
|
internal abstract class SimpleGetRepository<TId, TEntity, TDto> : NPocoRepositoryBase<TId, TEntity>
|
2018-01-10 12:48:51 +01:00
|
|
|
|
where TEntity : class, IEntity
|
2015-01-13 13:25:36 +11:00
|
|
|
|
where TDto: class
|
|
|
|
|
|
{
|
2020-09-17 09:42:55 +02:00
|
|
|
|
protected SimpleGetRepository(IScopeAccessor scopeAccessor, AppCaches cache, ILogger<SimpleGetRepository<TId, TEntity, TDto>> logger)
|
2017-12-14 17:04:44 +01:00
|
|
|
|
: base(scopeAccessor, cache, logger)
|
2017-12-12 15:04:13 +01:00
|
|
|
|
{ }
|
2015-01-13 13:25:36 +11:00
|
|
|
|
|
|
|
|
|
|
protected abstract TEntity ConvertToEntity(TDto dto);
|
|
|
|
|
|
protected abstract object GetBaseWhereClauseArguments(TId id);
|
|
|
|
|
|
protected abstract string GetWhereInClauseForGetAll();
|
|
|
|
|
|
|
|
|
|
|
|
protected virtual IEnumerable<TDto> PerformFetch(Sql sql)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Database.Fetch<TDto>(sql);
|
2017-07-20 11:21:28 +02:00
|
|
|
|
}
|
2015-01-13 13:25:36 +11:00
|
|
|
|
|
|
|
|
|
|
protected override TEntity PerformGet(TId id)
|
|
|
|
|
|
{
|
|
|
|
|
|
var sql = GetBaseQuery(false);
|
|
|
|
|
|
sql.Where(GetBaseWhereClause(), GetBaseWhereClauseArguments(id));
|
|
|
|
|
|
|
|
|
|
|
|
var dto = PerformFetch(sql).FirstOrDefault();
|
|
|
|
|
|
if (dto == null)
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
|
|
var entity = ConvertToEntity(dto);
|
|
|
|
|
|
|
2018-01-10 12:48:51 +01:00
|
|
|
|
if (entity is EntityBase dirtyEntity)
|
2015-01-13 13:25:36 +11:00
|
|
|
|
{
|
2017-11-10 11:27:12 +01:00
|
|
|
|
// reset dirty initial properties (U4-1946)
|
2017-07-20 11:21:28 +02:00
|
|
|
|
dirtyEntity.ResetDirtyProperties(false);
|
2015-01-13 13:25:36 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return entity;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override IEnumerable<TEntity> PerformGetAll(params TId[] ids)
|
|
|
|
|
|
{
|
2016-04-12 15:11:07 +02:00
|
|
|
|
var sql = Sql().From<TEntity>();
|
2015-01-13 13:25:36 +11:00
|
|
|
|
|
|
|
|
|
|
if (ids.Any())
|
|
|
|
|
|
{
|
2016-04-12 15:11:07 +02:00
|
|
|
|
sql.Where(GetWhereInClauseForGetAll(), new { /*ids =*/ ids });
|
2015-01-13 13:25:36 +11:00
|
|
|
|
}
|
2017-07-20 11:21:28 +02:00
|
|
|
|
|
2015-01-13 13:25:36 +11:00
|
|
|
|
return Database.Fetch<TDto>(sql).Select(ConvertToEntity);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-08-26 17:39:23 +02:00
|
|
|
|
protected sealed override IEnumerable<TEntity> PerformGetByQuery(IQuery<TEntity> query)
|
2015-01-13 13:25:36 +11:00
|
|
|
|
{
|
|
|
|
|
|
var sqlClause = GetBaseQuery(false);
|
|
|
|
|
|
var translator = new SqlTranslator<TEntity>(sqlClause, query);
|
|
|
|
|
|
var sql = translator.Translate();
|
|
|
|
|
|
return Database.Fetch<TDto>(sql).Select(ConvertToEntity);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#region Not implemented and not required
|
|
|
|
|
|
|
2017-08-26 17:39:23 +02:00
|
|
|
|
protected sealed override IEnumerable<string> GetDeleteClauses()
|
2015-01-13 13:25:36 +11:00
|
|
|
|
{
|
2019-10-06 11:34:25 +02:00
|
|
|
|
throw new InvalidOperationException("This method won't be implemented.");
|
2015-01-13 13:25:36 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-10-06 11:34:25 +02:00
|
|
|
|
protected sealed override Guid NodeObjectTypeId => throw new InvalidOperationException("This property won't be implemented.");
|
2015-01-13 13:25:36 +11:00
|
|
|
|
|
2017-08-26 17:39:23 +02:00
|
|
|
|
protected sealed override void PersistNewItem(TEntity entity)
|
2015-01-13 13:25:36 +11:00
|
|
|
|
{
|
2019-10-06 11:34:25 +02:00
|
|
|
|
throw new InvalidOperationException("This method won't be implemented.");
|
2015-01-13 13:25:36 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-08-26 17:39:23 +02:00
|
|
|
|
protected sealed override void PersistUpdatedItem(TEntity entity)
|
2015-01-13 13:25:36 +11:00
|
|
|
|
{
|
2019-10-06 11:34:25 +02:00
|
|
|
|
throw new InvalidOperationException("This method won't be implemented.");
|
2015-01-13 13:25:36 +11:00
|
|
|
|
}
|
2017-12-12 15:04:13 +01:00
|
|
|
|
|
2015-01-13 13:25:36 +11:00
|
|
|
|
#endregion
|
|
|
|
|
|
}
|
2017-07-20 11:21:28 +02:00
|
|
|
|
}
|