Search code examples
c#asp.net-mvc-3razordecoupling

How should I handle lookups in my ViewModel?


My database table for buildings stores the building type as a code. In a separate lookup table the description for that code is stored.

How should I design my ViewModel and where will I need to make the call to get the associated description value?

I sort of can see one option. I want to know if there is a better option.

BuildingViewModel
{
 public string BuildingTypeCode { get;set;}  
 ...other properties
}

Then in my view

code...

<p>@MyService.GetDescription(Model.BuildingTypeCode)</p>

...code

Am I incorrect in the way I am thinking? if I do the above I create a dependency in my View to the service?

Update 1

Working through some of the solutions offered. I seem to run into another issue. I can't access the constructor of each building directly...

    public ViewResult Show(string ParcelId)
    {
        var result = _service.GetProperty(ParcelId);
        var AltOwners = _service.GetAltOwners(ParcelId);
        var Buildings = _service.GetBuildings(ParcelId);

        ParcelDetailViewModel ViewModel = new ParcelDetailViewModel();
        ViewModel.AltOwnership = new List<OwnerShipViewModel>();
        ViewModel.Buildings = new List<BuildingViewModel>();

        AutoMapper.Mapper.Map(result, ViewModel);
        AutoMapper.Mapper.Map<IEnumerable<AltOwnership>, IEnumerable<OwnerShipViewModel>>(AltOwners,ViewModel.AltOwnership);
        AutoMapper.Mapper.Map<IEnumerable<Building>, IEnumerable<BuildingViewModel>>(Buildings, ViewModel.Buildings);
        ViewModel.Pool = _service.HasPool(ParcelId);
        ViewModel.Homestead = _service.IsHomestead(ParcelId);

        return View(ViewModel);
    }

public class ParcelDetailViewModel
    {
        public IEnumerable<OwnerShipViewModel> AltOwnership { get; set; }
        //public IEnumerable<ValueViewModel> Values { get; set; }
        public IEnumerable<BuildingViewModel> Buildings { get; set; }
        //public IEnumerable<TransferViewModel> Transfers { get; set; }
        //public IEnumerable<SiteAddressViewModel> SiteAddresses { get; set; }

        public string ParcelID { get; set; }
        //public string ParcelDescription { get; set; }
        //public int LandArea { get; set; }
        //public string Incorporation { get; set; }
        //public string SubdivisionCode {get;set;}
        public string UseCode { get; set; }
        //public string SecTwpRge { get; set; }
        //public string Census { get; set; }
        //public string Zoning { get; set; }
        public Boolean  Homestead {get;set;}

        //public int TotalBuildingArea { get; set; }
        //public int TotalLivingArea { get; set; }
        //public int LivingUnits { get; set; }
        //public int Beds { get; set; }
        //public decimal Baths { get; set; }
        public short Pool { get; set; }
        //public int YearBuilt { get; set; }
        
    }

Solution

  • My understanding is that the view model is meant for display ready data. I think the real problem here is putting model dependent logic into the view.

    You can do your service lookup but keep that code in the controller. The view model should be considered display ready (save for some formatting).

    class BuildingViewModel 
    { 
        public string BuildingTypeCode { get;set;}   
        ...other properties 
    } 
    

    and then do the lookup before you render:

    public ActionResult Building()
    {
        var typeCode = // get from original source?
        var model = new BuildingViewModel
        {
            BuildingTypeCode = MyService.GetDescription(typeCode)
        };
        return View("Building", model);
    }
    

    Having come from a long line of JSP custom tags I dread having any code hidden in the view layout. IMO, that layer should be as dumb as possible.