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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:34:58+00:00 2026-06-12T13:34:58+00:00

At the moment I’m working on Asp.Net MVC using: Repository, Unit-Of-Work patterns, Service Layer

  • 0

At the moment I’m working on Asp.Net MVC using: Repository, Unit-Of-Work patterns, Service Layer and ViewModels.

In this project every View is linked to a ViewModel Class, the Controllers are thin-one, so the Business Layer reside on a Service Layer.

I create instances of ViewModel class in the Controller and pass it to the view like this

    public ActionResult Create()
    {
        EventCreateViewModel eventViewModel = new EventCreateViewModel();
        return View(eventViewModel);
    }

In some ViewModel I use to call the Service Layer.

The system works, but I would like to know if it is a good idea adding call to a Service Layer in the ViewModel or better would be leave this operation only to the Controller.


 public class EventCreateViewModel
    {

        public CandidateListViewModel CandidateList = new CandidateListViewModel();

        public EventCreateViewModel()
        {
            DateTimeStart = DateTime.UtcNow;    // Add a default value when a Date is not selected
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.Web.Mvc;
using System.ComponentModel.DataAnnotations;

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;

    using XXX.Models;
    using XXX.Service;

        namespace XXX.ViewModels
        {
            public class CandidateListViewModel
            {
                // We are using the Service Layer
                private ICandidateBL serviceCandidate;

                // Property
                public IDictionary<string, string> Candidates = new Dictionary<string, string>();

                // An utility method that convert a list of Canddates from Enumerable to SortedDictionary
                // and save the result to an inner SortedDictionary for store
                public void ConvertSave(IEnumerable<Candidate> candidates)
                {
                    Candidates.Add("None", "0");    // Add option for no candidate
                    foreach (var candidate in candidates)
                        Candidates.Add(candidate.Nominative, candidate.CandidateId.ToString());
                }

                #region Costructors
                public CandidateListViewModel()
                {
                    serviceCandidate = new CandidateBL();
                    ConvertSave(serviceCandidate.GetCandidates());
                }

                // Dependency Injection enabled constructors
                public CandidateListViewModel(ICandidateBL serviceCandidate)
                {
                    this.serviceCandidate = serviceCandidate;
                }

                public CandidateListViewModel(IEnumerable<Candidate> candidates)
                {
                    serviceCandidate = new CandidateBL();
                    ConvertSave(candidates);
                }
                #endregion
            }
        }
  • 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-12T13:34:59+00:00Added an answer on June 12, 2026 at 1:34 pm

    The controller is the component that should be in control, so to say. The ViewModel should just be a data container, nothing more.

    Remember the Single Responsibility Principle. Once you start distributing logic it will become increasingly difficult to remember and understand all the moving parts.

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

Sidebar

Related Questions

The moment I added a unit test to my ASP.NET MVC application to test
At the moment I am using this to call the TabHost activity to change
at the moment i am using a while (true) loop to do this. I
Having a code-blind moment. ASP.NET 4.0. Web.config: <?xml version=1.0?> <configuration> <system.web> <authentication mode=Forms> <forms
At the moment I'm using this code if __name__==__main__: try: main() except KeyboardInterrupt: f.close
I am at the moment working on a project on Visual Studio 2010 using
At moment I want to implement picture upload without using any plug-ins. My upload
At the moment I'm using bat file to launch my jar and set the
At the moment I'm using all sorts of if statements and substrings in order
for the moment the script does like this: click on the link edit the

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.