DapperLib/Dapper.Contrib

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

Opened this issue · 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
}

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 that is a great idea.