Skip to content

Commit

Permalink
Merge pull request EduardoPires#24 from AlbertoMonteiro/master
Browse files Browse the repository at this point in the history
Using ProjectTo from AutoMapper
  • Loading branch information
EduardoPires authored Aug 9, 2017
2 parents 87ab1a7 + 9674ce3 commit 095ce0e
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 23 deletions.
9 changes: 5 additions & 4 deletions src/Equinox.Application/Services/CustomerAppService.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using AutoMapper;
using AutoMapper.QueryableExtensions;
using Equinox.Application.EventSourcedNormalizers;
using Equinox.Application.Interfaces;
using Equinox.Application.ViewModels;
Expand All @@ -18,9 +19,9 @@ public class CustomerAppService : ICustomerAppService
private readonly IEventStoreRepository _eventStoreRepository;
private readonly IBus Bus;

public CustomerAppService(IMapper mapper,
ICustomerRepository customerRepository,
IBus bus,
public CustomerAppService(IMapper mapper,
ICustomerRepository customerRepository,
IBus bus,
IEventStoreRepository eventStoreRepository)
{
_mapper = mapper;
Expand All @@ -31,7 +32,7 @@ public CustomerAppService(IMapper mapper,

public IEnumerable<CustomerViewModel> GetAll()
{
return _mapper.Map<IEnumerable<CustomerViewModel>>(_customerRepository.GetAll());
return _customerRepository.GetAll().ProjectTo<CustomerViewModel>();
}

public CustomerViewModel GetById(Guid id)
Expand Down
6 changes: 2 additions & 4 deletions src/Equinox.Domain/Interfaces/IRepository.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Linq;

namespace Equinox.Domain.Interfaces
{
public interface IRepository<TEntity> : IDisposable where TEntity : class
{
void Add(TEntity obj);
TEntity GetById(Guid id);
IEnumerable<TEntity> GetAll();
IQueryable<TEntity> GetAll();
void Update(TEntity obj);
void Remove(Guid id);
IEnumerable<TEntity> Find(Expression<Func<TEntity, bool>> predicate);
int SaveChanges();
}
}
8 changes: 4 additions & 4 deletions src/Equinox.Infra.Data/Repository/CustomerRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@

namespace Equinox.Infra.Data.Repository
{
public class CustomerRepository : Repository<Customer>, ICustomerRepository
public class CustomerRepository : Repository<Customer>, ICustomerRepository
{
public CustomerRepository(EquinoxContext context)
:base(context)
: base(context)
{

}
}

public Customer GetByEmail(string email)
{
return Find(c => c.Email == email).FirstOrDefault();
return DbSet.AsNoTracking().FirstOrDefault(c => c.Email == email);
}
}
}
15 changes: 4 additions & 11 deletions src/Equinox.Infra.Data/Repository/Repository.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Equinox.Domain.Interfaces;
using Equinox.Infra.Data.Context;
using Microsoft.EntityFrameworkCore;
Expand All @@ -10,8 +8,8 @@ namespace Equinox.Infra.Data.Repository
{
public class Repository<TEntity> : IRepository<TEntity> where TEntity : class
{
protected EquinoxContext Db;
protected DbSet<TEntity> DbSet;
protected readonly EquinoxContext Db;
protected readonly DbSet<TEntity> DbSet;

public Repository(EquinoxContext context)
{
Expand All @@ -29,9 +27,9 @@ public virtual TEntity GetById(Guid id)
return DbSet.Find(id);
}

public virtual IEnumerable<TEntity> GetAll()
public virtual IQueryable<TEntity> GetAll()
{
return DbSet.ToList();
return DbSet;
}

public virtual void Update(TEntity obj)
Expand All @@ -44,11 +42,6 @@ public virtual void Remove(Guid id)
DbSet.Remove(DbSet.Find(id));
}

public IEnumerable<TEntity> Find(Expression<Func<TEntity, bool>> predicate)
{
return DbSet.AsNoTracking().Where(predicate);
}

public int SaveChanges()
{
return Db.SaveChanges();
Expand Down

0 comments on commit 095ce0e

Please sign in to comment.