1

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; }
        
    }
Community
  • 1
  • 1
Doug Chamberlain
  • 11,192
  • 9
  • 51
  • 91
  • `@MyService.GetDescription(Model.BuildingTypeCode)` looks like a static method call. That should set off all the alarms right there. :) – bzlm Feb 21 '12 at 16:35

2 Answers2

4

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.

Matias Kinnunen
  • 7,828
  • 3
  • 35
  • 46
Ken Brittain
  • 2,255
  • 17
  • 21
  • Agreed. Whatever is building the ViewModel should be getting the description and creating the ViewModel with it. By the time the View is consuming the ViewModel, the ViewModel should already have all the data the View needs. – Eric King Feb 21 '12 at 16:36
0

I would recommend having a helper that does that, or a DisplayTemplate

public class ViewHelpers
{
  public static string GetDescription(string code)
  {
    MyService.GetDescription(Model.BuildingTypeCode);
  }
}

or

@ModelType string
@Html.DisplayFor("",MyService.GetDescription(Model.BuildingTypeCode));

More info on templates: http://www.headcrash.us/blog/2011/09/custom-display-and-editor-templates-with-asp-net-mvc-3-razor/

Both of these approaches introduce a dependency on your service but you can test/change them in one single place, instead of the whole application (plus the usage looks cleaner).

linkerro
  • 5,318
  • 3
  • 25
  • 29