服务设计:我应该把验证码放在哪里?

发布于 2024-10-14 03:37:00 字数 543 浏览 0 评论 0原文

我对服务的概念有点困惑。

假设我想创建一个新用户。 现在,我正在检查模型(project.Web 解决方案)和 UserServices(project.Services 解决方案)中的所有字段是否都非空。但为了验证电子邮件地址,我必须创建一个新函数。 我应该在project.Services 中创建该类(例如GeneralValidation.cs)并使用它,还是应该将其与服务分开并创建一个新项目?

到目前为止,我还没有为存储库和服务创建两个解决方案。我只有一个用于测试的解决方案,另一个用于 project.Web,还有另一个解决方案,其中我有域文件夹、存储库文件夹和一些用于业务逻辑的类,但在阅读了一些有关设计模式的内容后,我决定拆分这。

也许我还没有理解Services的含义。对我来说,服务是一个将使用存储库(如用户)的层,而服务层是我应该进行验证的地方。如果这是正确的,这就是为什么我不知道在哪里创建电子邮件验证功能的原因。

如果有人能向我解释这一点,我将不胜感激。我已经阅读过博客文章,并在 stackoverflow 中搜索类似的问题,但我不确定我是否真正理解它。

谢谢

I'm a little confused about the concept of services.

Let's assume i want to create a new user.
Right now, i'm checking if all fields are non-empty in Model (project.Web solution) and in UserServices too (project.Services solution). But to validate the email address i have to create a new function.
Should i create that class in project.Services, something like GeneralValidation.cs and use it, or should i separate it from the Services and create a new project?

Until now i didn't create two solutions for Repositories and Services. I just had one solution for testing, other for project.Web, and another solution where i had the a domain folder, repository folder, and a few classes for business logic, but after reading a little about design patterns i've decided to split this.

Maybe i haven't understand yet the meaning of Services. For me, a service is a layer that will consume a repository (like user), and the service layer is where i should do the validation. If this is correct, that's why i don't know where to create the function for email validation for example.

If someone could explain me this i would really be appreciated. I've already read blogs articles, and search similar questions in stackoverflow but i can't be sure if i've really understand it.

Thanks

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

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

发布评论

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

评论(1

安稳善良 2024-10-21 03:37:00

服务和存储库是领域驱动设计中的“一等公民”,所以我不明白您为什么要将它们放在一个单独的项目中?

对于您的特定场景,为什么不创建一个具有“CreateUser”方法的“UserService”,它看起来像这样:

public static class UserService
{
    public static CreateUser( User u )
    {
         var userRepository = RepositoryFactory.CreateUserRepository();

         userRepository.Save (u);

         SendActivationMailForUser(u);
    }

    private static void SendActivationMailForUser( User user )
    {
         ....
    }
}

可以找到有关服务的更多信息 此处

Services and Repositories are 'first class citizens' in Domain Driven Design, so I do not see why you'd want to put them in a separate project ?

For your specific scenario, why don't you create a 'UserService' which has a method 'CreateUser', which looks for instance like this:

public static class UserService
{
    public static CreateUser( User u )
    {
         var userRepository = RepositoryFactory.CreateUserRepository();

         userRepository.Save (u);

         SendActivationMailForUser(u);
    }

    private static void SendActivationMailForUser( User user )
    {
         ....
    }
}

Some more information about services can be found here

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