简体   繁体   English

Asp.Net MVC将相同的数据插入所有模型

[英]Asp.Net MVC Inserting Same Data Into All Models

I have more than 40 models in my application, and now i am trying to implement logging system. 我的应用程序中有40多个模型,现在我正在尝试实现日志记录系统。 For the first phase, i want to keep the information of "Created By" and "Created Date" information for all the rows in all the tables. 对于第一阶段,我想保留所有表中所有行的“创建者”和“创建日期”信息。

I think i can create a base class which includes the attributes"CRDATE" and "CRBY", and create all of my models from this base class. 我想我可以创建一个包含“ CRDATE”和“ CRBY”属性的基类,并从该基类创建我的所有模型。 But how can i write a generic method, to insert CRDATE and CRBY, and also deleted boo information ? 但是我如何编写一个通用方法来插入CRDATE和CRBY,并删除boo信息?

Here is my base-model: 这是我的基本模型:

 public class BaseModel
    {
        public DateTime CrDate { get; set; }

        public int UserId { get; set; }
        public ApplicationUser User { get; set; }

        public bool IsDeleted { get; set; }
    }

Should i use generic repository actions ? 我应该使用通用存储库操作吗?

Regards 问候

Have your base class inherit from an Interface (such as IBaseModel) that has contains CrDate and CrBy (maybe name as well, for logging purposes), but not the specialized information from your other classes. 让您的基类继承自包含CrDate和CrBy(也可能出于记录目的而命名)的接口(例如IBaseModel),而不继承其他类的专门信息。

It would look something like: 它看起来像:

public class BaseModel: IBaseModel
{
    public DateTime CrDate { get; set; }

    public int UserId { get; set; }
    public ApplicationUser User { get; set; }

    public bool IsDeleted { get; set; }
}

public interface IBaseModel
    {
        DateTime CrDate { get; set; }
        ApplicationUser User { get; set; }
    }

Then your generic logging class can take IBaseModel as a parameter and get those two properties. 然后,您的通用日志记录类可以将IBaseModel作为参数并获取这两个属性。

 public void LogWhatever(IBaseModel logModel)
 {
    //...
 }

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

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