Ols*_*ls1 5 c# repository mvvm viewmodel
我有一堆repositoiries.他们从EF 3.5生成的模型中检索数据.为了简单起见,我们只需public interface IRepository<T>{..}要将它们称为repository1,2和3.它们都实现了通用接口:我的viewmodels应该如何与theese存储库进行通信?我尝试用方法创建某种工厂IRepository GetRepository(string repositoryName){..},但我无法让它工作.那么我应该在需要时只在视图模型中引用存储库,还是有更好的解决方案呢?我更喜欢代码示例,谢谢.
干杯
n8w*_*wrl -2
存储库提供 T 服务。我所做的就是向我的 T 添加一个静态属性,以通过 IOC 获取存储库:
public class Part // This is one kind of T
{
public static IRepository<Part> Repository { get { return IoC.GetInstance<IRepository<Part>>(); } }
...
}
Run Code Online (Sandbox Code Playgroud)
然后当我需要零件时...
var part = Part.Repository.Find(id);
Run Code Online (Sandbox Code Playgroud)
对于我的单元测试,IoC 提供了模拟存储库。在生产中,真实的东西。
| 归档时间: |
|
| 查看次数: |
2112 次 |
| 最近记录: |