我在C#中构建项目,我需要能够支持不同的数据结构。这两种结构都在MS SQL上,我使用Entity Framework作为我的默认数据结构。
我已经实现了工作单元模式以及存储库模式和MVVM模式。 我的默认数据结构看起来像这样:
public class Bin {
public string Id { get; set; }
public string BinCode { get; set; }
public long StationId { get; set; }
public Station Station { get; set; }
public string Virtual { get; set; }
public string Physical { get; set; }
public long? ItemId { get; set; }
public Item Item { get; set; }
}
我的替代数据库已由其他人构建,看起来像这样:
public class Bin {
public long Id { get; set; }
public string BinCode { get; set; }
public long StationId { get; set; }
public Station Station { get; set; }
public long BinAliasId { get; set; }
public BinAlias BinAlias { get; set; }
public long? ItemId { get; set; }
public Item Item { get; set; }
}
public class BinAlias {
public long Id { get; set; }
public string Virtual { get; set; }
public string Physical { get; set; }
}
我有一个工作单元界面:
public interface IeVendContextUnitOfWork : IUnitOfWork {
/// <summary>
/// The Bin entities repository.
/// </summary>
IRepository<Bin, long> Bins { get; }
}
IRepository公开了数据访问所需的所有CRUD功能。
我也遵循MVVM模式,所以我有ViewModel,如下所示:
/// <summary>
/// Represents the Bins collection view model.
/// </summary>
public partial class ItemSelectionViewModel : CollectionViewModel<Bin, long, IeVendContextUnitOfWork> {
/// <summary>
/// Creates a new instance of ViewModel as a POCO view model.
/// </summary>
/// <param name="unitOfWorkFactory">A factory used to create a unit of work instance.</param>
public static ItemSelectionViewModel Create(IUnitOfWorkFactory<IeVendContextUnitOfWork> unitOfWorkFactory = null) {
return ViewModelSource.Create(() => new ItemSelectionViewModel(unitOfWorkFactory));
}
/// <summary>
/// Initializes a new instance of the ViewModel class.
/// This constructor is declared protected to avoid undesired instantiation of the ViewModel type without the POCO proxy factory.
/// </summary>
/// <param name="unitOfWorkFactory">A factory used to create a unit of work instance.</param>
protected ItemSelectionViewModel(IUnitOfWorkFactory<IeVendContextUnitOfWork> unitOfWorkFactory = null)
: base(unitOfWorkFactory ?? UnitOfWorkSource.GetUnitOfWorkFactory(), x => x.Bins, x => x.Include(y => y.Item).Where(y => y.Item != null && y.OnHand > 0).OrderBy(y => y.Virtual)) {
}
}
我对如何支持替代数据结构感到茫然,我甚至不知道从哪里开始。
我如何支持这些不同的数据层?
我是否需要关闭模型?
在我的视图模型和存储库之间构建服务层/ API?
有没有办法根据界面构建自定义存储库/ UoW,我可以手动将模型投影到默认模式?