Dapper.Contrib icon indicating copy to clipboard operation
Dapper.Contrib copied to clipboard

Dapper.Contrib new attribute for automatically set a datetime when inserting or updating a row

Open giammin opened this issue 6 years ago • 2 comments

I was thinking at something that could create the insert or update query with GETDATE or GETUTCDATE (in case of sql server) for datetime properties decorated with an attribute.

So you dont have to manually update properties like ChangedDate CreatedDate

It could be useful for inattentive developer or when saving/updating a big list of entities

[AttributeUsage(AttributeTargets.Property)]
public class AutoDateAttribute : Attribute
{
    public AutoDateAttribute (QueryAction action, bool useUTC)
    {
        UseUTC = useUTC;
        Action = action;
    }
    public bool UseUTC { get; }
    public QueryAction Action { get; }
}
public enum QueryAction
{
    Insert,
    Update
}

giammin avatar Jun 22 '18 09:06 giammin

I wonder if you could create a Default value attribute, and pass it a function to provide the insert functionality. Then maybe a another for always update. Might be a bit more general and the data being set would always be in .Net land and wouldn’t be restricted to db type

rhubley avatar Jun 22 '18 13:06 rhubley

@rhubley that is a great idea.

giammin avatar Jun 25 '18 07:06 giammin