Doug Chamberlain
Doug Chamberlain

Reputation: 11351

I'm injecting my Service into my controller. Should I inject my Service into my ViewModel instead?

So, I've been building a brand new website to replace our current classic asp site. I set up a base project and added in all the different technologies I would be using.

Ninject Automapper PagedList EF 4.0

So far I have a working prototype of the website. It pulls data from the database, and displays in on my page. I think I am not following a pattern correctly. Because my controller action looks strange. I'm beginning to think I need to change my injection to inject my service into my viewmodel? Is it common to insert a service into a ViewModel?

ViewModel

 public class ParcelDetailViewModel
    {
        public Property Property { get; set; }
        public int CurrentYear { get; set; }
        public IEnumerable<AltOwnership> AltOwnership { get; set; }
        public Ownership Ownership { get; set; }
        public TotalValues TotalValues { get; set; }
        public SiteAddressViewModel SiteAddress { get; set; }
        public Value CurrentValues { get; set; }

        public Transfer LatestTransfer { get; set; }
        public LegalDescription LegalDescription { get; set; }

    }

Controller

public class PropertyController : Controller
{
    ICamaService _service = null;

    [Inject]
    public PropertyController(ICamaService  service)
    {
        _service = service;
    }

    //TODO: ADD ACTIONS

    public ViewResult List(int? page, int? size = 100)
    {
        var result = _service.GetProperties(page,size);
        IList<ParcelDetailViewModel> ViewModel;
        ViewModel = new List<ParcelDetailViewModel>();
        Mapper.Map<IEnumerable<Models.Property>, IEnumerable<ParcelDetailViewModel>>(result, ViewModel);
        return View(ViewModel.ToPagedList(page ?? 1, size ?? 100));

    }


    public ViewResult Details(string id)
    {
        var property = _service.GetProperty(id);
        var acct = property.AccountNumber;
        var AltOwners = _service.GetAltOwners(id);
        var LegalDescription = _service.GetLegalDescription(id);
        var Legals = _service.GetLegals(id).FirstOrDefault();
        var Ownership = _service.GetOwnership((decimal)property.OwnerLookup);
        var Values = _service.GetValues(acct);
        var TotalValues = _service.GetTotalValues(acct);
        var Location = _service.GetLocation(property.LocationLookup);

        ParcelDetailViewModel ViewModel = new ParcelDetailViewModel();
        SiteAddressViewModel SitusAddress = new SiteAddressViewModel();

        ViewModel.SiteAddress = SitusAddress;

        Mapper.Map<Models.Property, ParcelDetailViewModel>(property, ViewModel);
        Mapper.Map<IEnumerable<AltOwnership>, ParcelDetailViewModel>(AltOwners, ViewModel);
        Mapper.Map<Ownership, ParcelDetailViewModel>(Ownership, ViewModel);
        Mapper.Map<TotalValues, ParcelDetailViewModel>(TotalValues, ViewModel);

        Mapper.Map(Location, SitusAddress);
        Mapper.Map(property, SitusAddress);


        return View(ViewModel);
    }

Upvotes: 1

Views: 607

Answers (1)

Wiktor Zychla
Wiktor Zychla

Reputation: 48279

Injecting services into controllers is perfectly correct.

On the other hand, injecting any services into view models would be against the idea of view models - classes which hold data and no actions. What would you do with a service inside a view model?

Upvotes: 8

Related Questions