1

我按照Project Silk 的结构创建了一个中型项目。但是,我无法将我从存储库中检索到的实体映射到域模型对象中以便在 Web 项目中使用。我在这里发布了一个包含所有代码的类似问题,但没有收到我正在寻找的帮助。

我的应用程序的架构非常接近 Project Silk 的架构。数据层包含存储库和模型 POCO。业务逻辑层保存服务。在这些服务中,我们将对象从数据层映射到业务层中的模型对象。

internal static Model.User ToDataModelUser(User userToConvert)
{
    if (userToConvert == null)
    {
        return null;
    }

    Model.User modelUser = new Model.User()
    {
        UserId = userToConvert.UserId,
        AuthorizationId = userToConvert.AuthorizationId,
        DisplayName = userToConvert.DisplayName,
        Country = userToConvert.Country,
        PostalCode = userToConvert.PostalCode,
        HasRegistered = userToConvert.HasRegistered,
    };
    return modelUser;
}

internal static User ToServiceUser(Model.User dataUser)
{
    if (dataUser == null)
    {
        return null;
    }

    User user = new User()
    {
        UserId = dataUser.UserId,
        AuthorizationId = dataUser.AuthorizationId,
        DisplayName = dataUser.DisplayName,
        Country = dataUser.Country,
        PostalCode = dataUser.PostalCode,
        HasRegistered = dataUser.HasRegistered,
    };
    return user;
}

我的问题是当它们具有多对多关系时如何映射这样的对象?例如,假设用户有一个 ICollection 角色。这意味着我的角色有一个 ICollection 用户。当我通过 ToDataModelUser 或 ToServiceUser 映射用户时,我现在有一个 Roles 属性要填充。因此,上面的代码将如下所示:

internal static Model.User ToDataModelUser(User userToConvert)
{
    if (userToConvert == null)
    {
        return null;
    }

    Model.User modelUser = new Model.User()
    {
        UserId = userToConvert.UserId,
        AuthorizationId = userToConvert.AuthorizationId,
        DisplayName = userToConvert.DisplayName,
        Country = userToConvert.Country,
        PostalCode = userToConvert.PostalCode,
        HasRegistered = userToConvert.HasRegistered,
        Roles = new Collection<Role>()
    };

    foreach (Role role in userToConvert.Roles)
        modelUser.Roles.Add(RoleServies.ToDataModelRole(role));

    return modelUser;
}

Now here comes the problem, if you look at RoleServices.ToDataModelRole(Role role) this is what you get:

internal static Model.Role ToDataModelRole(Role roleToConvert)
{
    if (roleToConvert == null) return null;

    Model.Role role = new Model.Role()
    {
        Description = roleToConvert.Description,
        RoleId = roleToConvert.RoleId,
        RoleName = roleToConvert.RoleName,
        Users = new Collection<User>()
    };

    foreach (User user in roleToConvert.Users)
        roleToConvert.Users.Add(UserServices.ToDataModelUser(user));

    return role;
}

如您所见,如果您运行此程序,您将收到堆栈溢出错误 b/c,我们将在尝试进行映射时从用户 >> 角色 >> 用户 >> 角色 >> 等进行。如果我不映射导航属性,我将无法在 Web 项目中访问它们。我有一种感觉,我在这里完全错过了一些东西。

4

1 回答 1

0

您可以为方法创建重载ToDataModelX。传递 aBoolean以取消/激活从属对象的加载。与其总是加载角色的用户,不如仅在被指示时加载它们。

internal static Model.User ToDataModelUser(User userToConvert)
{
    return ToDataModelUser(userToConvert, true);
}

internal static Model.User ToDataModelUser(User userToConvert, Boolean loadRoles)
{
    if (userToConvert == null)
    {
        return null;
    }

    Model.User modelUser = new Model.User()
    {
        ....
        Roles = new Collection<Role>()
    };

    if (loadRoles)
    {    
        foreach (Role role in userToConvert.Roles)
            modelUser.Roles.Add(RoleServies.ToDataModelRole(role, false));
    }

    return modelUser;
}


internal static Model.Role ToDataModelRole(Role roleToConvert)
{
    return ToDataModelRole(roleToConvert, true);
}

internal static Model.Role ToDataModelRole(Role roleToConvert, Boolean loadUsers)
{
    if (roleToConvert == null) return null;

    Model.Role role = new Model.Role()
    {
        ....
        Users = new Collection<User>()
    };

    if (loadUsers) 
    {
        foreach (User user in roleToConvert.Users)
            roleToConvert.Users.Add(UserServices.ToDataModelUser(user, false));
    }

    return role;
}
于 2012-08-20T19:41:29.420 回答