Skip to content

Commit

Permalink
Select fixes (#5)
Browse files Browse the repository at this point in the history
* Updated QueryBuilder for Select operations
* Added GetFirstWithSelectAsync()
  • Loading branch information
TieDyeGeek authored Nov 24, 2020
1 parent 7c77a94 commit d067635
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 11 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public async Task SpecifyReturn()
.Select(x => x.Name)
.Build();

var nameOfInactiveUsers = await _repository.GetAllAsync(nameOfInactiveUsersQuery);
var nameOfInactiveUsers = await _repository.GetAllWithSelectAsync(nameOfInactiveUsersQuery);
}
```
Queries can be saved through concrete classes and reused later:
Expand All @@ -106,7 +106,7 @@ public class UserQueries
.Build();
}

public IQuery<User> GetAllInactiveUsers()
public IQuery<User> GetAllInactiveUsersWithDepartment()
{
return new QueryBuilder<User>()
.Where(x => x.IsActive == false)
Expand All @@ -117,4 +117,4 @@ public class UserQueries
```
---

CSE Software Inc. is a privately held company founded in 1990. CSE develops software, AR/VR, simulation, mobile, and web technology solutions. The company also offers live, 24x7, global help desk services in 110 languages. All CSE teams are U.S. based with experience in multiple industries, including government, military, healthcare, construction, agriculture, mining, and more. CSE Software is a certified women-owned small business. Visit us online at [csesoftware.com](csesoftware.com).
CSE Software Inc. is a privately held company founded in 1990. CSE develops software, AR/VR, simulation, mobile, and web technology solutions. The company also offers live, 24x7, global help desk services in 110 languages. All CSE teams are U.S. based with experience in multiple industries, including government, military, healthcare, construction, agriculture, mining, and more. CSE Software is a certified women-owned small business. Visit us online at [csesoftware.com](https://www.csesoftware.com).
1 change: 0 additions & 1 deletion src/CSESoftware.Repository/Builder/Query.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ public class Query<TEntity> : IQuery<TEntity> where TEntity : class, IEntity
public Expression<Func<TEntity, bool>> Predicate { get; set; }
public Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> OrderBy { get; set; }
public List<Expression<Func<TEntity, object>>> Include { get; set; }
public Expression<Func<TEntity, object>> Select { get; set; }
public int? Skip { get; set; }
public int? Take { get; set; }
public CancellationToken CancellationToken { get; set; }
Expand Down
7 changes: 3 additions & 4 deletions src/CSESoftware.Repository/Builder/QueryBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,9 @@ public QueryBuilder<TEntity> Include(Expression<Func<TEntity, object>> include)
return this;
}

public QueryBuilder<TEntity> Select(Expression<Func<TEntity, object>> select)
public SelectQueryBuilder<TEntity, TOut> Select<TOut>(Expression<Func<TEntity, TOut>> select)
{
_entity.Select = select;
return this;
return new SelectQueryBuilder<TEntity, TOut>(_entity, select);
}

public QueryBuilder<TEntity> Skip(int? skip)
Expand All @@ -71,7 +70,7 @@ public QueryBuilder<TEntity> Take(int? take)
return this;
}

public QueryBuilder<TEntity> WithThisCancellationTokenToken(CancellationToken token)
public QueryBuilder<TEntity> WithThisCancellationToken(CancellationToken token)
{
_entity.CancellationToken = token;
return this;
Expand Down
21 changes: 21 additions & 0 deletions src/CSESoftware.Repository/Builder/QueryWithSelect.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Linq.Expressions;
using CSESoftware.Core.Entity;

namespace CSESoftware.Repository.Builder
{
public class QueryWithSelect<TEntity, TOut> : Query<TEntity>, IQueryWithSelect<TEntity, TOut> where TEntity : class, IEntity
{
public QueryWithSelect(IQuery<TEntity> query)
{
Predicate = query.Predicate;
OrderBy = query.OrderBy;
Include = query.Include;
Skip = query.Skip;
Take = query.Take;
CancellationToken = query.CancellationToken;
}

public Expression<Func<TEntity, TOut>> Select { get; set; }
}
}
29 changes: 29 additions & 0 deletions src/CSESoftware.Repository/Builder/SelectQueryBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Linq.Expressions;
using CSESoftware.Core.Entity;

namespace CSESoftware.Repository.Builder
{
public class SelectQueryBuilder<TEntity, TOut> where TEntity : class, IEntity
{
internal readonly IQueryWithSelect<TEntity, TOut> _entity;

public SelectQueryBuilder(IQuery<TEntity> entity)
{
_entity = new QueryWithSelect<TEntity, TOut>(entity);
}

public SelectQueryBuilder(IQuery<TEntity> entity, Expression<Func<TEntity, TOut>> select)
{
_entity = new QueryWithSelect<TEntity, TOut>(entity)
{
Select = select
};
}

public IQueryWithSelect<TEntity, TOut> Build()
{
return _entity;
}
}
}
2 changes: 1 addition & 1 deletion src/CSESoftware.Repository/CSESoftware.Repository.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>CSE Software, Inc.</Authors>
<Copyright>2020 CSE Software, Inc.</Copyright>
<Version>2.0.0</Version>
<Version>2.1.0</Version>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
<Description>A generic repository pattern.</Description>
<PackageIcon>packageIcon.png</PackageIcon>
Expand Down
1 change: 0 additions & 1 deletion src/CSESoftware.Repository/IQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ public interface IQuery<TEntity> where TEntity : class, IEntity
Expression<Func<TEntity, bool>> Predicate { get; set; }
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> OrderBy { get; set; }
List<Expression<Func<TEntity, object>>> Include { get; set; }
Expression<Func<TEntity, object>> Select { get; set; }
int? Skip { get; set; }
int? Take { get; set; }
CancellationToken CancellationToken { get; set; }
Expand Down
11 changes: 11 additions & 0 deletions src/CSESoftware.Repository/IQueryWithSelect.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using System.Linq.Expressions;
using CSESoftware.Core.Entity;

namespace CSESoftware.Repository
{
public interface IQueryWithSelect<TEntity, TOut> : IQuery<TEntity> where TEntity : class, IEntity
{
Expression<Func<TEntity, TOut>> Select { get; set; }
}
}
5 changes: 4 additions & 1 deletion src/CSESoftware.Repository/IReadOnlyRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ Task<bool> GetExistsAsync<TEntity>(IQuery<TEntity> filter)
Task<bool> GetExistsAsync<TEntity>(Expression<Func<TEntity, bool>> filter = null)
where TEntity : class, IEntity;

Task<List<TOut>> GetAllWithSelectAsync<TEntity, TOut>(IQuery<TEntity> filter = null)
Task<List<TOut>> GetAllWithSelectAsync<TEntity, TOut>(IQueryWithSelect<TEntity, TOut> filter = null)
where TEntity : class, IEntity;

Task<TOut> GetFirstWithSelectAsync<TEntity, TOut>(IQueryWithSelect<TEntity, TOut> filter = null)
where TEntity : class, IEntity;
}
}

0 comments on commit d067635

Please sign in to comment.