projection functionality
This commit is contained in:
@@ -63,6 +63,7 @@
|
|||||||
<Compile Include="Infrastructure\ITestRepository.cs" />
|
<Compile Include="Infrastructure\ITestRepository.cs" />
|
||||||
<Compile Include="Infrastructure\TestRepository.cs" />
|
<Compile Include="Infrastructure\TestRepository.cs" />
|
||||||
<Compile Include="CreateTests.cs" />
|
<Compile Include="CreateTests.cs" />
|
||||||
|
<Compile Include="ProjectTests.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
<Compile Include="ReadPartitionedTests.cs" />
|
<Compile Include="ReadPartitionedTests.cs" />
|
||||||
<Compile Include="ReadTests.cs" />
|
<Compile Include="ReadTests.cs" />
|
||||||
|
|||||||
@@ -0,0 +1,148 @@
|
|||||||
|
using IntegrationTests.Infrastructure;
|
||||||
|
using MongoDbGenericRepository.Models;
|
||||||
|
using NUnit.Framework;
|
||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace IntegrationTests
|
||||||
|
{
|
||||||
|
public class Nested
|
||||||
|
{
|
||||||
|
public DateTime SomeDate { get; set; }
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public class ProjectTestsDocument : Document
|
||||||
|
{
|
||||||
|
public ProjectTestsDocument()
|
||||||
|
{
|
||||||
|
Version = 2;
|
||||||
|
Nested = new Nested
|
||||||
|
{
|
||||||
|
SomeDate = DateTime.UtcNow
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public string SomeContent { get; set; }
|
||||||
|
|
||||||
|
public Nested Nested { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class ProjectTests : BaseMongoDbRepositoryTests<ProjectTestsDocument>
|
||||||
|
{
|
||||||
|
private class MyProjection
|
||||||
|
{
|
||||||
|
public DateTime SomeDate { get; set; }
|
||||||
|
public string SomeContent { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task ProjectOneAsync()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
const string someContent = "ProjectOneAsyncContent";
|
||||||
|
var someDate = DateTime.UtcNow;
|
||||||
|
var document = CreateTestDocument();
|
||||||
|
document.SomeContent = someContent;
|
||||||
|
document.Nested.SomeDate = someDate;
|
||||||
|
SUT.AddOne(document);
|
||||||
|
// Act
|
||||||
|
var result = await SUT.ProjectOneAsync<ProjectTestsDocument, MyProjection>(
|
||||||
|
x => x.Id == document.Id,
|
||||||
|
x => new MyProjection
|
||||||
|
{
|
||||||
|
SomeContent = x.SomeContent,
|
||||||
|
SomeDate = x.Nested.SomeDate
|
||||||
|
});
|
||||||
|
// Assert
|
||||||
|
Assert.IsNotNull(result);
|
||||||
|
Assert.AreEqual(someContent, result.SomeContent);
|
||||||
|
Assert.AreEqual(someDate.Minute, result.SomeDate.Minute);
|
||||||
|
Assert.AreEqual(someDate.Second, result.SomeDate.Second);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void ProjectOne()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
const string someContent = "ProjectOneContent";
|
||||||
|
var someDate = DateTime.UtcNow;
|
||||||
|
var document = CreateTestDocument();
|
||||||
|
document.SomeContent = someContent;
|
||||||
|
document.Nested.SomeDate = someDate;
|
||||||
|
SUT.AddOne(document);
|
||||||
|
// Act
|
||||||
|
var result = SUT.ProjectOne<ProjectTestsDocument, MyProjection>(
|
||||||
|
x => x.Id == document.Id,
|
||||||
|
x => new MyProjection
|
||||||
|
{
|
||||||
|
SomeContent = x.SomeContent,
|
||||||
|
SomeDate = x.Nested.SomeDate
|
||||||
|
});
|
||||||
|
// Assert
|
||||||
|
Assert.IsNotNull(result);
|
||||||
|
Assert.AreEqual(someContent, result.SomeContent);
|
||||||
|
Assert.AreEqual(someDate.Minute, result.SomeDate.Minute);
|
||||||
|
Assert.AreEqual(someDate.Second, result.SomeDate.Second);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public async Task ProjectManyAsync()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
const string someContent = "ProjectManyAsyncContent";
|
||||||
|
var someDate = DateTime.UtcNow;
|
||||||
|
var document = CreateTestDocuments(5);
|
||||||
|
document.ForEach(e =>
|
||||||
|
{
|
||||||
|
e.SomeContent = someContent;
|
||||||
|
e.Nested.SomeDate = someDate;
|
||||||
|
});
|
||||||
|
|
||||||
|
SUT.AddMany(document);
|
||||||
|
// Act
|
||||||
|
var result = await SUT.ProjectManyAsync<ProjectTestsDocument, MyProjection>(
|
||||||
|
x => x.SomeContent == someContent,
|
||||||
|
x => new MyProjection
|
||||||
|
{
|
||||||
|
SomeContent = x.SomeContent,
|
||||||
|
SomeDate = x.Nested.SomeDate
|
||||||
|
});
|
||||||
|
// Assert
|
||||||
|
Assert.AreEqual(5, result.Count);
|
||||||
|
Assert.AreEqual(someContent, result.First().SomeContent);
|
||||||
|
Assert.AreEqual(someDate.Minute, result.First().SomeDate.Minute);
|
||||||
|
Assert.AreEqual(someDate.Second, result.First().SomeDate.Second);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void ProjectMany()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
const string someContent = "ProjectManyContent";
|
||||||
|
var someDate = DateTime.UtcNow;
|
||||||
|
var document = CreateTestDocuments(5);
|
||||||
|
document.ForEach(e =>
|
||||||
|
{
|
||||||
|
e.SomeContent = someContent;
|
||||||
|
e.Nested.SomeDate = someDate;
|
||||||
|
});
|
||||||
|
|
||||||
|
SUT.AddMany(document);
|
||||||
|
// Act
|
||||||
|
var result = SUT.ProjectMany<ProjectTestsDocument, MyProjection>(
|
||||||
|
x => x.SomeContent == someContent,
|
||||||
|
x => new MyProjection
|
||||||
|
{
|
||||||
|
SomeContent = x.SomeContent,
|
||||||
|
SomeDate = x.Nested.SomeDate
|
||||||
|
});
|
||||||
|
// Assert
|
||||||
|
Assert.AreEqual(5, result.Count);
|
||||||
|
Assert.AreEqual(someContent, result.First().SomeContent);
|
||||||
|
Assert.AreEqual(someDate.Minute, result.First().SomeDate.Minute);
|
||||||
|
Assert.AreEqual(someDate.Second, result.First().SomeDate.Second);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -227,6 +227,58 @@ namespace MongoDbGenericRepository
|
|||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
#region Project
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously returns a projected document matching the filter condition.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument"></typeparam>
|
||||||
|
/// <typeparam name="TProjection"></typeparam>
|
||||||
|
/// <param name="filter"></param>
|
||||||
|
/// <param name="projection">The projection expression.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
Task<TProjection> ProjectOneAsync<TDocument, TProjection>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TProjection>> projection, string partitionKey = null)
|
||||||
|
where TDocument : IDocument
|
||||||
|
where TProjection : class;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns a projected document matching the filter condition.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument"></typeparam>
|
||||||
|
/// <typeparam name="TProjection"></typeparam>
|
||||||
|
/// <param name="filter"></param>
|
||||||
|
/// <param name="projection">The projection expression.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
TProjection ProjectOne<TDocument, TProjection>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TProjection>> projection, string partitionKey = null)
|
||||||
|
where TDocument : IDocument
|
||||||
|
where TProjection : class;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously returns a list of projected documents matching the filter condition.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument"></typeparam>
|
||||||
|
/// <typeparam name="TProjection"></typeparam>
|
||||||
|
/// <param name="filter"></param>
|
||||||
|
/// <param name="projection">The projection expression.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
Task<List<TProjection>> ProjectManyAsync<TDocument, TProjection>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TProjection>> projection, string partitionKey = null)
|
||||||
|
where TDocument : IDocument
|
||||||
|
where TProjection : class;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously returns a list of projected documents matching the filter condition.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument"></typeparam>
|
||||||
|
/// <typeparam name="TProjection"></typeparam>
|
||||||
|
/// <param name="filter"></param>
|
||||||
|
/// <param name="projection">The projection expression.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
List<TProjection> ProjectMany<TDocument, TProjection>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TProjection>> projection, string partitionKey = null)
|
||||||
|
where TDocument : IDocument
|
||||||
|
where TProjection : class;
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -577,27 +629,77 @@ namespace MongoDbGenericRepository
|
|||||||
|
|
||||||
#endregion Delete
|
#endregion Delete
|
||||||
|
|
||||||
#region Projection
|
#region Project
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Asynchronously returns a list of projected document matching the filter condition.
|
/// Asynchronously returns a projected document matching the filter condition.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="TDocument"></typeparam>
|
/// <typeparam name="TDocument"></typeparam>
|
||||||
/// <typeparam name="TProjection"></typeparam>
|
/// <typeparam name="TProjection"></typeparam>
|
||||||
/// <param name="filter"></param>
|
/// <param name="filter"></param>
|
||||||
/// <param name="projection">The projection expression.</param>
|
/// <param name="projection">The projection expression.</param>
|
||||||
/// <param name="partitionKey">An optional partition key.</param>
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
public async Task<TProjection> GetProjectedAsync<TDocument, TProjection>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TProjection>> projection, string partitionKey = null)
|
public async Task<TProjection> ProjectOneAsync<TDocument, TProjection>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TProjection>> projection, string partitionKey = null)
|
||||||
where TDocument : IDocument
|
where TDocument : IDocument
|
||||||
where TProjection : class, new()
|
where TProjection : class
|
||||||
{
|
{
|
||||||
return await HandlePartitioned<TDocument>(partitionKey).Find(filter)
|
return await HandlePartitioned<TDocument>(partitionKey).Find(filter)
|
||||||
.Project(projection)
|
.Project(projection)
|
||||||
.FirstOrDefaultAsync();
|
.FirstOrDefaultAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
/// <summary>
|
||||||
|
/// Returns a projected document matching the filter condition.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument"></typeparam>
|
||||||
|
/// <typeparam name="TProjection"></typeparam>
|
||||||
|
/// <param name="filter"></param>
|
||||||
|
/// <param name="projection">The projection expression.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
public TProjection ProjectOne<TDocument, TProjection>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TProjection>> projection, string partitionKey = null)
|
||||||
|
where TDocument : IDocument
|
||||||
|
where TProjection : class
|
||||||
|
{
|
||||||
|
return HandlePartitioned<TDocument>(partitionKey).Find(filter)
|
||||||
|
.Project(projection)
|
||||||
|
.FirstOrDefault();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously returns a list of projected documents matching the filter condition.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument"></typeparam>
|
||||||
|
/// <typeparam name="TProjection"></typeparam>
|
||||||
|
/// <param name="filter"></param>
|
||||||
|
/// <param name="projection">The projection expression.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
public async Task<List<TProjection>> ProjectManyAsync<TDocument, TProjection>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TProjection>> projection, string partitionKey = null)
|
||||||
|
where TDocument : IDocument
|
||||||
|
where TProjection : class
|
||||||
|
{
|
||||||
|
return await HandlePartitioned<TDocument>(partitionKey).Find(filter)
|
||||||
|
.Project(projection)
|
||||||
|
.ToListAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Asynchronously returns a list of projected documents matching the filter condition.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="TDocument"></typeparam>
|
||||||
|
/// <typeparam name="TProjection"></typeparam>
|
||||||
|
/// <param name="filter"></param>
|
||||||
|
/// <param name="projection">The projection expression.</param>
|
||||||
|
/// <param name="partitionKey">An optional partition key.</param>
|
||||||
|
public List<TProjection> ProjectMany<TDocument, TProjection>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TProjection>> projection, string partitionKey = null)
|
||||||
|
where TDocument : IDocument
|
||||||
|
where TProjection : class
|
||||||
|
{
|
||||||
|
return HandlePartitioned<TDocument>(partitionKey).Find(filter)
|
||||||
|
.Project(projection)
|
||||||
|
.ToList();
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Asynchronously returns a paginated list of the documents matching the filter condition.
|
/// Asynchronously returns a paginated list of the documents matching the filter condition.
|
||||||
|
|||||||
Reference in New Issue
Block a user