Question

I have more than 40 models in my application, and now i am trying to implement logging system. 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. But how can i write a generic method, to insert CRDATE and CRBY, and also deleted boo information ?

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

Was it helpful?

Solution

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.

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.

 public void LogWhatever(IBaseModel logModel)
 {
    //...
 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top