为每个 setter 添加 log 方法
我有 5 个使用实体框架加载的实体。他们在这里:
所有实体均继承自:
(每个实体由具有上述属性的类表示。所有实体都继承 Transmission 实体)。
正如您所看到的,一些实体具有共同的属性。但是属性 WorkerId、WorkerPersonalId、VehicleId、VehicleNumber、SubcontractorId 有特殊的 SET 方法,因此为了封装更新逻辑,我使用这些属性设置器实现创建了 WorkerVehicleTransmission 类。现在每个传输都使用 WorkerVehicleTransmission。
现在我有一个新的需求。我需要记录每个属性更改。为此,我有 Log() 方法。例如,我需要当用户进行 cargoStorage.Weight=8;
时,将调用 Log() 来记录此更改。
重要问题:我需要找到一种解决方案,其中实体的创建(例如通过实体框架)不会记录。
如何整合新需求?
这个问题是我在这里询问的示例的真正需要: 如何解决此代码重复+添加另一个方法
I have 5 entities that are loaded using the entity framework. Here they are:
All of the entities are inherited from:
(Each entity represented by a class with the properties described above. Al entities inherit Transmission entity).
As you can see, there are common properties in some of the entities. But the properties WorkerId, WorkerPersonalId, VehicleId, VehicleNumber, SubcontractorId has special methods for SET so in order to encapsulate the logic of update I created WorkerVehicleTransmission class with those properties setters implementation. Each transmission now uses the WorkerVehicleTransmission.
Now I have a new need. I need to log each property change. For that I have the Log() method. For eaxmple, I need that when the user makes cargoStorage.Weight=8;
there will be a call to Log() that will log this change.
Importent issue: I need to find a solution where the creation of an entity (by the entity framework for example) will not log.
How can I integrate the new need?
This question is the real need for the example I ask about here: how to solve this code duplication + add another method
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您可以将处理程序附加到 Transmission 基类中的 PropertyChanged 事件,这将使您能够在任何子类中的属性发生更改时调用 Log 方法。
INotifyPropertyChanged.PropertyChanged 事件
但是,当进行任何更改(包括实体框架创建对象时),因此只完成了一半。
编辑
如果您在 Transmission 类中创建一个新属性(布尔标志),您可以在数据访问对象例程中使用它来设置是否应启用日志记录。该标志仅在每个对象上的任何实体框架活动完成后才设置,因此记录的唯一属性更改是与您的代码相关的属性更改。
这不是一个优雅的解决方案,但我看不到任何其他方法。编辑
刚刚查看了 EntityObject 基本成员,有一个名为 EntityState 的属性(枚举)。
EntityObject.EntityState 属性
当实体框架创建实体(未附加到对象上下文)时,属性设置为“分离”,并将其值更改为“已添加”、“已删除”、“已修改”或“未更改”添加后(取决于对象的状态)。
通过检查该值是否为“Detached”以外的任何值,您可以确定是否应启用日志记录。
As a start you could attach a handler to the PropertyChanged event in the Transmission base class which will enable you to call the Log method whenever a property changes in any of your sub classes.
INotifyPropertyChanged.PropertyChanged Event
This however will fire when any change is made, including when the Entity Framework creates the objects, so is only half way there.
Edit
If you create a new property within the Transmission class (a boolean flag) you could use this in your data access object routines to set whether logging should be enabled.This flag is only ever set after any Entity Framework activity on each object has been completed therefore the only Property changes logged are those relating to your code.
Not an elegant solution but I cannot see any other way.Edit
Just had a look at the EntityObject base members and there is an Property (Enumeration) named EntityState.
EntityObject.EntityState Property
This property is set to "Detached" when the entity is being created (Unattached to the object context) by the Entity Framework and changes its value to "Added", "Deleted", "Modified" or "Unchanged" after it is added (depending on the state of the object).
By checking if the value is anything other than "Detached" you could then determine whether logging should be enabled.