1
votes

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 Answers

5
votes

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.