Last active
October 6, 2015 13:17
-
-
Save ferventcoder/2998823 to your computer and use it in GitHub Desktop.
RepositoryPattern with Linq Query Services
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 SomeProject.Infrastructure.App.Services | |
{ | |
/// <summary> | |
/// Houses the queries against users in the database | |
/// </summary> | |
public class UserRepositoryService : BaseRepositoryService<User>, IUserRepositoryService | |
{ | |
private readonly IDateTimeService _dateTimeService; | |
/// <summary> | |
/// Initializes a new instance of the <see cref="UserRepositoryService" /> class. | |
/// </summary> | |
/// <param name="repository"> The repository. </param> | |
/// <param name="dateTimeService"> The date time service </param> | |
public UserRepositoryService(IRepository repository, IDateTimeService dateTimeService) | |
: base(repository) | |
{ | |
_dateTimeService = dateTimeService; | |
} | |
public User GetUserByEmailAddress(string email) | |
{ | |
Ensure.That(() => email).IsNotNullOrWhiteSpace(); | |
this.Log().Debug("Looking for '{0}' (email address) in the database.", email); | |
return Repository.GetAll<User>() | |
.Where(u => u.Email == email) | |
.SingleOrDefault(); | |
} | |
/// <summary> | |
/// Gets the current system user. | |
/// </summary> | |
/// <returns> | |
/// An instance of <see cref="User"/> for the currently logged in user; otherwise null | |
/// </returns> | |
public User GetCurrentSystemUser() | |
{ | |
return GetUserByEmailAddress(ApplicationParameters.GetCurrentUserName()); | |
} | |
public User GetUserByKey(Guid userKey) | |
{ | |
Ensure.That(() => userKey).IsNotEmpty(); | |
this.Log().Debug("Looking for user by guid '{0}' in the database.", userKey.ToString()); | |
return Repository | |
.GetAll<User>() | |
.SingleOrDefault(u => u.Id == userKey); | |
} | |
} | |
} |
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 SomeProject.Infrastructure.Services | |
{ | |
/// <summary> | |
/// Base class that implements most of the functionality necessary for a repository service class, minus custom queries. | |
/// </summary> | |
/// <typeparam name="T">A class that implements <see cref="IDomainObject"/> with a default constructor</typeparam> | |
public abstract class BaseRepositoryService<T> : IRepositoryService<T> | |
where T : class, IDomainObject, new() | |
{ | |
public IRepository Repository { get; private set; } | |
/// <summary> | |
/// Initializes a new instance of the <see cref="BaseRepositoryService<T>"/> class. | |
/// </summary> | |
/// <param name="repository">The repository.</param> | |
public BaseRepositoryService(IRepository repository) | |
{ | |
Repository = repository; | |
} | |
/// <summary> | |
/// Gets the specified key. | |
/// </summary> | |
/// <typeparam name="TKeyType">The type of the key.</typeparam> | |
/// <param name="key">The key.</param> | |
/// <returns></returns> | |
public T Get<TKeyType>(TKeyType key) | |
{ | |
return Repository.Get<T, TKeyType>(key); | |
} | |
public IQueryable<T> GetAll() | |
{ | |
return Repository.GetAll<T>(); | |
} | |
public object InsertOnCommit(T entity) | |
{ | |
return Repository.InsertOnCommit(entity); | |
} | |
public void DeleteOnCommit(T entity) | |
{ | |
Repository.DeleteOnCommit(entity); | |
} | |
public void CommitChanges() | |
{ | |
Repository.CommitChanges(); | |
} | |
} | |
} |
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 SomeProject.Infrastructure.Persistence | |
{ | |
/// <summary> | |
/// The repository for working with the database. | |
/// </summary> | |
public interface IRepository | |
{ | |
/// <summary> | |
/// Commits the changes. | |
/// </summary> | |
void CommitChanges(); | |
/// <summary> | |
/// Gets an instance of <see cref="T"/> with the specified key. | |
/// </summary> | |
/// <typeparam name="T">A class that implements <see cref="IDomainObject"/> with a default constructor</typeparam> | |
/// <typeparam name="TKeyType">The type of the key</typeparam> | |
/// <param name="key">The key.</param> | |
/// <returns></returns> | |
T Get<T, TKeyType>(TKeyType key) where T : class, IDomainObject, new(); | |
/// <summary> | |
/// Gets all instances of <see cref="T"/>. Can be used with a linq query to limit results | |
/// </summary> | |
/// <typeparam name="T">A class that implements <see cref="IDomainObject"/> with a default constructor</typeparam> | |
/// <returns>A list of <see cref="T"/></returns> | |
IQueryable<T> GetAll<T>() where T : class, IDomainObject, new(); | |
/// <summary> | |
/// Inserts the specified entity instance of <see cref="T" /> on commit. | |
/// </summary> | |
/// <typeparam name="T"> A class that implements <see cref="IDomainObject" /> with a default constructor </typeparam> | |
/// <param name="entity"> The entity. </param> | |
/// <returns> The Id of the inserted item</returns> | |
object InsertOnCommit<T>(T entity) where T : class, IDomainObject, new(); | |
/// <summary> | |
/// Deletes the specified entity instance of <see cref="T"/> on commit. | |
/// </summary> | |
/// <typeparam name="T">A class that implements <see cref="IDomainObject"/> with a default constructor</typeparam> | |
/// <param name="entity">The entity.</param> | |
void DeleteOnCommit<T>(T entity) where T : class, IDomainObject, new(); | |
} | |
} |
This is missing quite a bit of other required items, like the actual Repository class - and the fact that IDomainObject is necessary to make it happen.
There is only one implementation/instance of IRepository because it has generic methods for the types themselves...unless of course you want to hit serveral databases or want to mix ORMs (and non ORMs alike)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This one is a little leaned towards EF Code First so forgive the idioms that have translated over.