视图模型应该如何与存储库通信?

发布于 2024-10-17 11:03:42 字数 294 浏览 6 评论 0原文

我有一堆存储库。他们从 EF 3.5 生成的模型中检索数据。为了简单起见,我们将它们称为repository1、2和3。它们都实现了通用接口:public interface IRepository{..} 我的视图模型应该如何与这些存储库通信?我尝试使用 IRepository GetRepository(stringrepositoryName){..} 方法创建某种工厂,但我无法让它工作。那么我应该在需要时引用视图模型中的存储库还是有更好的解决方案?我想要一个代码示例,谢谢。

干杯

I got a bunch of repositoiries. They retrive data from a EF 3.5 generated model. For the sake of simplicity let's just call them repository1,2 and 3. They all implement the generic interface: public interface IRepository<T>{..} How should my viewmodels communicate with theese repositories? I tried to create some kind of a factory with a IRepository GetRepository(string repositoryName){..}method, but I couldn't get it to work. So should I just reference the repositories in the viewmodels when needed or is there a better solution to this? I would prefer a code sample, thanks.

cheers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

も星光 2024-10-24 11:03:42

这些答案 以及来自 .NET 中的依赖注入 建议将存储库和 ui 与业务逻辑分开。依赖关系应该像这样的核心逻辑:

dal/repositories -> Businesslayer、Models 和 IRepository <- UI

我也想知道 ViewModels 在哪里适合这个。它们绝对不应该连接到存储库,但是 ViewModel 属于业务层(服务层)还是属于 UI 似乎是有争议的。我只是关注 asp.net mvc,目前最喜欢将它们与业务层放在一起以保持控制器简单。而且,业务层从逻辑上属于一起的各个存储库中收集项目,并且通过 ViewModel 对它们进行共同操作,这似乎也是合理的。也许作为一个事务,以便对所有存储库的更新必须成功或回滚。

These answers and the free introduction-chapter from Dependency Injection in .NET recommend having the repositories and ui separated from the businesslogic. Dependencies should go towareds the core-logic like this:

dal/repositories -> Businesslayer, Models and IRepository <- UI

I have also wondered where the ViewModels fit into this. They should definetly not be connected to the repositories at all, but whether the ViewModels belong in the businesslayer (servicelayer) or with the UI seems debatable. I'm just staring out with asp.net mvc and are currently most if favour of putting them with the businesslayer to keep the controllers simple. Also it seems reasonable that the businesslayer gathers items from various repositories that logically belong together and that they are acted on together via the ViewModel. Maybe as a transaction, so that updates to all repositories must succeed or be rolled back.

荒路情人 2024-10-24 11:03:42

我想不出您的视图模型应该与您的存储库进行通信的情况。 ViewModel 应该是供客户端使用的平面模型。

你到底想做什么?

I can't think of a situation where your view model should EVER communicate with your repository. A ViewModel should be a flat model for use by the Client.

What exactly are you trying to do?

枯寂 2024-10-24 11:03:42

您可能会找到 WPF 应用程序框架 (WAF)BookLibrary 示例应用程序 有趣。它使用实体框架和 MVVM。但它没有引入与实体框架一起使用的存储库。

You might find the BookLibrary sample application of the WPF Application Framework (WAF) interesting. It uses the Entity Framework together with MVVM. But it doesn't introduce a repository to work with the Entity Framework.

泪痕残 2024-10-24 11:03:42

存储库提供 T 服务。我所做的就是向我的 T 添加一个静态属性,以通过 IOC 获取存储库:

public class Part // This is one kind of T
{
    public static IRepository<Part> Repository { get { return IoC.GetInstance<IRepository<Part>>(); } }
    ...
}

然后当我需要零件时...

var part = Part.Repository.Find(id);

对于我的单元测试,IoC 提供模拟存储库。在生产中,真实的东西。

A Repository serves up T's. What I've done is add a static property to my T's to get the repostory via IOC:

public class Part // This is one kind of T
{
    public static IRepository<Part> Repository { get { return IoC.GetInstance<IRepository<Part>>(); } }
    ...
}

then when I need a Part...

var part = Part.Repository.Find(id);

For my unit testing IoC serves up mock repositories. In production, the real thing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文