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 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> |
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 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> |
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
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> |
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 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 | |
{ |
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
public Whiskey Create(Distillery d, WhiskeyType type) | |
{ | |
Whiskey w = new Whiskey | |
{ | |
Type = type | |
}; | |
Add(d, w); | |
return w; |
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
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)); |
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
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.)) |
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
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"); |
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
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); |
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
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); |
OlderNewer