Home > other >  Decorator @property on ASP net core like in django
Decorator @property on ASP net core like in django

Time:05-09

The thing is that like in Django which you can add model properties without changing the model class using the @property decorator.

I'm trying to achieve same with aspnetcore

I mean I want to add some functions to the model class which will return some string/int in order to access to them from the razor view(ex: model.extraProperty)

@edited Adding more info....

    public class Ticket
    {
        public int Id { get; set; }
        public int? AssignedId { get; set; }
        public virtual AppUser Assigned { get; set; }
        public int StationId { get; set; }
        public virtual Station Station { get; set; }
        public int FailureId { get; set; }
        public virtual Failure Failure { get; set; }
    }

    public class TicketData
    {
        public int Id { get; set; }
        public int TicketId { get; set; }
        public virtual Ticket Ticket { get; set; }
        public int TicketStatusId { get; set; }
        public virtual TicketStatus TicketStatus { get; set; }
        public string comments { get; set; }

    }

The logic is: Everytime a ticket is created, right after a TicketData is created with the Open Status and the Ticket id reference

Then another TicketData is created to change status to InProcess, and then another TicketData is created to change status to Closed

So I would like to access the last Ticket Status from the Ticket View

//On Django I would be able to achieved it only adding something like this Ticket.GetStatus()
@property
GetStatus()
{
    _context.TicketData.Where(x => x.TicketId == this.Id).LastOrDefault();
}

I hope can get some help from you guys.

Thanks.

CodePudding user response:

Im not sure about the @property approach. You might consider writing your own C# attribute to try and achieve something like that, but let me present a possible solution.

So, I need to make some assumptions as the example does not depict much on how and where you process the described logic. Assuming you are doing the logic within a eg. TicketsController.ProcessTicketAction() which returns your .cshtml view, you'd actually need to pass down the View's Model a complex object which represents the whole state transitioning. And that could be achieved eg. by passing a collection of TicketData inside it.

What I'd do here is create a view model class eg. class TicketModel and inside define a ICollection<TicketData>, and now whenever or wherever you'd be executing the described logic you would actually new up a TicketData, save the Ticket ref and id.

In your .cshtml view code you could extract the desired data then with eg. LINQ and .OrderByDescending(x => (int) x.TicketStatus).FirstOrDefault().

Another approach could be to remodel the presented solution and save a collection of TicketData inside the Ticket object and put the GetStatus() method inside it:

    public class Ticket
    {
        public int Id { get; set; }
        public int? AssignedId { get; set; }
        public virtual AppUser Assigned { get; set; }
        public int StationId { get; set; }
        public virtual Station Station { get; set; }
        public int FailureId { get; set; }
        public virtual Failure Failure { get; set; }

        public ICollection<TicketData> Data { get; set; }
    
        public TicketStatus GetStatus() => Data?.OrderByDescending(x => (int) x.TicketStatus)?.FirstOrDefault()?.TicketStatus;
    }

And then assuming the Ticket is you model class you'd just go with Model.GetStatus() in your view .cshtml.

  • Related