简体   繁体   English

在ServiceStack.OrmLite中可以使用拦截器吗?

[英]Are interceptors possible in ServiceStack.OrmLite?

I would like to create a hook for database inserts or updates in OrmLite. 我想为OrmLite中的数据库插入或更新创建一个钩子。

Lets say I want to write a CreateDate for every record which is inserted to the database and a LastUpdateDate for every update without database triggers or anything like that. 假设我要为插入到数据库中的每个记录编写一个CreateDate ,并为每个没有数据库触发器或类似事件的更新编写一个LastUpdateDate I see no way of doing this if I call Db.Save<Anything>(anyList) with anyList containing new and changed objects. 如果用包含新对象和更改对象的anyList调用Db.Save<Anything>(anyList) ,则看不到这样做的方法。

I know that NHibernate does provide interceptors to inject custom logic for each object before it gets persisted. 我知道NHibernate确实提供了拦截器,以便在持久化每个对象之前为它们注入自定义逻辑。 Is there any way to achieve this in ServiceStack.OrmLite? 在ServiceStack.OrmLite中有什么方法可以实现这一目标?

I thought this was a good feature that was missing so I've just added support for Insert and Update Filters to OrmLite . 我认为这是一个缺少的好功能,因此我刚刚添加了对OrmLite的插入和更新过滤器的支持 So now you can configure a global Update or Insert filter with: 因此,现在您可以使用以下命令配置全局更新或插入过滤器:

public interface IAudit
{
    DateTime CreatedDate { get; set; }
    DateTime ModifiedDate { get; set; }
    string ModifiedBy { get; set; }
}

OrmLiteConfig.InsertFilter = (dbCmd, row) =>
{
    var auditRow = row as IAudit;
    if (auditRow != null)
    {
        auditRow.CreatedDate = auditRow.ModifiedDate = DateTime.UtcNow;
    }
};

OrmLiteConfig.UpdateFilter = (dbCmd, row) =>
{
    var auditRow = row as IAudit;
    if (auditRow != null)
    {
        auditRow.ModifiedDate = DateTime.UtcNow;
    }
};

So now the Created and Modified date fields will be updated on every row that implements IAudit and is inserted in any of OrmLite's Typed APIs (ie not dynamic SQL or partial updates using anon types), eg: 因此,现在将在实现IAudit每一行上更新“创建日期”和“修改日期”字段,并将其插入OrmLite的任何Typed API(即,非动态SQL或使用匿名类型的部分更新),例如:

Table Definition 表定义

public class AuditTableA : IAudit
{
    [AutoIncrement]
    public int Id { get; set; }
    public DateTime CreatedDate { get; set; }
    public DateTime ModifiedDate { get; set; }
    public string ModifiedBy { get; set; }
}

public class AuditTableB : IAudit
{
    [AutoIncrement]
    public int Id { get; set; }
    public DateTime CreatedDate { get; set; }
    public DateTime ModifiedDate { get; set; }
    public string ModifiedBy { get; set; }
}

var a = new AuditTableA();
var b = new AuditTableB();
db.Save(a);
db.Save(b);

var insertRowA = db.SingleById<AuditTableA>(a.Id);
var insertRowB = db.SingleById<AuditTableB>(b.Id);

//both insertRowA/insertRowB CreatedDate/ModifiedDate fields populated

Validation 验证方式

The filters can also be used for validation where throwing an exception will prevent the operation and bubble the exception, eg: 过滤器还可以用于验证,如果抛出异常将阻止操作并冒泡该异常,例如:

OrmLiteConfig.InsertFilter = OrmLiteConfig.UpdateFilter = (dbCmd, row) =>
{
    var auditRow = row as IAudit;
    if (auditRow != null)
    {
        if (auditRow.ModifiedBy == null)
            throw new ArgumentNullException("ModifiedBy");
    }
};

try
{
    db.Insert(new AuditTableA());
}
catch (ArgumentNullException) {
   //throws ArgumentNullException
}

db.Insert(new AuditTableA { ModifiedBy = "Me!" }); //succeeds

This feature is now available in ServiceStack's MyGet feed and will be available in the next v4.0.11+ release on NuGet. 现在,此功能在ServiceStack的MyGet feed中可用,并且在NuGet的下一v4.0.11 +版本中可用。

There isn't any way to achieve this in ServiceStack.OrmLite. 在ServiceStack.OrmLite中没有任何方法可以实现此目的。 That functionality is typical only of the heavier ORMs such as NHibernate, like you suggested. 如您建议的那样,该功能仅是较重的ORM(例如NHibernate)的典型功能。

ServiceStack.OrmLite is intended to be very lightweight, and as such provides a minimal API . ServiceStack.OrmLite旨在非常轻巧,因此提供了最小的API

API

You can use an ORM such as NHibernate with ServiceStack. 您可以在ServiceStack中使用ORM(例如NHibernate)。 I personally use an ORM called LightSpeed from Mindscape in my ServiceStack projects, it can automatically take care of creation and update timestamps without any code hooks. 我个人在ServiceStack项目中使用了Mindscape的名为LightSpeed的ORM,它可以自动处理创建和更新时间戳,而无需任何代码挂钩。 Using the ServiceStack REST platform doesn't dictate that you have to use the ServiceStack ORM, if it isn't suitable. 如果不适合,使用ServiceStack REST平台并不一定要使用ServiceStack ORM。


As @Mythz suggests, you can request that this feature is added . 正如@Mythz建议的那样,您可以请求添加此功能

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM