服务、模型和存储层之间的交互

发布于 2024-12-13 18:17:21 字数 548 浏览 0 评论 0原文

我的任务是将应用程序从 SharePoint 转换为 .NET。我关心以正确的方式做这件事,所以我买了一本架构书来阅读模式和实践。

我尝试使用领域驱动设计对所有内容进行建模。我有一个代表我的世界的模型,一个将其存储在数据库中的存储库,以及一个与 UI 交互的服务层(即 WebForms,因为我在 MVC 方面的经验为零,并且已经很难涉足这项事业)。

我很难掌握各层交互的正确方式。我的理解是模型应该是一切的基础。它不引用任何内容,其他层引用它。
问题1:这样吗?

我真的很关心服务层。我注意到我正在开发一个非常贫乏的模型,原因有两个:1,我的模型没有引用存储库,所以我无法通过模型存储任何内容。 2,我试图在事情发生时做事(即,我将一个对象添加到对象列表中,因此我一次将其存储在数据库中,而不是在用户添加完对象后一次将其存储在数据库中)。因此,大量工作是在服务层和代表层之间完成的,而模型就在那里并且看起来不错。

我开始担心——我还处于开发的早期阶段,但我是被视为这一切的架构师的人。我不想维护噩梦(我预计这个应用程序将使用多年)。一如既往,时间是一个问题,我无法有效地准备/学习。任何帮助都会很大。 :-)

I have been tasked with the undertaking of translating an application from SharePoint to .NET. I am concerned with doing it the right way, so I got an architecture book to read up on patterns and practices.

I've tried to model everything out using Domain Driven Design. I have a Model that represents my world, a Repository to store it in the database, and a Service layer to interact with UI (which is WebForms, because I have 0 experience in MVC and am already hardly treading water with this undertaking).

I am having difficulty grasping the correct way for the layers to interact. My understanding is that the Model should be the base of everything. It references nothing, other layers reference it.
Question 1: Is that right?

I'm getting really concerned with the Service Layer. I'm noticing that I'm developing a very anemic Model, for two reasons: 1, my Model doesn't reference Repository, so I can't store anything via the Model. 2, I am trying to do things as they happen (ie. I add an object to a list of objects, so I store it in the DB one at a time instead of all at once when the user is done adding the objects). So a lot of the work is being done between the Service and Rep layers, with the Model just being there and looking nice.

I'm starting to worry--I'm early in the development, but I am the one being looked at to be the architect of all this. I don't want a maintenance nightmare (I expect this application will be used for years). As always, time is a concern, and I have not been able to prepare/learn effectively. Any help would be swell. :-)

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

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

发布评论

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

评论(1

桜花祭 2024-12-20 18:17:21

模型应该是一切的基础。它不引用任何内容,其他层引用它。
问题1:这样吗?

强制分离域模型和持久性系统的典型方法是定义存储库。然而,持久性不是域模型的一部分。

您的模型应该调用由存储库定义的方法

例如,考虑这个完全假的存储库:

// Repository
public class SharepointRepository
{
   void SaveWidget(); // Implement
}

因此存储库只关心加载和保存数据,它们不包含任何您的领域逻辑。

但是,如果您的模型与存储库紧密绑定,那么您就会遇到关注点分离问题。

所以在这种情况下,依赖注入就派上用场了。对于前面的示例,您的模型必须显式实例化 SharePointRepository 并调用方法。一种更简洁的方法是在运行时注入存储库的依赖项,这样您的模型就不会关心。

namespace YourApp.Domain.Abstract
{
    public interface ISharePointRepository
    {
        void SaveWidget();
    }
}

基于此接口,您可以构建具体实现并在运行时将依赖项注入到具体实现中。

namespace YourApp.Domain.Concrete
{
    public class SqlSharePointRepository : ISharePointRepository
    {
        void SaveWidget()
        {
          // Code that Saves the widget using the managed sql provider
        }

    }
}

因此,在您的 Web 表单上:

当您收集数据时,您的模型将与表单中的数据进行水合,并将调用存储库方法,但是存储库本身将在运行时注入到 asp.net 应用程序中,因此您可以将 SqlSharePointRepository 更改为 RavenDbRepository,而不会破坏您的应用程序。

要了解如何在 Webforms 中绑定存储库,请参阅此 SO 帖子:我如何在 asp.net Web 表单上实现 Ninject 或 DI?

使用 MVC,控制器负责解决您认为遇到的差距。但是对于您的问题并根据您当前的设计,模型应该调用存储库操作,但是存储库本身应该是松散耦合的。

Model should be the base of everything. It references nothing, other layers reference it.
Question 1: Is that right?

The typical way to enforce separation between the domain model and the persistence system is to define repositories. Persistence however is not a part of the domain model.

Your models should call methods defined by the repository

For example consider this totally fake repository:

// Repository
public class SharepointRepository
{
   void SaveWidget(); // Implement
}

So the repository is only concerned with loading and saving data, they don't contain any of your domain logic at all.

However if your model is tightly bound to the repository, you've got a separation of concerns issue.

So in this case, Dependency Injection becomes useful. With the prior example, your model would have to explicitly instantiate SharePointRepository and invoke methods. A cleaner way of doing this so that your model doesn't care is to inject the dependency of your repository at runtime.

namespace YourApp.Domain.Abstract
{
    public interface ISharePointRepository
    {
        void SaveWidget();
    }
}

Based on this interface, you could build a concrete implementation and inject the dependency to the concrete implementation at run time.

namespace YourApp.Domain.Concrete
{
    public class SqlSharePointRepository : ISharePointRepository
    {
        void SaveWidget()
        {
          // Code that Saves the widget using the managed sql provider
        }

    }
}

So on your web form:

When you are collecting data, your model will be hydrated with data from the form, and will invoke a repository method, however the repository itself would have been injected into the asp.net application at runtime, so you could change SqlSharePointRepository to RavenDbRepository without breaking your app.

To see how to bind your repository in Webforms see this SO Post: How can I implement Ninject or DI on asp.net Web Forms?

With MVC the controller is responsible for the gap you think your are experiencing. But as to your questions and based on your current design, the model should invoke repository operations, however the repository itself should be loosely coupled.

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