Skip to content

Commit 4897de7

Browse files
Merge pull request #522 from johelvisguzman/cache-conventions-2
Added a generic constraint to the repository conventions
2 parents 16ede44 + e32663d commit 4897de7

File tree

11 files changed

+28
-17
lines changed

11 files changed

+28
-17
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public AdoNetRepositoryContext(string nameOrConnectionString, bool ensureDatabas
7979
{
8080
Guard.NotEmpty(nameOrConnectionString, nameof(nameOrConnectionString));
8181

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

8484
_dbHelper = new DbHelper(Conventions, nameOrConnectionString);
8585
_schemaConfigHelper = new SchemaTableConfigurationHelper(_dbHelper);
@@ -101,7 +101,7 @@ public AdoNetRepositoryContext(string providerName, string connectionString, boo
101101
Guard.NotEmpty(providerName, nameof(providerName));
102102
Guard.NotEmpty(connectionString, nameof(connectionString));
103103

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

106106
_dbHelper = new DbHelper(Conventions, providerName, connectionString);
107107
_schemaConfigHelper = new SchemaTableConfigurationHelper(_dbHelper);
@@ -121,7 +121,7 @@ public AdoNetRepositoryContext(DbConnection existingConnection, bool ensureDatab
121121
{
122122
Guard.NotNull(existingConnection, nameof(existingConnection));
123123

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

126126
_dbHelper = new DbHelper(Conventions, existingConnection);
127127
_schemaConfigHelper = new SchemaTableConfigurationHelper(_dbHelper);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ internal class EfRepositoryContext : LinqRepositoryContextBaseAsync
3636
/// <param name="context">The context.</param>
3737
public EfRepositoryContext(DbContext context)
3838
{
39-
Conventions = RepositoryConventions.Default(GetType());
39+
Conventions = RepositoryConventions.Default<EfRepositoryContext>();
4040

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ internal class EfCoreRepositoryContext : LinqRepositoryContextBaseAsync
3636
/// <param name="context">The context.</param>
3737
public EfCoreRepositoryContext(DbContext context)
3838
{
39-
Conventions = RepositoryConventions.Default(GetType());
39+
Conventions = RepositoryConventions.Default<EfCoreRepositoryContext>();
4040

4141
_context = Guard.NotNull(context, nameof(context));
4242
_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(GetType());
61+
Conventions = RepositoryConventions.Default<InMemoryRepositoryContext>();
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(GetType());
61+
Conventions = RepositoryConventions.Default<JsonRepositoryContext>();
6262

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

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(GetType());
61+
Conventions = RepositoryConventions.Default<XmlRepositoryContext>();
6262

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

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,10 @@ public RepositoryConventions([NotNull] Type owner)
6868
/// <summary>
6969
/// Gets the default conventions.
7070
/// </summary>
71-
/// <param name="owner">type that owns this conventions</param>
72-
public static IRepositoryConventions Default([NotNull] Type owner)
71+
/// <typeparam name="TOwner">The type that owns this conventions.</typeparam>
72+
public static IRepositoryConventions Default<TOwner>() where TOwner : IRepositoryContext
7373
{
74-
var conventions = new RepositoryConventions(Guard.NotNull(owner, nameof(owner)));
74+
var conventions = new RepositoryConventions(typeof(TOwner));
7575

7676
conventions.PrimaryKeysCallback = pi => PrimaryKeyConventionHelper.GetPrimaryKeyPropertyInfos(conventions, pi);
7777
conventions.ForeignKeysCallback = (sourceType, foreignType) => ForeignKeyConventionHelper.GetForeignKeyPropertyInfos(conventions, sourceType, foreignType);

src/DotNetToolkit.Repository/Configuration/LinqRepositoryContextBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public abstract class LinqRepositoryContextBase : IRepositoryContext
4444
/// </summary>
4545
protected LinqRepositoryContextBase()
4646
{
47-
Conventions = RepositoryConventions.Default(GetType());
47+
Conventions = RepositoryConventions.Default<LinqRepositoryContextBase>();
4848
}
4949

5050
#endregion

src/DotNetToolkit.Repository/Configuration/Options/RepositoryOptionsBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ public virtual RepositoryOptionsBuilder UseConventions([NotNull] Action<IReposit
228228

229229
conventionsAction(conventions);
230230

231-
_options = _options.With(Guard.NotNull(conventions, nameof(conventions)));
231+
_options = _options.With(conventions);
232232

233233
return this;
234234
}

src/DotNetToolkit.Repository/Extensions/RepositoryConventionsExtensions.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
{
33
using Configuration;
44
using Configuration.Conventions;
5+
using Extensions.Internal;
56
using JetBrains.Annotations;
67
using Properties;
78
using Queries.Strategies;
@@ -365,10 +366,20 @@ public static void Apply<T>([NotNull] this T target, [NotNull] T source) where T
365366
}
366367

367368
private static Type EnsureOwner([NotNull] IRepositoryConventions source)
368-
=> Guard.EnsureNotNull(
369-
Guard.NotNull(source, nameof(source)).Owner,
369+
{
370+
Guard.NotNull(source, nameof(source));
371+
372+
var owner = Guard.EnsureNotNull(
373+
source.Owner,
370374
string.Format("The conventions '{0}' cannot be null.", nameof(source.Owner)));
371375

376+
if (!owner.ImplementsInterface(typeof(IRepositoryContext)))
377+
throw new InvalidOperationException(string.Format(
378+
"The conventions '{0}' is not an instance of type '{1}'.", nameof(source.Owner), typeof(IRepositoryContext).FullName));
379+
380+
return owner;
381+
}
382+
372383
private static T EnsureCallback<T>([NotNull] T value, [InvokerParameterName] string parameterName) where T : class
373384
=> Guard.EnsureNotNull(
374385
value,

0 commit comments

Comments
 (0)