using System;
using NPoco;
using Umbraco.Cms.Core.Cache;
using Umbraco.Cms.Core.Persistence;
using Umbraco.Cms.Core.Persistence.Querying;
using Umbraco.Cms.Core.Scoping;
using Umbraco.Cms.Infrastructure.Persistence.SqlSyntax;
namespace Umbraco.Cms.Infrastructure.Persistence.Repositories.Implement
{
///
/// Base repository class for all instances
///
public abstract class RepositoryBase : IRepository
{
///
/// Initializes a new instance of the class.
///
protected RepositoryBase(IScopeAccessor scopeAccessor, AppCaches appCaches)
{
ScopeAccessor = scopeAccessor ?? throw new ArgumentNullException(nameof(scopeAccessor));
AppCaches = appCaches ?? throw new ArgumentNullException(nameof(appCaches));
}
///
/// Gets the
///
protected AppCaches AppCaches { get; }
///
/// Gets the
///
protected IScopeAccessor ScopeAccessor { get; }
///
/// Gets the AmbientScope
///
protected IScope AmbientScope
{
get
{
IScope scope = ScopeAccessor.AmbientScope;
if (scope == null)
{
throw new InvalidOperationException("Cannot run a repository without an ambient scope.");
}
return scope;
}
}
///
/// Gets the repository's database.
///
protected IUmbracoDatabase Database => AmbientScope.Database;
///
/// Gets the Sql context.
///
protected ISqlContext SqlContext => AmbientScope.SqlContext;
///
/// Gets the
///
protected ISqlSyntaxProvider SqlSyntax => SqlContext.SqlSyntax;
///
/// Creates an expression
///
protected Sql Sql() => SqlContext.Sql();
///
/// Creates a expression
///
protected Sql Sql(string sql, params object[] args) => SqlContext.Sql(sql, args);
///
/// Creates a new query expression
///
protected IQuery Query() => SqlContext.Query();
}
}