Reputation: 355
Let's assume that I have a class Offer
which is:
public class Offer
{
[Key]
public int ID { get; set; }
//...
public virtual List<OfferEventManager> EventManagers { get; set; }
public virtual List<EventDay> EventDays { get; set; }
public virtual List<OfferStatus> OfferStatuses { get; set; }
public virtual List<EstimatedCost> EstimatedCosts { get; set; }
public virtual List<Payment> Payments { get; set; }
}
And I'll have to do some checking, e.g. Someone wants to send an Offer
to client, but first Offer
has to be in speciffic OfferStatus
, some example EventDays
and example EstimatedCost
. Now let's assume that I'll have to check it not only in one function, but in some more, so I'll need to know what is the latest OfferStatus
etc. Should I store a function inside Model e.g. GetLatestStatus()
and some other functions or Model should have only properties which are stored in DB? If I can't store functions inside then what is the best way to write some usefull functions which I can use with Offer
got from DB call?
Upvotes: 0
Views: 1547
Reputation: 18975
You can add method in your model class by partial class public partial class Offer
Can read more at Using partial-classes in Entity Framework with custom properties
What is the best way to write some usefull functions which I can use with Offer got from DB call?
You should follow repository pattern
More reference at https://www.c-sharpcorner.com/article/creating-web-api-with-repository-pattern-and-dependency-injection/
Entity Framework 4 / POCO - Where to start?
Upvotes: 1
Reputation: 353
Typically, models in EF should be direct models of the database (possibly with some virtual mappings).
I'm not entirely sure what your question is (or what you are wanting), but if all you're wanting is the most recent status, you can do this:
// assume that 'offer' is an Offer from the DB
var latestStatus = offer.OfferStatuses.OrderByDescending(x => x.Timestamp).First();
You can add a "getter" on the model like so:
public class Offer
{
// Getter method
public OfferStatus GetLatestStatus
{
get { return OfferStatuses.OrderByDescending(x => x.Timestamp).FirstOrDefault(); }
set { ; }
}
}
Upvotes: 1