mardi 2 mai 2017

Specifying A Property As An Attribute Of Another Property?

So I have an EF class with two properties.

public class MyTable
{
    public DateTime Requested { get; set; }
    public Guid RequestedUserID { get; protected set; }
}

Now I want to be able to insert the current User ID into RequestedUserID when Requested is changed. To do this I can override EF's DbContext.SaveChanges() event and check if the any modified column has a given attribute instructing my code to also modify an associated UserID column. Example attribute markup:

public class MyTable
{
    [TrackModifingUser(UserIDColumn = nameof(RequestedUserID))]
    public DateTime Requested { get; set; }
    public Guid RequestedUserID { get; protected set; }
}

And then later in my DbContext.SaveChanges() event I would get the property by name and set its value accordingly.

Issue, I wish my Attribute could store the property itself somehow. And with that skip looking the property up by name. Something like this.

public class MyTable
{
    [TrackModifingUser(UserIDColumn = RequestedUserID)]
    public DateTime Requested { get; set; }
    public Guid RequestedUserID { get; protected set; }
}

Is this possible or must I resolve the property at run time? Is there any sort of caching I can use to avoid getting the property by name more than once for the same column?





Aucun commentaire:

Enregistrer un commentaire