Skip to content

Commit

Permalink
perf: use query as a method
Browse files Browse the repository at this point in the history
  • Loading branch information
leynier committed May 19, 2022
1 parent 827ee4a commit 259b6c1
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 37 deletions.
5 changes: 3 additions & 2 deletions Gateways.Api/Controllers/DevicesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public DevicesController(IDeviceService service, IMapper mapper, IConfiguration
[HttpPost]
public override Response<DeviceGetModel> Post([FromBody] DevicePostModel model)
{
var count = service.Where(g => g.GatewayId == model.GatewayId).Count();
var count = service.Query().Where(g => g.GatewayId == model.GatewayId).Count();
if (count >= maxNumberOfDevicesPerGateway)
throw new BadRequestError("Maximum number of devices reached for gateway");
return base.Post(model);
Expand All @@ -34,14 +34,15 @@ public override Response<DeviceGetModel> Post([FromBody] DevicePostModel model)
public override Response<DeviceGetModel> Put(int id, [FromBody] DevicePutModel model)
{
var gatewayId = service
.Query()
.Where(g => Equals(g.Id, id))
.Select(g => g.GatewayId)
.FirstOrDefault();
if (gatewayId == null)
throw new NotFoundError();
if (gatewayId != model.GatewayId)
{
var count = service.Where(g => g.GatewayId == model.GatewayId).Count();
var count = service.Query().Where(g => g.GatewayId == model.GatewayId).Count();
if (count >= maxNumberOfDevicesPerGateway)
throw new BadRequestError("Maximum number of devices reached for gateway");
}
Expand Down
3 changes: 2 additions & 1 deletion Gateways.Business.Contracts/Contracts/IRepository.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
namespace Gateways.Business.Contracts;

public interface IRepository<TEntity> : IQueryable<TEntity> where TEntity : class
public interface IRepository<TEntity> where TEntity : class
{
IObjectContext Context { get; }
IQueryable<TEntity> Query();
void Add(TEntity entity);
void Update(TEntity entity);
void Remove(TEntity entity);
Expand Down
3 changes: 2 additions & 1 deletion Gateways.Business.Contracts/Services/IService.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
namespace Gateways.Business.Contracts.Services;

public interface IService<TEntity> : IQueryable<TEntity> where TEntity : class
public interface IService<TEntity> where TEntity : class
{
IQueryable<TEntity> Query();
void Add(TEntity entity);
void Update(TEntity entity);
void Remove(TEntity entity);
Expand Down
17 changes: 4 additions & 13 deletions Gateways.Business.Implementations/Services/BaseService.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using Gateways.Business.Contracts;
using Gateways.Business.Contracts.Services;
using System.Collections;
using System.Linq.Expressions;

namespace Gateways.Business.Implementations.Services;

Expand All @@ -13,17 +11,9 @@ public BaseService(IRepository<TEntity> repository)
}

protected IRepository<TEntity> BaseRepository { get; }

public Type ElementType => BaseRepository.ElementType;

public Expression Expression => BaseRepository.Expression;

public IQueryProvider Provider => BaseRepository.Provider;

public IEnumerator<TEntity> GetEnumerator() => BaseRepository.GetEnumerator();

IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();


public IQueryable<TEntity> Query() => BaseRepository.Query();

public void Add(TEntity entity) => BaseRepository.Add(entity);

public void Update(TEntity entity) => BaseRepository.Update(entity);
Expand All @@ -33,4 +23,5 @@ public BaseService(IRepository<TEntity> repository)
public void Commit() => BaseRepository.Commit();

public Task CommitAsync() => BaseRepository.CommitAsync();

}
8 changes: 4 additions & 4 deletions Gateways.Common/Controllers/CrudApiControllerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public virtual PaginatedResponse<IEnumerable<TGet>> GetAll([FromQuery] Paginatio
{
var skip = pagination.Page * pagination.PageSize;
var take = pagination.PageSize;
var query = service.AsNoTrackingWithIdentityResolution();
var query = service.Query().AsNoTrackingWithIdentityResolution();
if (order != null)
query = order(query);
var models = query.Skip(skip).Take(pagination.PageSize).ToList();
Expand All @@ -42,7 +42,7 @@ public virtual PaginatedResponse<IEnumerable<TGet>> GetAll([FromQuery] Paginatio
[HttpGet("{id}")]
public virtual Response<TGetDetails> Get(TKey id)
{
IQueryable<TEntity> query = service.AsNoTrackingWithIdentityResolution();
IQueryable<TEntity> query = service.Query().AsNoTrackingWithIdentityResolution();
if (includer != null)
query = includer(query);
var model = query.FirstOrDefault(g => Equals(g.Id, id));
Expand All @@ -63,7 +63,7 @@ public virtual Response<TGet> Post([FromBody] TPost model)
[HttpPut("{id}")]
public virtual Response<TGet> Put(TKey id, [FromBody] TPut model)
{
var entity = service.FirstOrDefault(g => Equals(g.Id, id));
var entity = service.Query().FirstOrDefault(g => Equals(g.Id, id));
if (entity == null)
throw new NotFoundError();
mapper.Map(model, entity);
Expand All @@ -74,7 +74,7 @@ public virtual Response<TGet> Put(TKey id, [FromBody] TPut model)
[HttpDelete("{id}")]
public virtual Response<TGet> Delete(TKey id)
{
var entity = service.FirstOrDefault(g => Equals(g.Id, id));
var entity = service.Query().FirstOrDefault(g => Equals(g.Id, id));
if (entity == null)
throw new NotFoundError();
service.Remove(entity);
Expand Down
20 changes: 4 additions & 16 deletions Gateways.Data.Implementations/Repositories/BaseRepository.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Gateways.Business.Contracts;
using System.Collections;
using System.Linq.Expressions;

namespace Gateways.Data.Implementations.Repositories;

Expand All @@ -9,29 +7,19 @@ public class BaseRepository<TEntity> : IRepository<TEntity> where TEntity : clas
public BaseRepository(IObjectContext context)
{
Context = context;
Expression = context.Query<TEntity>().Expression;
Provider = context.Query<TEntity>().Provider;
}

public IObjectContext Context { get; }

public Expression Expression { get; }

public IQueryProvider Provider { get; }

public Type ElementType => typeof(TEntity);
public IQueryable<TEntity> Query() => Context.Query<TEntity>();

public void Add(TEntity entity) => Context.Add(entity);

public void Commit() => Context.Commit();

public Task CommitAsync() => Context.CommitAsync();

public IEnumerator<TEntity> GetEnumerator() => Context.Query<TEntity>().GetEnumerator();
public void Update(TEntity entity) => Context.Update(entity);

public void Remove(TEntity entity) => Context.Remove(entity);

public void Update(TEntity entity) => Context.Update(entity);
public void Commit() => Context.Commit();

IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
public Task CommitAsync() => Context.CommitAsync();
}

0 comments on commit 259b6c1

Please sign in to comment.