开发者

Base Models and Derived Models in MVC

I'm putting together an MVC application where I have created a base Model which then has four derived Models, all of which inherit from the base Model:

public abstract class BaseFund
{
    public string Name { get; set; }
    public int AccountId { get; set; }
    public abstract decimal Value { get; }
    public virtual InvestmentAccount Account { get; set; }
}

one of the derived Models:

public class ShareFund : BaseFund
{
    public string ISIN { get; set; }
    public ShareFundType FundType { get; set; }
    public IncomeStatus IncomeStatus { get; set; }
    public decimal TotalShares {
        get
        {
            ICollection<ShareTransaction> tt = this.Transactions;
            var outgoings = Transactions.Count > 0 ? Transactions.Where(t => t.TransactionType.IsOutgoing.Equals(true)).Sum(a => a.Units) : 0;
            var incomings = Transactions.Count > 0 ? Transactions.Where(t => t.TransactionType.IsOutgoing.Equals(false)).Sum(a => a.Units) : 0;
            return incomings - outgoings;
        }
    }
    public override decimal Value
    {
        get
        {
            return this.TotalShares * (this.SharePrice / 100);
        }
    }
    public decimal SharePrice { get; set; }
    public ICollection<ShareTransaction> Transactions { get; set; }
}

And there are three other derived Models that are similar. All of the Models are POCO's used by Entity Framework.

EDIT : The view is standard MVC scaffolding stuff at this stage:

<table>
<tr>
    <th>
        Name
    </th>
    <th>
        Account
    </th>
    <th>
        Value
    </th>
    <th></th>
</tr>

@foreach (var item in Model) {
<tr>
    <td>
        @Html.DisplayFor(modelItem => item.Name)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Account.AccountNumber)
    </td>
    <td>            
        @Html.DisplayFor(modelItem => item.Value)
    </td>
    <td>
        @Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
        @Html.ActionLink("Details", "Details", new { id=item.Id }) |
        @Html.ActionLink("Delete", "Delete", new { id=item.Id })
    </td>
</tr>
}
</table>

What I want to do is create a View that shows the fields from the Base Fund (Name, AccountId and Value). The problem is that the logic that works out the Value is different for every derived Model - in the case of ShareFund it uses TotalShares, so the BaseFund used in the View must be cast to be of type ShareFund. The other derived Models don't necessarily have TotalShares as a property.

With that in mind:

  • Is using inheritance with Models in this way the way to go? If so, how do I get the fields specific to the derived models in the View?
  • If using inheritance is not recommended for开发者_运维技巧 this type of scenario, what should I use in its place?

Thanks


There turned out to be a simple answer to this. One of the Transaction properties further down the chain wasn't being populated from the database by EF. This meant TransactionType was null which led to an null reference error in TotalShares. I misread this as meaning there was a problem with the property as it belonged to the derived Model and not the base Model.

Thanks Lazarus, your comment led me to the problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜