Skip to content

Instantly share code, notes, and snippets.

View aidanmorgan's full-sized avatar

Aidan Morgan aidanmorgan

View GitHub Profile
namespace WhiskeyRecommender.Persistence
{
/// <summary>
/// A simple factory interface that is used for creating implementations of the <see cref="IUnitOfWork"/> interface.
/// </summary>
public interface IUnitOfWorkFactory
{
/// <summary>
/// Create a new <see cref="IUnitOfWork"/> with the provided name.
/// </summary>
namespace WhiskeyRecommender.Persistence
{
/// <summary>
/// Simple base interface that defines standard operations that are performed by all repositories in the system.
/// </summary>
public interface IRepository<T>
{
/// <summary>
/// Returns an instance of <c>T</c> that has the provided id.
/// </summary>
public interface IWhiskeyRepository : IRepository<Whiskey>
{
/// <summary>
/// Creates a new <see cref="Whiskey"/> instance, associates it with the provided <see cref="Distillery"/> and returns the new instance.
/// </summary>
Whiskey Create(Distillery d, WhiskeyType type);
/// <summary>
/// Adds the provided <see cref="Whiskey"/> to the database, associating it with the provided <see cref="Distillery"/>.
/// </summary>
namespace WhiskeyRecommender.Persistence
{
/// <summary>
/// A <see cref="IUnitOfWork"/> wraps a database transaction and provides access to the objects via a repository pattern.
///
/// This class implements <see cref="IDisposable"/> to allow use inside <c>using</c> blocks, and should automatically
/// close the database transaction (without saving) when disposed.
/// </summary>
public interface IUnitOfWork : IDisposable
{
public Whiskey Create(Distillery d, WhiskeyType type)
{
Whiskey w = new Whiskey
{
Type = type
};
Add(d, w);
return w;
public RatingDTO GetUserRatingForWhiskey(Guid userId, Guid whiskeyId)
{
using(IUnitOfWork uow = _unitOfWorkFactory.Create("RatingService.GetUserRatingForWhiskey"))
{
User u = uow.UserRepository.FindById(userId);
Whiskey w = uow.WhiskeyRepository.FindById(whiskeyId);
if(u == null)
{
throw new ArgumentException(string.Format("No User with id {0} exists.", userId));
public RatingDTO Create(Guid userId, Guid whiskeyId, int score, string review = "")
{
using(IUnitOfWork uow = _unitOfWorkFactory.Create("RatingService.Create"))
{
Rating r = uow.RatingRepository.All().Where(x => x.ReviewOf.Id == whiskeyId && x.ReviewedBy.Id == userId).FirstOrDefault();
Whiskey w = uow.WhiskeyRepository.FindById(whiskeyId);
User u = uow.UserRepository.FindById(userId);
// there is no existing Rating in the system, so create a new one.))
public IList<WhiskeyDTO> FindWhiskeyForRegion(Guid regionId)
{
using (IUnitOfWork uow = _factory.Create("WhiskeyService.FindWhiskeyForRegion"))
{
Region r = uow.RegionRepository.FindById(regionId);
IList<WhiskeyDTO> result = new List<WhiskeyDTO>();
if (r == null)
{
throw new ArgumentException(string.Format("Cannot find whiskey for Region {0} as it does not exist.", regionId), "regionId");
public class WhiskeyDTO
{
public static WhiskeyDTO Create(Whiskey w)
{
WhiskeyDTO dto = new WhiskeyDTO();
dto.Name = w.Name;
dto.Id = w.Id;
dto.Description = w.Description;
dto.Type = WhiskeyTypeDTO.Create(w.Type);
public interface IServiceFactory
{
IAchievementService CreateAchivementService(ServiceContext context);
IDistilleryService CreateDistilleryService(ServiceContext context);
IRatingService CreateRatingService(ServiceContext context);
IRecommendationService CreateRecommendationService(ServiceContext context);
IUserService CreateUserService(ServiceContext context);
IRegionService CreateRegionService(ServiceContext context);
IWhiskeyService CreateWhiskeyService(ServiceContext context);
IWhiskeyTypeService CreateWhiskeyTypeService(ServiceContext context);