Skip to content

Commit 76ce058

Browse files
author
jtreuting
committed
Removed TKey from CompositeRepository
No need to do a Get() call on the CompositeRepository so the TKey is not needed. Then this allows the Join() method to automatically infer the generic types based on usage and therefore makes the calls cleaner, plus it allows using anonymous types as the result selector which should be the most common usage
1 parent b6bbc0c commit 76ce058

6 files changed

Lines changed: 34 additions & 34 deletions

File tree

SharpRepository.Repository/CompositeRepository.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
namespace SharpRepository.Repository
66
{
7-
public class CompositeRepository<T, TKey> : LinqRepositoryBase<T, TKey>, IRepositoryQueryable<T, TKey> where T : class, new()
7+
// right now int is hard coded but it's sloppy and need to fix this inheritance
8+
public class CompositeRepository<T> : LinqRepositoryBase<T, int> where T : class
89
{
910
private readonly IQueryable<T> _baseQuery;
1011

SharpRepository.Repository/IRepository.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq.Expressions;
24
using SharpRepository.Repository.Transactions;
35

46
namespace SharpRepository.Repository
@@ -8,8 +10,24 @@ namespace SharpRepository.Repository
810
/// </summary>
911
/// <typeparam name="T">The entity type that the repository acts on.</typeparam>
1012
/// <typeparam name="TKey">The type of the primary key.</typeparam>
11-
public interface IRepository<T, in TKey> : IRepositoryQueryable<T, TKey> where T : class
13+
public interface IRepository<T, in TKey> : IRepositoryQueryable<T> where T : class
1214
{
15+
/// <summary>
16+
/// Gets the specified entity of type <typeparamref name="T"/> from the repository by the primary key.
17+
/// </summary>
18+
/// <param name="key">The primary key.</param>
19+
/// <returns>The entity that matches on the primary key</returns>
20+
T Get(TKey key);
21+
22+
/// <summary>
23+
/// Gets the specified entity of type <typeparamref name="T"/> from the repository by the primary key and maps it to the result of type <typeparamref name="TResult"/>.
24+
/// </summary>
25+
/// <typeparam name="TResult">The type of the result.</typeparam>
26+
/// <param name="key">The primary key.</param>
27+
/// <param name="selector">The mapping selector that returns the result type.</param>
28+
/// <returns>The mapped entity based on the selector that matches on the primary key.</returns>
29+
TResult Get<TResult>(TKey key, Expression<Func<T, TResult>> selector);
30+
1331
/// <summary>
1432
/// Adds the specified entity.
1533
/// </summary>

SharpRepository.Repository/IRepositoryQueryable.cs

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,13 @@
77

88
namespace SharpRepository.Repository
99
{
10-
public interface IRepositoryQueryable<T, in TKey> : IDisposable where T : class
10+
public interface IRepositoryQueryable<T> : IDisposable where T : class
1111
{
1212
IQueryable<T> AsQueryable(); // don't want this public, just for POC
1313

14-
IRepositoryQueryable<TResult, TResultKey> Join<TOuterKey, TInner, TResult, TResultKey>(IRepositoryQueryable<TInner, TOuterKey> innerRepository, Expression<Func<T, TOuterKey>> outerKeySelector, Expression<Func<TInner, TOuterKey>> innerKeySelector, Expression<Func<T, TInner, TResult>> resultSelector)
14+
IRepositoryQueryable<TResult> Join<TJoinKey, TInner, TResult>(IRepositoryQueryable<TInner> innerRepository, Expression<Func<T, TJoinKey>> outerKeySelector, Expression<Func<TInner, TJoinKey>> innerKeySelector, Expression<Func<T, TInner, TResult>> resultSelector)
1515
where TInner : class
16-
where TResult : class, new();
17-
18-
/// <summary>
19-
/// Gets the specified entity of type <typeparamref name="T"/> from the repository by the primary key.
20-
/// </summary>
21-
/// <param name="key">The primary key.</param>
22-
/// <returns>The entity that matches on the primary key</returns>
23-
T Get(TKey key);
24-
25-
/// <summary>
26-
/// Gets the specified entity of type <typeparamref name="T"/> from the repository by the primary key and maps it to the result of type <typeparamref name="TResult"/>.
27-
/// </summary>
28-
/// <typeparam name="TResult">The type of the result.</typeparam>
29-
/// <param name="key">The primary key.</param>
30-
/// <param name="selector">The mapping selector that returns the result type.</param>
31-
/// <returns>The mapped entity based on the selector that matches on the primary key.</returns>
32-
TResult Get<TResult>(TKey key, Expression<Func<T, TResult>> selector);
16+
where TResult : class;
3317

3418
/// <summary>
3519
/// Gets all entities from the repository.

SharpRepository.Repository/LinqRepositoryBase.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
namespace SharpRepository.Repository
1111
{
12-
public abstract class LinqRepositoryBase<T, TKey> : RepositoryBase<T, TKey> where T : class, new()
12+
public abstract class LinqRepositoryBase<T, TKey> : RepositoryBase<T, TKey> where T : class
1313
{
1414
protected LinqRepositoryBase(ICachingStrategy<T, TKey> cachingStrategy = null) : base(cachingStrategy)
1515
{
@@ -75,14 +75,14 @@ protected override IEnumerable<T> FindAllQuery(ISpecification<T> criteria, IQuer
7575
return queryOptions.Apply(query).ToList();
7676
}
7777

78-
public override IRepositoryQueryable<TResult, TResultKey> Join<TOuterKey, TInner, TResult, TResultKey>(IRepositoryQueryable<TInner, TOuterKey> innerRepository, Expression<Func<T, TOuterKey>> outerKeySelector, Expression<Func<TInner, TOuterKey>> innerKeySelector, Expression<Func<T, TInner, TResult>> resultSelector)
78+
public override IRepositoryQueryable<TResult> Join<TJoinKey, TInner, TResult>(IRepositoryQueryable<TInner> innerRepository, Expression<Func<T, TJoinKey>> outerKeySelector, Expression<Func<TInner, TJoinKey>> innerKeySelector, Expression<Func<T, TInner, TResult>> resultSelector)
7979
{
8080
var innerQuery = innerRepository.AsQueryable();
8181
var outerQuery = BaseQuery();
8282

8383
var resultQuery = outerQuery.Join(innerQuery, outerKeySelector, innerKeySelector, resultSelector);
8484

85-
return new CompositeRepository<TResult, TResultKey>(resultQuery);
85+
return new CompositeRepository<TResult>(resultQuery);
8686
}
8787
}
8888
}

SharpRepository.Repository/RepositoryBase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ public IEnumerable<TResult> GetAll<TResult>(Expression<Func<T, TResult>> selecto
7979
// These are the actual implementation that the derived class needs to implement
8080
protected abstract T GetQuery(TKey key);
8181

82-
public abstract IRepositoryQueryable<TResult, TResultKey> Join<TOuterKey, TInner, TResult, TResultKey>(IRepositoryQueryable<TInner, TOuterKey> innerRepository, Expression<Func<T, TOuterKey>> outerKeySelector, Expression<Func<TInner, TOuterKey>> innerKeySelector, Expression<Func<T, TInner, TResult>> resultSelector)
82+
public abstract IRepositoryQueryable<TResult> Join<TJoinKey, TInner, TResult>(IRepositoryQueryable<TInner> innerRepository, Expression<Func<T, TJoinKey>> outerKeySelector, Expression<Func<TInner, TJoinKey>> innerKeySelector, Expression<Func<T, TInner, TResult>> resultSelector)
8383
where TInner : class
84-
where TResult : class, new();
84+
where TResult : class;
8585

8686
public T Get(TKey key)
8787
{

SharpRepository.Tests.Integration/RepositoryJoinTests.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
1+
using System.Linq;
52
using NUnit.Framework;
63
using SharpRepository.Repository;
74
using SharpRepository.Tests.Integration.TestAttributes;
@@ -26,9 +23,9 @@ public void Join_GetAll_Should_Return_All_Items(IRepository<Contact, int> reposi
2623
var contactTypeRepository = new InMemoryRepository<ContactType, int>();
2724
contactTypeRepository.Add(new ContactType() { ContactTypeId = 1, Abbreviation = "T1"});
2825
contactTypeRepository.Add(new ContactType() { ContactTypeId = 2, Abbreviation = "T2" });
29-
30-
var compositeRepos = repository.Join<int, ContactType, ContactTypeResult, int>(contactTypeRepository, c => c.ContactTypeId, ct => ct.ContactTypeId,
31-
(c, ct) => new ContactTypeResult { Id = c.ContactId, Name = c.Name, TypeAbbrev = ct.Abbreviation });
26+
27+
var compositeRepos = repository.Join(contactTypeRepository, c => c.ContactTypeId, ct => ct.ContactTypeId,
28+
(c, ct) => new { Id = c.ContactId, Name = c.Name, TypeAbbrev = ct.Abbreviation });
3229

3330
var all = compositeRepos.GetAll().ToList();
3431

0 commit comments

Comments
 (0)