EF实体与服务模式与视图模型(MVC) [英] EF Entities vs. Service Models vs. View Models (MVC)

查看:204
本文介绍了EF实体与服务模式与视图模型(MVC)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图理解并计算好做法,设计您的应用程序/域模型(波苏斯/ DTO的)。

I'm trying to understand and figure good practices for designing your app/domain models (POCOs/DTOs).

比方说,我有以下的数据库表,帐户:

Let's say I have the following database table, Account:

UserID int
Email varchar(50)
PasswordHash varchar(250)
PasswordSalt varchar(250)

当然,EF4将建立实体,像这样:

Of course, EF4 would build the entity like so:

public class Account
{
    public int UserID { get; set; }
    public string Email { get; set; }
    public string PasswordHash { get; set; }
    public string PasswordSalt { get; set; }
}

现在,让我们说我有一个注册新用户视图模型,这可能看起来像这样:

Now, let's say I have a view model for registering a new user, which may look something like so:

public class RegistrationViewModel
{
    public string Email { get; set; }
    public string Password { get; set; }
}

最后,我有一个需要注册用户的服务:

Lastly, I have a service which needs to register the user:

public class RegistrationService
{
    public void RegisterUser(??? registration)
    {
        // Do stuff to register user
    }
}

我试图弄清楚如何传递到RegisterUser方法。视图模型,当然,位于下我的web应用程序(presentation层),所以我不希望这得到传递给我服务。

I'm trying to figure out what to pass into the RegisterUser method. The view model is, of course, located under my web app (presentation layer), so I do not want this getting passed to my service.

所以,我想的四种可能性之一:

1)设置一个服务模型,是类似的,如果不相同,对RegistrationViewModel,并使用此

1) Set up a service model that is similar, if not identical, to the RegistrationViewModel, and use this:

public class RegistrationServiceModel
{
    public string Email { get; set; }
    public string Password { get; set; }
}

public class RegistrationService
{
    public void RegisterUser(RegistrationServiceModel registration)
    {
        // Do stuff to register user
    }
}

2)建立模型的接口,并在我的视图模型继承这一点,并成立了我的方法来接受接口:

2) Set up an interface of the model, and inherit this in my view model, and set up my method to accept the interface:

public interface IRegistrationModel
{
    string Email;
    string Password;
}

public class RegistrationServiceModel : IRegistrationModel
{
    public string Email { get; set; }
    public string Password { get; set; }
}

public class RegistrationService
{
    public void RegisterUser(IRegistrationModel registration)
    {
        // Do stuff to register user
    }
}

在帐户实体

3)传球,做在我的控制器的RegistrationViewModel到帐户的映射:

3) Pass in the Account entity, doing the RegistrationViewModel-to-Account mapping in my controller:

public class RegistrationService
{
    public void RegisterUser(Account account)
    {
        // Do stuff to register user
    }
}

4)把我的视图模型了presentation成域/服务层,并传递到服务方式:

4) Move my view model out of the presentation into a domain/service layer, and pass that into the service method:

public class RegistrationService
{
    public void RegisterUser(RegistrationViewModel account)
    {
        // Do stuff to register user
    }
}

这三种情形似乎都不理想,因为我看到他们每个人的问题。所以,我想知道是否有另一种方法,我不能想到的。

None of these three scenarios seem ideal, as I see problems in each of them. So I'm wondering if there's another method I can't think of.

什么是好的做法呢?

先谢谢了。

推荐答案

您永远传递一个视图模型的服务。服务甚至不知道,你可能在你的presentation层定义视图模型的存在。服务与域模型作品。

使用自动映射以视图模型和领域模型,反之亦然之间进行映射。

You never pass a view model to the service. A service doesn't even know about the existence of a view model that you might have defined in your presentation tier. A service works with domain models.
Use Auto mapper to map between view model and domain model and vice versa.

就个人而言,我从来没有听说过的服务模式在DDD(视图模型服务费)。

Personally, I've never heard of service models in DDD (view models for services).

这篇关于EF实体与服务模式与视图模型(MVC)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆