Skip to content

Commit f3a95ae

Browse files
Merge pull request #520 from johelvisguzman/cache-conventions
Allowed to cache the repository conventions results by the context that is attached to (owner)
2 parents 3c5e7bc + 5eacf87 commit f3a95ae

File tree

18 files changed

+270
-131
lines changed

18 files changed

+270
-131
lines changed

src/DotNetToolkit.Repository.AdoNet/Internal/AdoNetRepositoryContext.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ internal class AdoNetRepositoryContext : IRepositoryContextAsync
4343
#region Properties
4444

4545
/// <summary>
46-
/// Gets or sets the configurable conventions.
46+
/// Gets the configurable conventions.
4747
/// </summary>
48-
public IRepositoryConventions Conventions { get; set; }
48+
public IRepositoryConventions Conventions { get; internal set; }
4949

5050
/// <summary>
5151
/// Gets or sets the repository context logger.
@@ -79,10 +79,10 @@ public AdoNetRepositoryContext(string nameOrConnectionString, bool ensureDatabas
7979
{
8080
Guard.NotEmpty(nameOrConnectionString, nameof(nameOrConnectionString));
8181

82-
Conventions = RepositoryConventions.Default;
82+
Conventions = RepositoryConventions.Default(GetType());
8383

8484
_dbHelper = new DbHelper(Conventions, nameOrConnectionString);
85-
_schemaConfigHelper = new SchemaTableConfigurationHelper(Conventions, _dbHelper);
85+
_schemaConfigHelper = new SchemaTableConfigurationHelper(_dbHelper);
8686
_ensureDatabaseCreated = ensureDatabaseCreated;
8787
}
8888

@@ -101,10 +101,10 @@ public AdoNetRepositoryContext(string providerName, string connectionString, boo
101101
Guard.NotEmpty(providerName, nameof(providerName));
102102
Guard.NotEmpty(connectionString, nameof(connectionString));
103103

104-
Conventions = RepositoryConventions.Default;
104+
Conventions = RepositoryConventions.Default(GetType());
105105

106106
_dbHelper = new DbHelper(Conventions, providerName, connectionString);
107-
_schemaConfigHelper = new SchemaTableConfigurationHelper(Conventions, _dbHelper);
107+
_schemaConfigHelper = new SchemaTableConfigurationHelper(_dbHelper);
108108
_ensureDatabaseCreated = ensureDatabaseCreated;
109109
}
110110

@@ -121,10 +121,10 @@ public AdoNetRepositoryContext(DbConnection existingConnection, bool ensureDatab
121121
{
122122
Guard.NotNull(existingConnection, nameof(existingConnection));
123123

124-
Conventions = RepositoryConventions.Default;
124+
Conventions = RepositoryConventions.Default(GetType());
125125

126126
_dbHelper = new DbHelper(Conventions, existingConnection);
127-
_schemaConfigHelper = new SchemaTableConfigurationHelper(Conventions, _dbHelper);
127+
_schemaConfigHelper = new SchemaTableConfigurationHelper(_dbHelper);
128128
_ensureDatabaseCreated = ensureDatabaseCreated;
129129
}
130130

src/DotNetToolkit.Repository.AdoNet/Internal/DbHelper.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ internal class DbHelper : IDisposable
6767
/// </summary>
6868
public ILogger Logger { get; private set; } = NullLogger.Instance;
6969

70+
/// <summary>
71+
/// Gets the repository conventions.
72+
/// </summary>
73+
public IRepositoryConventions Conventions { get { return _conventions; } }
74+
7075
#endregion
7176

7277
#region Constructors

src/DotNetToolkit.Repository.AdoNet/Internal/Schema/SchemaTableConfigurationHelper.cs

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
namespace DotNetToolkit.Repository.AdoNet.Internal.Schema
22
{
3-
using Configuration.Conventions;
4-
using Configuration.Logging;
53
using Extensions;
64
using Extensions.Internal;
75
using Properties;
@@ -24,7 +22,6 @@ internal class SchemaTableConfigurationHelper
2422
{
2523
#region Fields
2624

27-
private readonly IRepositoryConventions _conventions;
2825
private readonly DbHelper _dbHelper;
2926
private readonly Dictionary<Type, bool> _tableCreationMapping = new Dictionary<Type, bool>();
3027
private readonly Dictionary<Type, Tuple<string, Type>> _foreignTableCreationMapping = new Dictionary<Type, Tuple<string, Type>>();
@@ -37,11 +34,9 @@ internal class SchemaTableConfigurationHelper
3734
/// <summary>
3835
/// Initializes a new instance of the <see cref="SchemaTableConfigurationHelper"/> class.
3936
/// </summary>
40-
/// <param name="conventions">The configurable conventions.</param>
4137
/// <param name="dbHelper">The database helper.</param>
42-
public SchemaTableConfigurationHelper(IRepositoryConventions conventions, DbHelper dbHelper)
38+
public SchemaTableConfigurationHelper(DbHelper dbHelper)
4339
{
44-
_conventions = Guard.NotNull(conventions, nameof(conventions));
4540
_dbHelper = Guard.NotNull(dbHelper, nameof(dbHelper));
4641
}
4742

@@ -182,7 +177,7 @@ private bool ExecuteTableExists(Type entityType)
182177
if (entityType == null)
183178
throw new ArgumentNullException(nameof(entityType));
184179

185-
var tableName = _conventions.GetTableName(entityType);
180+
var tableName = _dbHelper.Conventions.GetTableName(entityType);
186181
var sql = @"SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = @tableName";
187182
var parameters = new Dictionary<string, object> { { "@tableName", tableName } };
188183

@@ -206,7 +201,7 @@ private void ExecuteTableValidate(Type entityType)
206201
if (entityType == null)
207202
throw new ArgumentNullException(nameof(entityType));
208203

209-
var tableName = _conventions.GetTableName(entityType);
204+
var tableName = _dbHelper.Conventions.GetTableName(entityType);
210205
var schemaTableColumns = GetSchemaTableColumns(tableName);
211206

212207
ValidateTable(entityType, schemaTableColumns, tableName);
@@ -217,7 +212,7 @@ private async Task ExecuteTableValidateAsync(Type entityType, CancellationToken
217212
if (entityType == null)
218213
throw new ArgumentNullException(nameof(entityType));
219214

220-
var tableName = _conventions.GetTableName(entityType);
215+
var tableName = _dbHelper.Conventions.GetTableName(entityType);
221216
var schemaTableColumns = await GetSchemaTableColumnsAsync(tableName, cancellationToken);
222217

223218
ValidateTable(entityType, schemaTableColumns, tableName);
@@ -230,8 +225,8 @@ private void ValidateTable(Type entityType, IEnumerable<SchemaTableColumn> schem
230225
var propertiesMapping = entityType
231226
.GetRuntimeProperties()
232227
.Where(x => x.IsPrimitive())
233-
.OrderBy(_conventions.GetColumnOrderOrDefault)
234-
.ToDictionary(_conventions.GetColumnName, x => x);
228+
.OrderBy(_dbHelper.Conventions.GetColumnOrderOrDefault)
229+
.ToDictionary(_dbHelper.Conventions.GetColumnName, x => x);
235230

236231
var columns = propertiesMapping.Keys.ToArray();
237232

@@ -247,14 +242,14 @@ private void ValidateTable(Type entityType, IEnumerable<SchemaTableColumn> schem
247242

248243
// Gets all the constraints
249244
var schemaTableColumnConstraintsMapping = GetSchemaTableColumnConstraintsMapping(tableName, columns);
250-
var primaryKeyPropertiesMapping = _conventions.GetPrimaryKeyPropertyInfos(entityType).ToDictionary(_conventions.GetColumnName, x => x);
245+
var primaryKeyPropertiesMapping = _dbHelper.Conventions.GetPrimaryKeyPropertyInfos(entityType).ToDictionary(_dbHelper.Conventions.GetColumnName, x => x);
251246

252247
// Gets all the foreign keys
253248
var foreignKeyPropertyInfosMapping = entityType
254249
.GetRuntimeProperties()
255250
.Where(x => x.IsComplex())
256-
.Select(pi => _conventions.GetForeignKeyPropertyInfos(entityType, pi.PropertyType)
257-
.ToDictionary(_conventions.GetColumnName, x => pi))
251+
.Select(pi => _dbHelper.Conventions.GetForeignKeyPropertyInfos(entityType, pi.PropertyType)
252+
.ToDictionary(_dbHelper.Conventions.GetColumnName, x => pi))
258253
.SelectMany(x => x)
259254
.ToDictionary(x => x.Key, x => x.Value);
260255

@@ -284,7 +279,7 @@ private void ValidateTable(Type entityType, IEnumerable<SchemaTableColumn> schem
284279
{
285280
var foreignPropertyInfo = foreignKeyPropertyInfosMapping[columnName];
286281

287-
if (_conventions.HasCompositePrimaryKey(foreignPropertyInfo.PropertyType))
282+
if (_dbHelper.Conventions.HasCompositePrimaryKey(foreignPropertyInfo.PropertyType))
288283
{
289284
order = schemaTableColumn.OrdinalPosition - (schemaTableColumn.OrdinalPosition - columnAttribute.Order);
290285
}
@@ -474,10 +469,10 @@ private Dictionary<Type, string> GetPreparedCreateTableQueriesMapping(Type entit
474469

475470
private string PrepareCreateTableQuery(Type entityType)
476471
{
477-
var tableName = _conventions.GetTableName(entityType);
472+
var tableName = _dbHelper.Conventions.GetTableName(entityType);
478473

479474
// Check if has composite primary keys (more than one key), and if so, it needs to defined an ordering
480-
var primaryKeyPropertyInfos = _conventions.GetPrimaryKeyPropertyInfos(entityType);
475+
var primaryKeyPropertyInfos = _dbHelper.Conventions.GetPrimaryKeyPropertyInfos(entityType);
481476
if (primaryKeyPropertyInfos.Count() > 1)
482477
{
483478
var keysHaveNoOrdering = primaryKeyPropertyInfos.Any(x => x.GetCustomAttribute<ColumnAttribute>() == null);
@@ -495,10 +490,10 @@ private string PrepareCreateTableQuery(Type entityType)
495490
// Check if has composite foreign keys (more than one key for a given navigation property), and if so, it needs to defined an ordering
496491
var propertiesMapping = properties
497492
.Where(x => x.IsPrimitive())
498-
.OrderBy(_conventions.GetColumnOrderOrDefault)
499-
.ToDictionary(_conventions.GetColumnName, x => x);
493+
.OrderBy(_dbHelper.Conventions.GetColumnOrderOrDefault)
494+
.ToDictionary(_dbHelper.Conventions.GetColumnName, x => x);
500495

501-
var primaryKeyPropertyInfosMapping = _conventions.GetPrimaryKeyPropertyInfos(entityType).ToDictionary(_conventions.GetColumnName, x => x);
496+
var primaryKeyPropertyInfosMapping = _dbHelper.Conventions.GetPrimaryKeyPropertyInfos(entityType).ToDictionary(_dbHelper.Conventions.GetColumnName, x => x);
502497
var primaryKeyConstraints = new List<string>();
503498
var foreignKeyConstraints = new Dictionary<string, List<Tuple<string, string>>>();
504499
var foreignKeyOrder = 0;
@@ -549,10 +544,10 @@ private string PrepareCreateTableQuery(Type entityType)
549544

550545
if (hasPrincipal)
551546
{
552-
var foreignNavigationTableName = _conventions.GetTableName(foreignNavigationType);
553-
var foreignPrimaryKeyColumnNames = _conventions
547+
var foreignNavigationTableName = _dbHelper.Conventions.GetTableName(foreignNavigationType);
548+
var foreignPrimaryKeyColumnNames = _dbHelper.Conventions
554549
.GetPrimaryKeyPropertyInfos(foreignNavigationType)
555-
.Select(_conventions.GetColumnName);
550+
.Select(_dbHelper.Conventions.GetColumnName);
556551
var foreignPrimaryKeyColumnName = foreignPrimaryKeyColumnNames.ElementAt(foreignKeyOrder++);
557552

558553
if (!foreignKeyConstraints.ContainsKey(foreignNavigationTableName))
@@ -590,7 +585,7 @@ private string PrepareCreateTableQuery(Type entityType)
590585
}
591586

592587
// Constraints
593-
if (_conventions.IsColumnIdentity(item.Value))
588+
if (_dbHelper.Conventions.IsColumnIdentity(item.Value))
594589
{
595590
sb.Append("IDENTITY ");
596591
}
@@ -624,7 +619,7 @@ private string PrepareCreateTableQuery(Type entityType)
624619
if (!foreignKeyConstraints.Any())
625620
{
626621
var primaryKeyPropertyInfo = primaryKeyPropertyInfosMapping.First().Value;
627-
var primaryKeyName = _conventions.GetColumnName(primaryKeyPropertyInfo);
622+
var primaryKeyName = _dbHelper.Conventions.GetColumnName(primaryKeyPropertyInfo);
628623

629624
var keyMappingFromForeign = GetForeignKeyPropertyInfosMapping(entityType, true);
630625

@@ -673,8 +668,8 @@ private Dictionary<string, PropertyInfo> GetForeignKeyPropertyInfosMapping(Type
673668
foreach (var pi in foreignNavigationPropertyInfos)
674669
{
675670
var foreignKeyPropertyInfos = getFromForeign
676-
? _conventions.GetForeignKeyPropertyInfos(pi.PropertyType, entityType)
677-
: _conventions.GetForeignKeyPropertyInfos(entityType, pi.PropertyType);
671+
? _dbHelper.Conventions.GetForeignKeyPropertyInfos(pi.PropertyType, entityType)
672+
: _dbHelper.Conventions.GetForeignKeyPropertyInfos(entityType, pi.PropertyType);
678673

679674
if (foreignKeyPropertyInfos.Any())
680675
{
@@ -686,7 +681,7 @@ private Dictionary<string, PropertyInfo> GetForeignKeyPropertyInfosMapping(Type
686681
Resources.UnableToDetermineCompositePrimaryKeyOrdering, "foreign", entityType.FullName));
687682
}
688683

689-
var dict = foreignKeyPropertyInfos.ToDictionary(_conventions.GetColumnName, x => pi);
684+
var dict = foreignKeyPropertyInfos.ToDictionary(_dbHelper.Conventions.GetColumnName, x => pi);
690685

691686
foreach (var item in dict)
692687
{
@@ -703,7 +698,7 @@ private async Task<bool> ExecuteTableExistsAsync(Type entityType, CancellationTo
703698
if (entityType == null)
704699
throw new ArgumentNullException(nameof(entityType));
705700

706-
var tableName = _conventions.GetTableName(entityType);
701+
var tableName = _dbHelper.Conventions.GetTableName(entityType);
707702
var sql = @"SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = @tableName";
708703
var parameters = new Dictionary<string, object> { { "@tableName", tableName } };
709704

src/DotNetToolkit.Repository.EntityFramework/Internal/EfRepositoryContext.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
{
33
using Configuration;
44
using Configuration.Conventions;
5-
using Configuration.Logging;
65
using Extensions;
76
using Extensions.Internal;
87
using Queries;
@@ -37,7 +36,7 @@ internal class EfRepositoryContext : LinqRepositoryContextBaseAsync
3736
/// <param name="context">The context.</param>
3837
public EfRepositoryContext(DbContext context)
3938
{
40-
Conventions = RepositoryConventions.Default;
39+
Conventions = RepositoryConventions.Default(GetType());
4140

4241
_context = Guard.NotNull(context, nameof(context));
4342
_context.Database.Log = s => Logger?.Debug(s.TrimEnd(Environment.NewLine.ToCharArray()));

src/DotNetToolkit.Repository.EntityFrameworkCore/Internal/EfCoreRepositoryContext.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
{
33
using Configuration;
44
using Configuration.Conventions;
5-
using Configuration.Logging;
65
using Extensions;
76
using Extensions.Internal;
87
using Microsoft.EntityFrameworkCore;
@@ -37,7 +36,7 @@ internal class EfCoreRepositoryContext : LinqRepositoryContextBaseAsync
3736
/// <param name="context">The context.</param>
3837
public EfCoreRepositoryContext(DbContext context)
3938
{
40-
Conventions = RepositoryConventions.Default;
39+
Conventions = RepositoryConventions.Default(GetType());
4140

4241
_context = Guard.NotNull(context, nameof(context));
4342
_context.ConfigureLogging(s => Logger.Debug(s.TrimEnd(Environment.NewLine.ToCharArray())));

src/DotNetToolkit.Repository.InMemory/Internal/InMemoryRepositoryContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public InMemoryRepositoryContext(bool ignoreTransactionWarning = false, bool ign
5858
public InMemoryRepositoryContext(string databaseName, bool ignoreTransactionWarning = false, bool ignoreSqlQueryWarning = false)
5959
{
6060
DatabaseName = string.IsNullOrEmpty(databaseName) ? DefaultDatabaseName : databaseName;
61-
Conventions = RepositoryConventions.Default;
61+
Conventions = RepositoryConventions.Default(GetType());
6262

6363
_ignoreTransactionWarning = ignoreTransactionWarning;
6464
_ignoreSqlQueryWarning = ignoreSqlQueryWarning;

src/DotNetToolkit.Repository.Json/Internal/FileStreamRepositoryContextBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ protected FileStreamRepositoryContextBase(string path, string extension, bool ig
5858
if (!path.EndsWith(@"\"))
5959
path += @"\";
6060

61-
Conventions = RepositoryConventions.Default;
61+
Conventions = RepositoryConventions.Default(GetType());
6262

6363
_items = new BlockingCollection<EntitySet>();
6464
_ignoreTransactionWarning = ignoreTransactionWarning;

src/DotNetToolkit.Repository.NHibernate/Internal/NHibernateRepositoryContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ private void ConfigureConventions(ISessionFactory sessionFactory)
7070
{
7171
var helper = new NHibernateConventionsHelper(Guard.NotNull(sessionFactory, nameof(sessionFactory)));
7272

73-
Conventions = new RepositoryConventions
73+
Conventions = new RepositoryConventions(GetType())
7474
{
7575
PrimaryKeysCallback = type => helper.GetPrimaryKeyPropertyInfos(type),
7676
ColumnNameCallback = pi => helper.GetColumnName(pi),

src/DotNetToolkit.Repository.Xml/Internal/FileStreamRepositoryContextBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ protected FileStreamRepositoryContextBase(string path, string extension, bool ig
5858
if (!path.EndsWith(@"\"))
5959
path += @"\";
6060

61-
Conventions = RepositoryConventions.Default;
61+
Conventions = RepositoryConventions.Default(GetType());
6262

6363
_items = new BlockingCollection<EntitySet>();
6464
_ignoreTransactionWarning = ignoreTransactionWarning;

src/DotNetToolkit.Repository/Configuration/Conventions/IRepositoryConventions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,10 @@ public interface IRepositoryConventions
4242
/// Gets or sets a callback function for determining whether the specified property is mapped.
4343
/// </summary>
4444
Func<PropertyInfo, bool> IsColumnMappedCallback { get; set; }
45+
46+
/// <summary>
47+
/// Gets the type that owns this conventions.
48+
/// </summary>
49+
Type Owner { get; }
4550
}
4651
}

0 commit comments

Comments
 (0)