WPF - 在使用 MVVM 的 3 层架构设计中将 DAL 放在哪里?

发布于 2024-09-08 02:40:42 字数 367 浏览 4 评论 0原文

我对整个 n 层架构相当陌生,并且对将 MVVM 与 3 层应用程序一起使用有一些疑问。

根据我的理解,我们有:

  • View,或 UI 层,它是 xaml 文件
  • Model,它是一个自定义类,包含“建模”数据对象的属性和方法
  • ViewModel,它是 View 之间的“适配器”模型
  • A WCF 服务器应该处理数据库访问以及
  • 用于存储数据的 SQL 数据库

我的问题是,如何使用数据访问层将所有这些放在一起?使用 MVVM,我会让模型包含加载/更新自身的方法。相反,这应该发生在 WCF 服务器上吗?如果是这样,对服务器的引用应该存储在模型中还是视图模型中?又该如何称呼呢?

I'm fairly new to the whole n-tier architecture thing, and I had some questions about using MVVM with a 3-tier application.

From my understanding, we have:

  • The View, or UI layer, which is the xaml file
  • The Model, which is a custom class containing properties and methods that "models" the data object
  • The ViewModel, which is the "adapter" between the View and the Model
  • A WCF Server which is supposed to handle Database Access among other things
  • SQL Database for storing data

My question is, how do I put this all together using the Data Access Layer? With MVVM, I would have the models contain the methods to Load/Update themselves. Instead should this be something that happens on the WCF Server? If so, should the reference to the server be stored in the Model or the ViewModel? And how should it be called?

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

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

发布评论

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

评论(3

捶死心动 2024-09-15 02:40:42

严格来说,DAL 不是 MVVM 模式的一部分。 DAL 位于模型“背后”的某个地方,视图和视图模型应该对 DAL 一无所知。

例如,将实体公开为模型的属性,并在首次访问时加载。

public class ProductListModel
{
    public List<Product> AllProducts 
    {
       get
       { 
          if (_AllProducts == null)
              _AllProducts = MyServiceProxy.LoadAllProducts(...)  
          return _AllProducts;
       }
    }

    public void SaveChanges()
    {
         if (_AllProducts != null)
           MyServiceProxy.SaveProducts(AllProducts);
    }
} 

Strictly, DAL is not a part of MVVM pattern. DAL is somewhere 'behind' model, and view and view model should know nothing about DAL.

For example, expose entities as properties of your model which are loaded at first access.

public class ProductListModel
{
    public List<Product> AllProducts 
    {
       get
       { 
          if (_AllProducts == null)
              _AllProducts = MyServiceProxy.LoadAllProducts(...)  
          return _AllProducts;
       }
    }

    public void SaveChanges()
    {
         if (_AllProducts != null)
           MyServiceProxy.SaveProducts(AllProducts);
    }
} 
戴着白色围巾的女孩 2024-09-15 02:40:42

数据访问是一个单独且独立的问题...您可以通过多种不同的方式和模式来实现它,但在所有情况下,最终结果都是一个将由您的 MVVM 类使用的模型。
WCF 可能返回模型中使用的类,也可能返回被设计为数据传输对象的更简单的类,在其中,您将这些对象转换为模型中定义的类的实例...
实际的数据访问(从数据库本身到数据库本身当然是在 WCF 的服务器端编码的......

Data Access is a separate and independant issue... You can implement it in a number of different ways and patterns, but in all cases, the end result is a model that will be used by your MVVM classes.
The WCF may return the classes used in your model, or it may return simpler classses that are designed just as data transfer objects, in which cxase you will have transform these objects into instances of the classes defined in your model...
Actual data access (to-from the DataBase itself is of course coded on the server side of the WCF...

落在眉间の轻吻 2024-09-15 02:40:42

有大量非常冗长的博客文章和描述来组织这一切。这是我最近(今天)读到的一篇:

[链接文本][1]

[1]:http://dotnetslackers.com/articles/data-management/About-layers-separation-and-Entity-Framework.aspx “Dino Esposito 谈 EF 和层数

There are tons of very lengthy blog posts and descriptions on organizing all this. Here is the one I read most recently (today):

[link text][1]

[1]: http://dotnetslackers.com/articles/data-management/About-layers-separation-and-Entity-Framework.aspx "Dino Esposito on EF and Layers

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