Created
October 15, 2013 16:08
-
-
Save Boggin/b2fdc06d8e2e21e27e26 to your computer and use it in GitHub Desktop.
Specification pattern for repository.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Demo.Repository.Specifications | |
{ | |
public class IssueByNameSpecification : Specification<Issue> | |
{ | |
private readonly string key; | |
public IssueByNameSpecification(string key) | |
{ | |
this.key = key; | |
} | |
public override Expression<Func<Issue, bool>> Predicate | |
{ | |
get { return p => p.Name.Equals(this.key); } | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Upstream.Repository | |
{ | |
public class IssueRepository : GenericRepository<Issue>, IIssueRepository | |
{ | |
public IssueRepository(IUnitOfWork unitOfWork) : base(unitOfWork) | |
{ | |
} | |
public IEnumerable<Issue> Find(params Specification<Issue>[] specifications) | |
{ | |
var issues = | |
this.UnitOfWork.ApplicationContext.Issues | |
.Include(i => i.AdditionalCountries); | |
return issues.Where(specifications[0].Predicate); | |
// TODO: sort out syntax for fluent interface to Find(). | |
// var set = specifications.Aggregate(issues, (current, spec) => (DbSet<Issue>)current.Where(spec.Predicate)); | |
// return set; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Demo.Repository | |
{ | |
public static class IssueSpecification | |
{ | |
public static Specification<Issue> ById(int key) | |
{ | |
return new IssueByIdSpecification(key); | |
} | |
public static Specification<Issue> ByName(string key) | |
{ | |
return new IssueByNameSpecification(key); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Upstream.Repository.Business | |
{ | |
public class Query : IQuery | |
{ | |
public Query(IIssueRepository issueRepository) | |
{ | |
this.IssueRepository = issueRepository; | |
} | |
protected IIssueRepository IssueRepository { get; set; } | |
public IssueDto GetIssue(string name) | |
{ | |
var issue = this.IssueRepository.Find(IssueSpecification.ByName(name)).FirstOrDefault(); | |
var issueDto = Mapper.Map<IssueDto>(issue); | |
return issueDto; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Demo.Repository.Infrastructure | |
{ | |
public abstract class Specification<TEntity> | |
{ | |
// TODO: make this a composite specification pattern. | |
public abstract Expression<Func<TEntity, bool>> Predicate { get; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment