Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8422189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:22:29+00:00 2026-06-10T03:22:29+00:00

I created a mid-size project following Project Silk’s structure. However, I have trouble mapping

  • 0

I created a mid-size project following Project Silk’s structure. However, I have trouble mapping the entities I retrieve from my repositories into domain model objects for use in the Web project. I have posted a similar question here with all the code and haven’t received the help I’m looking for.

My application’s architecture follows Project Silk’s very closely. The data tier holds the repositories and model POCOs. The Business Logic layer holds the services. Inside these services, we map objects from the Data Tier to the Model objects in the business layer.

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;
}

My question is how do I map objects like this when they have many-to-many relationships? For example, lets say a User has an ICollection Roles. That means my Role has an ICollection Users. When I’m mapping a users via ToDataModelUser or ToServiceUser, I now have a Roles property to populate. Thus the code from above will look like this:

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;
}

As you can easily see, if you run this you will get a stack overflow error b/c we will be going from User >> Role >> User >> Role >> etc.. when trying to do the mapping. If I don’t map the navigation properties, I don’t have access to them in the web project. I have a feeling I am totally missing something here.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T03:22:31+00:00Added an answer on June 10, 2026 at 3:22 am

    You could create an overload for the ToDataModelX methods. Pass a Boolean to de/activate the loading of the subordinate object. Instead of always loading the a Role’s Users, only load them when directed to do so.

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a collection of .html files created in the mid-90s, which include a
We have a mid-size SQL Server based application that has no indexes defined. Not
I have just created a mid-sized web-application using Java, a custom MVC framework, javascript.
I have a mid-size collection of records --about 20 million -- that I need
I have created web page http://jsfiddle.net/KNfrm/embedded/result/ mid div is not extending to the height
I have a bucket created since mid 2010. The bucket exists but is empty.
Created .NET WCF service, tested it - works. Generated schemas from Data and service
I created a following class within a namespace Global namespace Global { public static
I created a PHP Drop Down which is populated from a MySql Database and
I work in a mid size company as a Senior dev and work on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.