jeudi 23 juillet 2015

Duck Typing for Razor views

I have a data session model which contains many different types of applicants. All of these have FirstName and LastName properties which I would like to display in a razor view.

var applicant = model.Lead ??
                model.Drivers.FirstOrDefault() ??
                model.Loans.FirstOrDefault() ?? 
                model.Renewals.FirstOrDefault();

At first glance, I would use inheritance, except that these are classes in an EF database, not sure how to setup a parent class.

Is there a clean way to do this or am I stuck with something like this?

dynamic applicant = new ExpandoObject();

if (model.Lead != null) {
   applicant.FirstName = model.Lead.FirstName;
   applicant.LastName = model.Lead.LastName;
} else if ( model.Drivers.Any() ) {
   var first = model.Drivers.FirstOrDefault();
   applicant.FirstName = first.FirstName;
   applicant.LastName = first.LastName;
} else if ...

Aucun commentaire:

Enregistrer un commentaire