Skip to content

Commit 3cab415

Browse files
author
Jeff Treuting
committed
Overloaded IRepository<T> to be IRepository<T,int>
This allows the use of IRepository<Contact> repos = new InMemoryRepository<Contact>() instead of having to define IRepository<Contact,int> even when using the default version of the class where int isn't needed. Fixes #41
1 parent 5132825 commit 3cab415

7 files changed

Lines changed: 109 additions & 49 deletions

File tree

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,42 @@
1-
using System;
2-
using System.Data.Entity;
3-
using SharpRepository.Repository.Caching;
4-
5-
namespace SharpRepository.Ef5Repository
6-
{
7-
/// <summary>
8-
/// Entity Framework repository layer
9-
/// </summary>
10-
/// <typeparam name="T">The Entity type</typeparam>
11-
/// <typeparam name="TKey">The type of the primary key.</typeparam>
12-
public class Ef5Repository<T, TKey> : Ef5RepositoryBase<T, TKey> where T : class, new()
13-
{
14-
/// <summary>
15-
/// Initializes a new instance of the <see cref="Ef5Repository&lt;T, TKey&gt;"/> class.
16-
/// </summary>
17-
/// <param name="dbContext">The Entity Framework DbContext.</param>
18-
/// <param name="cachingStrategy">The caching strategy to use. Defaults to <see cref="NoCachingStrategy&lt;T, TKey&gt;" /></param>
19-
public Ef5Repository(DbContext dbContext, ICachingStrategy<T, TKey> cachingStrategy = null) : base(dbContext, cachingStrategy)
20-
{
21-
if (dbContext == null) throw new ArgumentNullException("dbContext");
22-
}
23-
}
24-
25-
/// <summary>
26-
/// Entity Framework repository layer
27-
/// </summary>
28-
/// <typeparam name="T">The Entity type</typeparam>
29-
public class Ef5Repository<T> : Ef5RepositoryBase<T, int> where T : class, new()
30-
{
31-
/// <summary>
32-
/// Initializes a new instance of the <see cref="Ef5Repository&lt;T&gt;"/> class.
33-
/// </summary>
34-
/// <param name="dbContext">The Entity Framework DbContext.</param>
35-
/// <param name="cachingStrategy">The caching strategy to use. Defaults to <see cref="NoCachingStrategy&lt;T, TKey&gt;" /></param>
36-
public Ef5Repository(DbContext dbContext, ICachingStrategy<T, int> cachingStrategy = null) : base(dbContext, cachingStrategy)
37-
{
38-
if (dbContext == null) throw new ArgumentNullException("dbContext");
39-
}
40-
}
41-
}
1+
using System;
2+
using System.Data.Entity;
3+
using SharpRepository.Repository;
4+
using SharpRepository.Repository.Caching;
5+
6+
namespace SharpRepository.Ef5Repository
7+
{
8+
/// <summary>
9+
/// Entity Framework repository layer
10+
/// </summary>
11+
/// <typeparam name="T">The Entity type</typeparam>
12+
/// <typeparam name="TKey">The type of the primary key.</typeparam>
13+
public class Ef5Repository<T, TKey> : Ef5RepositoryBase<T, TKey> where T : class, new()
14+
{
15+
/// <summary>
16+
/// Initializes a new instance of the <see cref="Ef5Repository&lt;T, TKey&gt;"/> class.
17+
/// </summary>
18+
/// <param name="dbContext">The Entity Framework DbContext.</param>
19+
/// <param name="cachingStrategy">The caching strategy to use. Defaults to <see cref="NoCachingStrategy&lt;T, TKey&gt;" /></param>
20+
public Ef5Repository(DbContext dbContext, ICachingStrategy<T, TKey> cachingStrategy = null) : base(dbContext, cachingStrategy)
21+
{
22+
if (dbContext == null) throw new ArgumentNullException("dbContext");
23+
}
24+
}
25+
26+
/// <summary>
27+
/// Entity Framework repository layer
28+
/// </summary>
29+
/// <typeparam name="T">The Entity type</typeparam>
30+
public class Ef5Repository<T> : Ef5RepositoryBase<T, int>, IRepository<T> where T : class, new()
31+
{
32+
/// <summary>
33+
/// Initializes a new instance of the <see cref="Ef5Repository&lt;T&gt;"/> class.
34+
/// </summary>
35+
/// <param name="dbContext">The Entity Framework DbContext.</param>
36+
/// <param name="cachingStrategy">The caching strategy to use. Defaults to <see cref="NoCachingStrategy&lt;T, TKey&gt;" /></param>
37+
public Ef5Repository(DbContext dbContext, ICachingStrategy<T, int> cachingStrategy = null) : base(dbContext, cachingStrategy)
38+
{
39+
if (dbContext == null) throw new ArgumentNullException("dbContext");
40+
}
41+
}
42+
}

SharpRepository.InMemoryRepository/InMemoryRepository.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using SharpRepository.Repository;
12
using SharpRepository.Repository.Caching;
23

34
namespace SharpRepository.InMemoryRepository
@@ -9,7 +10,8 @@ public InMemoryRepository(ICachingStrategy<T, TKey> cachingStrategy = null) : ba
910
}
1011
}
1112

12-
public class InMemoryRepository<T> : InMemoryRepositoryBase<T, int> where T : class, new()
13+
// The IRepository<T> is needed here and not on the one above in order to allow programming against IRepository<T> when using an int as the PK
14+
public class InMemoryRepository<T> : InMemoryRepositoryBase<T, int>, IRepository<T> where T : class, new()
1315
{
1416
public InMemoryRepository(ICachingStrategy<T, int> cachingStrategy = null)
1517
: base(cachingStrategy)

SharpRepository.Ioc.StructureMap/StructureMapExtensions.cs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,22 @@ public static LambdaInstance<object> ForRepositoriesUseSharpRepository(this IIni
1111
{
1212
initialization.Scan(scan => scan.IncludeNamespaceContainingType<IAmInRepository>());
1313

14+
initialization.For(typeof(IRepository<>))
15+
.Use(context =>
16+
{
17+
var genericArgs = context.BuildStack.Current.RequestedType.GetGenericArguments();
18+
var entityType = genericArgs[0];
19+
20+
return RepositoryFactory.GetInstance(entityType, repositoryName);
21+
}
22+
);
23+
1424
return initialization.For(typeof(IRepository<,>))
1525
.Use(context =>
1626
{
17-
var entityType = context.BuildStack.Current.RequestedType.GetGenericArguments()[0];
18-
var keyType = context.BuildStack.Current.RequestedType.GetGenericArguments()[1];
27+
var genericArgs = context.BuildStack.Current.RequestedType.GetGenericArguments();
28+
var entityType = genericArgs[0];
29+
var keyType = genericArgs[1];
1930

2031
return RepositoryFactory.GetInstance(entityType, keyType, repositoryName);
2132
}
@@ -26,11 +37,22 @@ public static LambdaInstance<object> ForRepositoriesUseSharpRepository(this IIni
2637
{
2738
initialization.Scan(scan => scan.IncludeNamespaceContainingType<IAmInRepository>());
2839

40+
initialization.For(typeof(IRepository<>))
41+
.Use(context =>
42+
{
43+
var genericArgs = context.BuildStack.Current.RequestedType.GetGenericArguments();
44+
var entityType = genericArgs[0];
45+
46+
return RepositoryFactory.GetInstance(entityType, configuration);
47+
}
48+
);
49+
2950
return initialization.For(typeof(IRepository<,>))
3051
.Use(context =>
3152
{
32-
var entityType = context.BuildStack.Current.RequestedType.GetGenericArguments()[0];
33-
var keyType = context.BuildStack.Current.RequestedType.GetGenericArguments()[1];
53+
var genericArgs = context.BuildStack.Current.RequestedType.GetGenericArguments();
54+
var entityType = genericArgs[0];
55+
var keyType = genericArgs[1];
3456

3557
return RepositoryFactory.GetInstance(entityType, keyType, configuration);
3658
}

SharpRepository.Repository/IRepository.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,12 @@ public interface IRepository<T, TKey> : IRepositoryQueryable<T> where T : class
8181

8282
bool CachingEnabled { get; set; }
8383
}
84+
85+
/// <summary>
86+
/// Defaults to int as the Primary Key
87+
/// </summary>
88+
/// <typeparam name="T"></typeparam>
89+
public interface IRepository<T> : IRepository<T, int> where T : class
90+
{
91+
}
8492
}

SharpRepository.Repository/RepositoryFactory.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ public static class RepositoryFactory
3737
return GetInstance<T, TKey>("sharpRepository", repositoryName);
3838
}
3939

40+
public static object GetInstance(Type entityType, string repositoryName = null)
41+
{
42+
return GetInstance(entityType, typeof(int), repositoryName);
43+
}
44+
4045
public static object GetInstance(Type entityType, Type keyType, string repositoryName = null)
4146
{
4247
return GetInstance(entityType, keyType, "sharpRepository", repositoryName);
@@ -63,6 +68,11 @@ public static object GetInstance(Type entityType, Type keyType, string repositor
6368
return GetInstance<T, TKey>(GetConfiguration(configSection), repositoryName);
6469
}
6570

71+
public static object GetInstance(Type entityType, string configSection, string repositoryName)
72+
{
73+
return GetInstance(entityType, typeof(int), configSection, repositoryName);
74+
}
75+
6676
public static object GetInstance(Type entityType, Type keyType, string configSection, string repositoryName)
6777
{
6878
return GetInstance(entityType, keyType, GetConfiguration(configSection), repositoryName);
@@ -79,6 +89,12 @@ public static object GetInstance(Type entityType, Type keyType, string configSec
7989
return configuration.GetInstance<T, TKey>(repositoryName);
8090
}
8191

92+
public static object GetInstance(Type entityType, ISharpRepositoryConfiguration configuration,
93+
string repositoryName = null)
94+
{
95+
return GetInstance(entityType, typeof (int), configuration, repositoryName);
96+
}
97+
8298
public static object GetInstance(Type entityType, Type keyType, ISharpRepositoryConfiguration configuration, string repositoryName = null)
8399
{
84100
if (String.IsNullOrEmpty(repositoryName))

SharpRepository.Tests/Configuration/ConfigurationTests.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
using System;
2-
using System.Configuration;
32
using NUnit.Framework;
43
using SharpRepository.Ef5Repository;
54
using SharpRepository.Repository.Caching;
65
using SharpRepository.Repository.Configuration;
76
using SharpRepository.Tests.TestObjects;
87
using SharpRepository.InMemoryRepository;
9-
using SharpRepository.Ef5Repository;
108
using SharpRepository.Repository;
119

1210
namespace SharpRepository.Tests.Configuration
@@ -16,6 +14,17 @@ namespace SharpRepository.Tests.Configuration
1614
[TestFixture]
1715
public class ConfigurationTests
1816
{
17+
[Test]
18+
public void InMemoryConfigurationNoParametersNoKeyTypes()
19+
{
20+
var repos = RepositoryFactory.GetInstance<Contact>();
21+
22+
if (!(repos is InMemoryRepository<Contact, int>))
23+
{
24+
throw new Exception("Not InMemoryRepository");
25+
}
26+
}
27+
1928
[Test]
2029
public void InMemoryConfigurationNoParameters()
2130
{
@@ -26,6 +35,7 @@ public void InMemoryConfigurationNoParameters()
2635
throw new Exception("Not InMemoryRepository");
2736
}
2837
}
38+
2939
[Test]
3040
public void LoadConfigurationRepositoryByName()
3141
{

SharpRepository.XmlRepository/XmlRepository.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using SharpRepository.Repository;
23
using SharpRepository.Repository.Caching;
34

45
namespace SharpRepository.XmlRepository
@@ -25,7 +26,7 @@ public XmlRepository(string storagePath, ICachingStrategy<T, TKey> cachingStrate
2526
/// XML Repository layer
2627
/// </summary>
2728
/// <typeparam name="T">The object type that is stored as XML.</typeparam>
28-
public class XmlRepository<T> : XmlRepositoryBase<T, int> where T : class, new()
29+
public class XmlRepository<T> : XmlRepositoryBase<T, int>, IRepository<T> where T : class, new()
2930
{
3031
/// <summary>
3132
/// Initializes a new instance of the <see cref="XmlRepository&lt;T&gt;"/> class.

0 commit comments

Comments
 (0)