EF代码优先继承一个基类来实现轻松historocity [英] EF Code-First inherit a single base class to implement easy historocity

查看:307
本文介绍了EF代码优先继承一个基类来实现轻松historocity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到如下所述实施我的计划了一些错误。我不是在解决,因为我在这是否是一个好主意,尤其是错误这一点很感兴趣。

I am running into some errors implementing my plan as described below. I am not so interested at this point in resolving particular errors as I am in whether or not this is a good idea.

所有的历史能力的对象从普通类下降 AuditableObject 有一个属性公众的Guid ID {搞定;组; }

All history-capable objects descend from a common class AuditableObject with a single property public Guid ID { get; set; }.

一个后代可能是:

公共类塔克:AuditableObject {公共字符串调料{搞定;组; }}

现在,我想执行保存事件处理程序编写如下表(类)

Now, I would like to implement the save event handler to write to the following table (class)

public class AuditItem
{
    public Guid ID { get; set; }
    public virtual AuditableObject Object { get; set; }
    public string ObjectClassName { get; set; } //ugly
    public string OldObjectXMLData { get; set; }
    public string NewObjectXMLData { get; set; }
    public DateTime Timestamp { get; set; }
}



我不知道如果我需要在 ObjectClassName ,因为我可以检查在运行时的对象类型,但是

I am not sure if I need the ObjectClassName as I can check the type of the object at runtime but it's there just in case.

它的存在,以防万一。一旦保存我基本上会序列化对象之前和到各自的属性后,保存一个时间戳,并设置目标 - 即映射FK

On save I would basically serialize the object before and after to the respective properties, save a timestamp, and set the object - ie map the FK.

这是一个丑陋的路要走呢?是否有任何明显的缺陷,从与EF代码优先单个类下降,因为我在做什么?

Is this an ugly way to go about it? Are there any obvious drawbacks to descending from a single class with EF Code First as I am doing?

推荐答案

我想你会需要对象类型的完全限定名称时,脱盐,这样将是强制性的。结果

I think you will need the full-qualified-name of the object's type when desalinizing, so that will be mandatory.

另外序列化对象会因为你的问题。结果假设我们要使用计算策略审核塔克类TacoOject1重新编译后,序列化的数据将被放置在数据的基础上,后来由于我们需要添加另一个属性塔克业务的变化,当我们需要deserilazed TacoOject1我们将获得 TypeMissMatchException (不知道例外名)。

Alternatively serializing the object will be cause you problems.
Assume that we are going to audit TacoOject1 of Taco class using the approch, the serialized data will be put in data base, later due to business changes we need to add another property to Taco, after recompilation when we need to deserilazed TacoOject1 we will get TypeMissMatchException (not sure of exception name).

另一个设计反对使用继承的审核过程。结果
第一:在现实塔克不是 AuditableObject,它由塔可起到滚,使用继承将违反<一个HREF =htt​​p://en.wikipedia.org/wiki/Liskov_substitution_principle。相对=nofollow>里氏替换原则。结果
:您不能使用多重继承,认为如果我们有一个TacoSupperClass,我们怎么可能审核塔克然后

Another design objection is using inheritance for audit process.
First: In reality Taco is not a AuditableObject , Its a roll played by Taco, using inheritance will violate Liskov Substitution Principle.
Second: You can not use multiple inheritance, think that if we had a TacoSupperClass, how we could audit Taco then?

如果我要去哪里,以设计恰当的审计过程中,我会使用的实体 - 属性 - 值模型结果使 AuditItem 一个标记接口,并将其重命名为 IAuditableEntity 。结果
有一个名为 AuditableProperty 属性b中所述实体需要被审核将由IAuditableEntity标记将提高我们的流程。结果$ b $ ,需要在审计中partcipated实体的任何财产将由AuditableProperty属性标记

If I where going to design auditing process, I would use Entity–attribute–value model
Making AuditItem a marker interface and rename it to IAuditableEntity.
Having an attribute called AuditableProperty would enhance our process.
Any entity needs to be audited will be marked by IAuditableEntity, any property of the entity needed to be partcipated in audit will be marked by AuditableProperty attribute.

public class Taco : IAuditableEntity 
{ 
  [AuditableProperty]
  public string Seasoning { get; set; } 

  [AuditableProperty]
  public string OtherProperty1 { get; set; } 

  public string OtherProperty2 { get; set; } 

}



审计日志表将有这些列:结果
1 EntityFullTypeName :(字符串)我们将审核不同的实体,该字段将用于获得有意义的报告(强制性的)结果
2. 为ObjectIdentifier :那被操纵,主键或实体的业务重点实体标识符

3. 字段名:(字符串)实体的字段名称结果
4. 的OldValue :(字符串)实体领域的旧值结果
5. 的NewValue :(字符串)实体领域的新值结果
6. TransactionUser :使更改应用程序用户。 (强制性的)结果
7. 的TransactionID :任何操作改变实体将需要有一个唯一的事务ID(如GUID)(强制性),如果关于更改多个域的实体的更新,这些列将为跟踪在更新(transcation)结果
8.所有变化的关键点 CHANGEDATE : 交易日期。 (强制性的)结果
9. 的FieldType :枚举或文本显示字段类型,如文本或双人床。 (必选)

The AuditLog table will have these columns:
1. EntityFullTypeName: (String) We are going to audit different entities, the field will be used to get meaningful reports .(mandatory)
2. ObjectIdentifier: Entity identifier that is being manipulated, primary key or business key of the entity.
3. FieldName: (String) Entity field name.
4. OldValue: (String) Entity field old value.
5. NewValue: (String) Entity field new value.
6. TransactionUser: Application user that makes the change. (mandatory)
7. TransactionID: Any operation changing the entities will need to have a unique transaction ID (like GUID) (mandatory), In case of an update on an entity changing multiple fields,these column will be the key point to trace all changes in the update(transcation)
8. ChangeDate: Transaction date. (mandatory)
9. FieldType: enumeration or text showing the field type like TEXT or Double. (mandatory)

在当Taco1将被更新(或插入),我们将检查是否Taco1类型由IAuditableEntity使用反射标记服务层(使用懒chash到店内反射数据),如果是这样的属性已经改变(我们需要一个单独的数据库调用来获取旧值)结果
例如:

In service layer when Taco1 is going to be updated(or inserted) we will check if Taco1 type is marked by IAuditableEntity using reflection(using a lazy chash to store reflection data), if so which properties have been changed(we need a separate DB call to fetch old values).
e.g :

Taco1 = new Taco(); 
Taco1.Seasoning = "old Seasoning value";
Taco1.OtherProperty1 = "Old Other Property1 value";
Taco1.OtherProperty2 = "Old Other Property2 value";

在保存,现在更新:结果

Saved before,now updating:

Taco1.Seasoning = "New Seasoning value";
Taco1.OtherProperty1 = "New Other Property1 value";
Taco1.OtherProperty2 = "New Other Property2 value";

我们将插入在审计日志两条记录具有相同的TransactionID:

We will insert two records in AuditLog with the same TransactionID:

有了这个方法结果的任何实体(表)可以追溯到结果报告将是可读的结果只有更改将被记录下来。结果

Having this approach
Any entity (table) could be traced
Reports will be readable
Only changes will be logged.

这篇关于EF代码优先继承一个基类来实现轻松historocity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆