我一直在寻找一些博客帖子,试图创建一个适合以下要求的解决方案,但我似乎无法将它们拼凑在一起。希望完全有人可以帮忙。MVC仓库与工作单元,自动映射器和通用仓库
我一直在使用使用Automapper接口库模式......这里有一个下调例如:
public class BookingRepository : IBookingRepository
{
Entities context = new Entities();
public IEnumerable<BookingDto> GetBookings
{
get { return Mapper.Map<IQueryable<Booking>, IEnumerable<BookingDto>>(context.Bookings); }
}
public BookingDto GetBookingWithProduct(Guid bookingId)
{
return Mapper.Map<BookingDto>(context.Bookings.Include(c => c.Products).SingleOrDefault(c => c.BookingId == bookingId));
}
public void Update(BookingDto bookingDto)
{
var booking = Mapper.Map<Booking>(bookingDto);
context.Entry(booking).State = EntityState.Modified;
}
public void Save()
{
context.SaveChanges();
}
public void Dispose()
{
context.Dispose();
}
}
public interface IBookingRepository : IDisposable
{
IEnumerable<BookingDto> GetBookings { get; }
BookingDto GetBooking(Guid bookingId);
void Update(BookingDto bookingDto);
void Save();
}
随着不同的实体一个单独的存储库,例如
public class ProductRepository : IProductRepository
{
Entities context = new Entities();
public IEnumerable<ProductDto> GetProducts
{
get { return Mapper.Map<IQueryable<Product>, IEnumerable<ProductDto>>(context.Products); }
}
public ProductDto GetProductWithDesign(int productId)
{
return Mapper.Map<ProductDto>(context.Products.Include(c => c.Designs).SingleOrDefault(c => c.ProductId == productId));
}
public void Update(ProductDto productDto)
{
var product = Mapper.Map<Product>(productDto);
context.Entry(product).State = EntityState.Modified;
}
public void Save()
{
context.SaveChanges();
}
public void Dispose()
{
context.Dispose();
}
}
public interface IProductRepository : IDisposable
{
IEnumerable<ProductDto> GetProducts { get; }
ProductDto GetProduct(int productId);
void Update(ProductDto productDto);
void Save();
}
然后在我的控制器中,我使用的库如下:
public class HomeController : Controller
{
private readonly IBookingRepository bookingRepository;
private readonly IProductRepository productRepository;
public HomeController() : this(new BookingRepository(), new ProductRepository()) { }
public HomeController(IBookingRepository bookingRepository, IProductRepository productRepository)
{
this.bookingRepository = bookingRepository;
this.productRepository = productRepository;
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (disposing && this.bookingRepository != null)
this.bookingRepository.Dispose();
if (disposing && this.productRepository != null)
this.productRepository.Dispose();
}
}
因此,现在我希望创建一个工作单元来抽象这些存储库并共享上下文,并为重复操作(保存和更新)创建一个通用存储库,同时记住我正在传递Dtos和映射到实体对象。我很难理解如何将它们结合在一起。
此外,我已经看到了这个帖子
Repository pattern with generics and DI
其中指出:“你不应该有除了你的通用仓库等库接口”和自定义查询“应该拥有自己的(通用)抽象:”这是增加了我的劳累过度的大脑的另一个复杂因素,因为我的存储库将具有使用包含语句返回复杂链接对象的自定义查询,因为禁用了“延迟加载”。
所以我准备好被击落,并告诉我这是错误的方式,但会感谢任何方向。
在此先感谢。
我觉得你的文章很有趣,但看起来你实际上正在展示如何创建一个通用库。 – reddy 2014-06-04 06:52:58
@Joey:重要的区别是我不直接使用它,而是继承它。 – jgauffin 2014-06-04 07:47:47
这很有道理,谢谢 – reddy 2014-06-04 07:50:33