Skip to content

Commit

Permalink
GetMany and GetManyAsDictionary
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff Treuting committed Apr 10, 2014
1 parent 9420ab2 commit 332f488
Show file tree
Hide file tree
Showing 6 changed files with 205 additions and 2 deletions.
31 changes: 31 additions & 0 deletions SharpRepository.RavenDbRepository/RavenDbRepositoryBase.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Raven.Client;
Expand Down Expand Up @@ -71,6 +72,36 @@ protected override T GetQuery(TKey key)
return base.GetQuery(key);
}

public override IEnumerable<T> GetMany(params TKey[] keys)
{
return GetMany(keys.ToList());
}

public override IEnumerable<T> GetMany(IEnumerable<TKey> keys)
{
return keys.Select(Get);
}

public override IEnumerable<TResult> GetMany<TResult>(Expression<Func<T, TResult>> selector, params TKey[] keys)
{
return GetMany(keys.ToList(), selector);
}

public override IEnumerable<TResult> GetMany<TResult>(IEnumerable<TKey> keys, Expression<Func<T, TResult>> selector)
{
return keys.Select(x => Get(x, selector));
}

public override IDictionary<TKey, T> GetManyAsDictionary(params TKey[] keys)
{
return GetManyAsDictionary(keys.ToList());
}

public override IDictionary<TKey, T> GetManyAsDictionary(IEnumerable<TKey> keys)
{
return GetMany(keys).ToDictionary(GetPrimaryKey);
}

public override TResult Min<TResult>(ISpecification<T> criteria, Expression<Func<T, TResult>> selector)
{
var pagingOptions = new PagingOptions<T, TResult>(1, 1, selector);
Expand Down
30 changes: 30 additions & 0 deletions SharpRepository.Repository/ConfigurationBasedRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,36 @@ public TResult Get<TResult>(TKey key, Expression<Func<T, TResult>> selector)
return Repository.Get(key, selector);
}

public IEnumerable<T> GetMany(params TKey[] keys)
{
return Repository.GetMany(keys);
}

public IEnumerable<T> GetMany(IEnumerable<TKey> keys)
{
return Repository.GetMany(keys);
}

public IEnumerable<TResult> GetMany<TResult>(Expression<Func<T, TResult>> selector, params TKey[] keys)
{
return Repository.GetMany(selector, keys);
}

public IEnumerable<TResult> GetMany<TResult>(IEnumerable<TKey> keys, Expression<Func<T, TResult>> selector)
{
return Repository.GetMany(keys, selector);
}

public IDictionary<TKey, T> GetManyAsDictionary(params TKey[] keys)
{
return Repository.GetManyAsDictionary(keys);
}

public IDictionary<TKey, T> GetManyAsDictionary(IEnumerable<TKey> keys)
{
return Repository.GetManyAsDictionary(keys);
}

public bool Exists(TKey key)
{
T entity;
Expand Down
22 changes: 22 additions & 0 deletions SharpRepository.Repository/ICrudRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,28 @@ public interface ICrudRepository<T, TKey> : IRepositoryBase<T> where T : class
/// <returns>The mapped entity based on the selector that matches on the primary key.</returns>
TResult Get<TResult>(TKey key, Expression<Func<T, TResult>> selector);

/// <summary>
/// Gets the specified entity of type <typeparamref name="T"/> from the repository by the primary key.
/// </summary>
/// <param name="keys">The primary keys.</param>
/// <returns>The entity that matches on the primary key</returns>
IEnumerable<T> GetMany(params TKey[] keys);

/// <summary>
/// Gets the specified entity of type <typeparamref name="T"/> from the repository by the primary key.
/// </summary>
/// <param name="keys">The primary keys.</param>
/// <returns>The entity that matches on the primary key</returns>
IEnumerable<T> GetMany(IEnumerable<TKey> keys);

IEnumerable<TResult> GetMany<TResult>(Expression<Func<T, TResult>> selector, params TKey[] keys);

IEnumerable<TResult> GetMany<TResult>(IEnumerable<TKey> keys, Expression<Func<T, TResult>> selector);

IDictionary<TKey, T> GetManyAsDictionary(params TKey[] keys);

IDictionary<TKey, T> GetManyAsDictionary(IEnumerable<TKey> keys);

/// <summary>
/// Returns true if the specified entity of type <typeparamref name="T"/> from the repository by the primary key exists
/// </summary>
Expand Down
51 changes: 51 additions & 0 deletions SharpRepository.Repository/RepositoryBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,36 @@ public TResult Get<TResult>(TKey key, Expression<Func<T, TResult>> selector)
}
}

public virtual IEnumerable<T> GetMany(params TKey[] keys)
{
return GetMany(keys.ToList());
}

public virtual IEnumerable<T> GetMany(IEnumerable<TKey> keys)
{
return FindAll(ByMultipleKeysSpecification(keys));
}

public virtual IEnumerable<TResult> GetMany<TResult>(Expression<Func<T, TResult>> selector, params TKey[] keys)
{
return GetMany(keys.ToList(), selector);
}

public virtual IEnumerable<TResult> GetMany<TResult>(IEnumerable<TKey> keys, Expression<Func<T, TResult>> selector)
{
return FindAll(ByMultipleKeysSpecification(keys), selector);
}

public virtual IDictionary<TKey, T> GetManyAsDictionary(params TKey[] keys)
{
return GetManyAsDictionary(keys.ToList());
}

public virtual IDictionary<TKey, T> GetManyAsDictionary(IEnumerable<TKey> keys)
{
return GetMany(keys).ToDictionary(GetPrimaryKey);
}

public bool Exists(TKey key)
{
T entity;
Expand Down Expand Up @@ -1426,6 +1456,27 @@ protected virtual ISpecification<T> ByPrimaryKeySpecification(TKey key)
return new Specification<T>(lambda);
}

protected virtual ISpecification<T> ByMultipleKeysSpecification(IEnumerable<TKey> keys)
{
var propInfo = GetPrimaryKeyPropertyInfo();
if (propInfo == null || keys == null)
return null;

var parameter = Expression.Parameter(typeof(T), "x");

return keys.Select(key =>
Expression.Lambda<Func<T, bool>>(
Expression.Equal(
Expression.PropertyOrField(parameter, propInfo.Name),
Expression.Constant(key)
), parameter
)
)
.Aggregate<Expression<Func<T, bool>>, ISpecification<T>>(null,
(current, lambda) => current == null ? new Specification<T>(lambda) : current.Or(lambda)
);
}

protected virtual PropertyInfo GetPrimaryKeyPropertyInfo()
{
// checks for properties in this order that match TKey type
Expand Down
9 changes: 8 additions & 1 deletion SharpRepository.Repository/Traits/ICanGet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,18 @@ namespace SharpRepository.Repository.Traits
/// </summary>
/// <typeparam name="T">Generic repository entity type</typeparam>
/// <typeparam name="TKey">Generic repository entity key type</typeparam>
public interface ICanGet<T, in TKey>
public interface ICanGet<T, TKey>
{
T Get(TKey key);
TResult Get<TResult>(TKey key, Expression<Func<T, TResult>> selector);

IEnumerable<T> GetMany(params TKey[] keys);
IEnumerable<T> GetMany(IEnumerable<TKey> keys);
IEnumerable<TResult> GetMany<TResult>(Expression<Func<T, TResult>> selector, params TKey[] keys);
IEnumerable<TResult> GetMany<TResult>(IEnumerable<TKey> keys, Expression<Func<T, TResult>> selector);
IDictionary<TKey, T> GetManyAsDictionary(params TKey[] keys);
IDictionary<TKey, T> GetManyAsDictionary(IEnumerable<TKey> keys);

bool Exists(TKey key);
bool TryGet(TKey key, out T entity);
bool TryGet<TResult>(TKey key, Expression<Func<T, TResult>> selector, out TResult entity);
Expand Down
64 changes: 63 additions & 1 deletion SharpRepository.Tests.Integration/RepositoryGetTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Linq;
using NUnit.Framework;
using SharpRepository.Repository;
using SharpRepository.Tests.Integration.TestAttributes;
Expand Down Expand Up @@ -109,9 +110,70 @@ public void TryGet_Should_Return_True_If_Item_Exists(IRepository<Contact, string
}

[ExecuteForAllRepositories]
public void TryGet_Should_Return_Falsel_If_Item_Does_Not_Exists(IRepository<Contact, string> repository)
public void TryGet_Should_Return_False_If_Item_Does_Not_Exists(IRepository<Contact, string> repository)
{
repository.Exists(string.Empty).ShouldBeFalse();
}

[ExecuteForAllRepositories]
public void GetMany_Params_Should_Return_Multiple_Items(IRepository<Contact, string> repository)
{
for (var i = 1; i <= 5; i++)
{
var contact = new Contact { ContactId = i.ToString(), Name = "Test User " + i};
repository.Add(contact);
}

var items = repository.GetMany("1", "3", "4", "5");
items.Count().ShouldEqual(4);
}

[ExecuteForAllRepositories]
public void GetMany_List_Should_Return_Multiple_Items(IRepository<Contact, string> repository)
{
for (var i = 1; i <= 5; i++)
{
var contact = new Contact { ContactId = i.ToString(), Name = "Test User " + i};
repository.Add(contact);
}

var items = repository.GetMany(new [] {"1", "3", "4", "5" }.ToList());
items.Count().ShouldEqual(4);
}

[ExecuteForAllRepositories]
public void GetManyAsDictionary_Params_Should_Return_Multiple_Items(IRepository<Contact, string> repository)
{
for (var i = 1; i <= 5; i++)
{
var contact = new Contact { ContactId = i.ToString(), Name = "Test User " + i};
repository.Add(contact);
}

var items = repository.GetManyAsDictionary("1", "3", "4", "5");
items.Count().ShouldEqual(4);
items.ContainsKey("1").ShouldBeTrue();
items.ContainsKey("2").ShouldBeFalse();
items.ContainsKey("3").ShouldBeTrue();
items.ContainsKey("4").ShouldBeTrue();
items.ContainsKey("5").ShouldBeTrue();
}

[ExecuteForAllRepositories]
public void GetManyAsDictionary_List_Should_Return_Multiple_Items(IRepository<Contact, string> repository)
{
for (var i = 1; i <= 5; i++)
{
var contact = new Contact { ContactId = i.ToString(), Name = "Test User " + i};
repository.Add(contact);
}

var items = repository.GetManyAsDictionary(new [] {"1", "3", "4", "5" }.ToList());
items.ContainsKey("1").ShouldBeTrue();
items.ContainsKey("2").ShouldBeFalse();
items.ContainsKey("3").ShouldBeTrue();
items.ContainsKey("4").ShouldBeTrue();
items.ContainsKey("5").ShouldBeTrue();
}
}
}

0 comments on commit 332f488

Please sign in to comment.