Entity Framework Custom Property using external Parameters

598 Views Asked by At

I have an EDM used to bring back a collection of "Product" entities within a DAL project. An MVC website project references and directly instantiates the ObjectContext and returns an IQueryable to the website for paging and displaying of Products.

This Entity, to simplify things, we'll say only has "ID", "Rate" and "Description" properties. I want to create another calculated property called "CostPerMonth" which will take an input from the website called "LoanAmount", and, again, to keep things simple, this "CostPerMonth" property will be made up of (("LoanAmount" * "Rate") / 12).

I can't help but feel this calculation should be done in the DAL project to make the code more reusable, as whereever this data is used, a "LoanAmount" will always be specified. The possible solutions I have had are below:

  • Create a partial class for Product, add a new property "CostPerMonth" to Product. In the website controller, when the data is returned, populate this new column by iterating through the returned data and doing the calculations?

  • Create a partial class for Product, add a new property "CostPerMonth" to Product. In the DAL project, create a helper class that has a method that accepts a "LoanAmount" parameter and returns a List. The trouble with this is I would need to materialize the data in order to do the calculations. The method could accept a "Skip" and "Take" parameter that could be used in the Linq query used to return the products?

  • Create a partial class for Product, add a new property "CostPerMonth" to Product. Add another tier to the solution using WCF web services, within the service method do the calculations and use REST to return the data to the MVC web site?

Any advice would be very much appreciated.

Best regards,

Mark

1

There are 1 best solutions below

0
On BEST ANSWER

If we take a step back and actually breakdown what you are asking in your question it might be easier to explain my answer.

  • We have an object/entity named Products
  • There is a calculated field which will be (("LoanAmount" * "Rate") / 12)
  • CostPerMonth does not need to be stored in the database

So this calculated field is a business rule for the Product Entity. Given that you are using MVC, there is only one place where this code/logic can go and that is the model.

Given that your using EF It will sit in a non mapped/computed field and the code to give you an idea will look something like this....

   [NotMapped]
    public decimal CostPerMonth
    {
        get { return (LoanAmount * Rate)/12 ; }
    }

I can go on for days about the problems about having fragmented business logic throughout various layers.

Now if you want your this logic to be consumed easily by other clients, you need to expose a service to do that. (you can use webAPI/service stack/ wcf, etc) it is really another entire question based on your needs. But the point here is that your logic is in one spot.