views:

39

answers:

1

Let's say that I have Customer model with following fields: Id, FirstName, LastName. I want to present list of Customers in List View. For this I use my service method which return List to iterate in view.

But now I want also to display some additional information for each customer, for example credit. This information is not stored in specific field in database, but has to be calculated based on data in Transacitons. Where should I do this calculation?

I can have extra field called Credit for Customer model, and do it in getter, but I'm not sure if this is the best way, especially that inside od Model I don't have access to EF context.

+3  A: 

Presuming that your question is correctly worded and this really is just for display, not for business processes (which would be an entirely different question)...

Do it in your presentation model. Add the inputs you need, project onto them, and calculate the output. Here's a very trivial example. The real world is more complicated.

class Person // entity
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

class PersonPresentation
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string DisplayName 
    {  
        get 
        { 
            return string.Format("{0} {1}", this.FirstName, this.LastName);
        }
    }
}

public ActionResult DisplayPeople()
{
    var model = from p in Repository.AllPeople()
                select new PersonPresentation
                {
                    FirstName = p.FirstName,
                    LastName = p.LastName
                };
    return View(model);
}
Craig Stuntz