MVC业务逻辑助手

时间:2012-10-29 00:09:45

标签: c# asp.net-mvc entity-framework

我有一些常用功能适用于我的应用程序,以便在操作发生时更新数据库的特定部分(审计跟踪,修改日期等)。我将以AuditTrail为例。

我应该在哪里存储这些功能?

目前我将它们存储在dbcontext类

//... my db context class ...

public bool AddAuditEntry(int ID, string objectName)
{
    // Here I create a new AuditTrail object, assign values then insert into db.
    // This mode doesn't have a controller.
}

// We also have a table that keeps track of modified state for 
// client side caching (nothing I have control over)
public bool ModifyObject(int ID)
{
    // Here I mark the object id with modified date then save to db
    // This particular model doesn't have a controller either.
}

我认为它们应该属于模型,但我不太确定如何实现它。将它们放在控制器中并不是最好的选择,因为其中一些只涉及可能没有控制器的特定模型类。

我在模型中存在的问题是更新实体的最佳方法是什么?

1 个答案:

答案 0 :(得分:0)

我不确定这是否与其他人一样,但实际上我有两种型号。我的商业模式包含这些功能和验证。像帐户余额这样的东西不能小于零或类似的东西。完成所有操作后,模型将被转换为数据库模型,如有必要,数据库模型将负责数据库级验证。