MVC:存储库和视图模型都一起构建以获得更好的结构?

发布于 2025-01-06 21:48:16 字数 103 浏览 5 评论 0原文

如果我想结合使用每个实体的存储库和每个视图的视图模型,效果如何?

我可以查找任何网站提示吗?也许有人可以举一个简单的例子?

谢谢

最诚挚的问候!

If I want to combine using repositorys per entity and Viewmodels per view how does it work out?

Any website tips I could look up? Maby someone could give an easy example?

Thanks

Best Regards!

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

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

发布评论

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

评论(3

慕烟庭风 2025-01-13 21:48:17

我喜欢以下结构(来自著名的 Steven Sanderson 的 Pro ASP.NET MVC 系列):

域项目(业务逻辑):

  • 抽象文件夹(存储库接口)
  • 具体文件夹(存储库实现)
  • 实体(EF 生成的类)

Web UI 项目(MVC Web 应用程序):

  • 模型(视图模型)
  • 视图
  • 控制器
  • 等,您明白了

最主要的是您将业务逻辑(应容纳您的存储库)与 Web 分离UI(MVC 项目)

在这种情况下,您的控制器类引用域层并使用 DI/IoC 来调用存储库的正确实例。

控制器类示例:

namespace MyMvcProject
{
    using System.Whatever;
    using MyDomainLayer;

    public class MyController : Controller
    {
        private readonly IMyRepository _myRepository;

        public MyController(IMyRepository myRepository)
        {
            // Resolved using your favorite DI/IoC Container:
            this._myRepository = myRepository;
        }

        public ActionResult DoSomething()
        {
            var stuff = _myRepository.GetStuff();
            return View(stuff);
        }
    }
}

I like the following structure (from the famous Steven Sanderson's Pro ASP.NET MVC series):

Domain Project (Business Logic):

  • Abstract Folder (repository interfaces)
  • Concrete Folder (repository implementations)
  • Entities (EF generated classes)

Web UI Project (MVC Web App):

  • Models (view models)
  • Views
  • Controlers
  • etc, you get the point

The main thing is you're separating your business logic (which should house your repositories) from your Web UI (the MVC project)

In this scenario, your Controller classes reference the domain layer and use DI/IoC to call up the correct instance of the repository.

Example controller class:

namespace MyMvcProject
{
    using System.Whatever;
    using MyDomainLayer;

    public class MyController : Controller
    {
        private readonly IMyRepository _myRepository;

        public MyController(IMyRepository myRepository)
        {
            // Resolved using your favorite DI/IoC Container:
            this._myRepository = myRepository;
        }

        public ActionResult DoSomething()
        {
            var stuff = _myRepository.GetStuff();
            return View(stuff);
        }
    }
}
你怎么敢 2025-01-13 21:48:17

使用 AutoMapper 将数据从实体复制到模型,反之亦然。
这将减少您必须编写的大量“管道”代码。

Use AutoMapper to copy data from entities to models and vice-versa.
This will reduce a lot of 'plumbing' code you will have to write otherwise.

极度宠爱 2025-01-13 21:48:17

我不是专业的开发人员,但我认为 Steve Sanderson 的模型对于某些项目来说不是正确的模型,因为你直接根据模型的观点进行工作。如果您只想显示少数属性而不是全部属性,会发生什么情况?您的完整模型正在前往视图。

我认为你的视图必须针对 viewmodel 类,而不是直接来自 orm 的模型(通过存储库等)。

我发现的唯一问题是模型到视图模型以及视图模型到模型之间的映射过程。让我解释一下...

我试图用 automap 进行此映射,模型 -> 之间的方向viewmodel 工作正常,但在另一个方向(viewmodel 到模型)我找不到自动执行此操作的方法,因为 viewmodel 通常不拥有模型所具有的所有属性,并且如果您经常自动映射到模型对象属性为空。
最后,您始终需要进行一些手动映射。

针对这种情况的想法可能会受到欢迎。
谢谢

I'm not a professional developer but I think Steve Sanderson's model is not the right model for some projects because you are working in your views against the model directly. What happen if you want to show only a few properties and not all of them? Your full model is traveling to the view.

I think your views must work against viewmodel classes and not directly the model coming from orm (trough repository, etc.)

The only problem that I'm finding is the mapping process between model to viewmodel and viewmodel to model. Let me explain...

I was trying to do this mapping with automap, the direction between model -> viewmodel works fine, but in the other direction (viewmodel to model) I'm not finding the way to do it automatically because the viewmodel, normally does not own all the properties that model have and if you do an automap to model object a lot of properties are empty.
Finally you need to make always some manual mappings.

Ideas for this situation may be welcome.
Thanks

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