本文介绍了Dapper控制日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该问题旨在通过Dapper 阐明控制日期时间。

This question is meant to bring some light around control date times using Dapper.

这些控件用于审核数据存储中的信息,并确定何时创建/更新了特定行。我在StackOverflow的项目中都找不到任何信息,因此我希望这篇文章成为帮助他人甚至成为图书馆未来扩展的真理的主要来源。

These controls are used to audit the information in a data storage and figure out when a particular row has been created / updated. I couldn't manage to find any information on GitHub's project, either here in StackOverflow, so I would like this post to become a central source of truth to help others or even to turn into a future extension of the library.

任何答案,资源或最佳实践都会

Any answer, resource or best practice will be appreciated.

推荐答案

我遇到了一起使用Rails和Dapper都使用过的数据库的情况。 Rails正在管理created_at和updated_at,而不是数据库。因此,使用.net应用程序,我必须实现一个解决方案来管理这些事务,并提供在这些层(例如事件)上添加其他业务逻辑的能力。

I've ran into a case where I was working with a database that was consumed by both Rails and Dapper. Rails was managing created_at and updated_at, not the database. So with the .net application I had to implement a solution that managed these and provided the ability to add additional business logic at these layers such as events.

一个基本的示例,说明了我如何使用Dapper Simple Crud周围的包装器进行插入和更新。此示例未包括公开dapper和simplecrud中的其他关键方法,例如Query,GET,Delete等。您需要自由选择公开这些方法。

I've included a basic example of how I handled this with a wrapper around Dapper Simple Crud for inserts and updates. This example does not include exposing the other critical methods from dapper and simplecrud such as Query, GET, Delete, etc. You will need to expose those at your discresion.

对于安全性,请确保使用属性[Dapper.IgnoreUpdate]装饰模型created_at属性

For safety ensure that you decorate your models created_at property with the attribute [Dapper.IgnoreUpdate]

[Table("examples")]
public partial class example
{
    [Key]
    public virtual int id { get; set; }
    [Required(AllowEmptyStrings = false)]
    [StringLength(36)]
    public virtual string name { get; set; }
    [Dapper.IgnoreUpdate]
    public virtual DateTime created_at { get; set; }
    public virtual DateTime updated_at { get; set; }
}

public class ExampleRepository : IExampleRepository
{
    private readonly IYourDapperWrapper db;

    public PartnerRepository(IYourDapperWrapper yourDapperWrapper){
        if (yourDapperWrapper == null) throw new ArgumentNullException(nameof(yourDapperWrapper));
        db = yourDapperWrapper;
    }

    public void Update(example exampleObj)
    {
      db.Update(exampleObj);
    }

    public example Create(example exampleObj)
    {
      var result = db.Insert(exampleObj);
      if (result.HasValue) exampleObj.id = result.value;
      return exampleObj;
    }
}

public class YourDapperWrapper : IYourDapperWrapper
{
    private IDbConnectionFactory db;

    public YourDapperWrapper(IDbConnectionFactory dbConnectionFactory){
      if (dbConnectionFactory == null) throw new ArgumentNullException(nameof(dbConnectionFactory));
      db = dbConnectionFactory;
    }

    public int Insert(object model, IDbTransaction transaction = null, int? commandTimeout = null)
    {
      DateUpdate(model, true);
      var results = Db.NewConnection().Insert(model, transaction, commandTimeout);
      if (!results.HasValue || results == 0) throw new DataException("Failed to insert object.");
      return results;
    }

    public int Update(object model, IDbTransaction transaction = null, int? commandTimeout = null)
    {
      DateUpdate(model, false);
      var results = Db.NewConnection().Update(model, transaction, commandTimeout);
      if (!results.HasValue || results == 0) throw new DataException("Failed to update object.");     
      return results;
    }

    private void DateUpdate(object model, bool isInsert)
    {
      model.GetType().GetProperty("updated_at")?.SetValue(model, DateTime.UtcNow, null);
      if (isInsert) model.GetType().GetProperty("created_at")?.SetValue(model, DateTime.UtcNow, null);
    }
}

这篇关于Dapper控制日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 17:14