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 184535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T15:18:34+00:00 2026-05-11T15:18:34+00:00

in this MVC tutorial it showed how to decouple Controller from database logic using

  • 0

in this MVC tutorial it showed how to decouple Controller from database logic using Repository and a Service layer. However the example is for a very simple model of pure properties. What about associations?

Say if Product model has an association to the Users table through Owner association. The controller calls the service or the repository to get a list of users, pass it to View, and View displays the users as a drop-down list.

Then on a standard POST: product/Create, controller needs to get the chosen userID, then grab the User entity of that id, and associate it with the to be created Product.

  if (!String.IsNullOrEmpty(Request['SelectedUserId']))         {             int selectedUserId = Convert.ToInt16(Request['SelectedUserId']);             newProduct.Owner = _productService.GetUserById(ownerId);         }    _productService.AddProduct(newProduct); 

But this process complicates the controller logic. Even worse if we need to validate the associations (since there wouldn’t be OnChanging events for the associations, we can’t do it in the Model partial class).

My question is, is there any better way to handle associations? Is it better to pass the Request parameters to the service layers as well and do everything there? What about validations?

  • 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. 2026-05-11T15:18:34+00:00Added an answer on May 11, 2026 at 3:18 pm

    Update: (after the comment on it not being linq2sql)

    In your specific scenario (based on your desc + the code you posted), I would definitely move the load down to the service layer. This doesn’t mean moving the request down to it. I would refactor as:

    string userIdStr = Request['SelectedUserId']; var userId = !String.IsNullOrEmpty(userIdStr) ?       Convert.ToInt16(userIdStr) :       default(int?); _productService.AddProduct(userId, newProduct); 

    I also usually have an extension method like ToNullable<int>(), that makes it:

    var userId = Request['SelectedUserId'].ToNullable<int>(); _productService.AddProduct(userId, newProduct); 

    Depending on the emphasis you give to models, you can have something that contains both fields, like CustomerProduct.


    If you are persisting with linq2sql, you don’t need to get the User to do that in this scenario. Linq2sql will generate an OwnerId property, which you can use without having to go the db to get it.

    Having the model tied for different sub-systems is not always the best approach when dealing with complex models. This is more important if you have parts of the system that are appropriate for their own bounded context. When that’s the case, the above has even more importance, as you want to have really clear and specific places to integrate the 2 bounded contexts.

    Besides the above, there are some times when it is appropriate to have some extra glue for these type of processes. Consider adding a service class to help you on it. This doesn’t mean that everything needs to go through a service class or that the domain objects wouldn’t have the logic, its just there for the specific scenarios where you need the extra actions that isn’t appropriate for either the domain objects or the controllers.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 119k
  • Answers 119k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Assignment is (sort of) right to left: int a =… May 11, 2026 at 11:49 pm
  • Editorial Team
    Editorial Team added an answer IntelliJ is not free, but I think it's superior to… May 11, 2026 at 11:49 pm
  • Editorial Team
    Editorial Team added an answer Would this work: var answer1Sum = SurveyAnswers.Sum( survey => survey.Answer1… May 11, 2026 at 11:49 pm

Related Questions

In this code from Microsoft's MVC Tutorial NerdDinners: public class DinnerRepository { private NerdDinnerDataContext
I'm using a masterpage in my ASP.NET MVC project. This masterpage expects some ViewData
I'm getting back into .NET development after a couple years and it seems that
I just finished Scott Gu's Nerd Diner tutorial. I found it very helpful because

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.