1

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

1 Answer 1

1

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)
 {
    //...
 }
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for your help. But why i need to use interface ? I asked to learn. And ie, i have a model called, employee. How can i call the LogWhatEver() method ?
Interfaces allow you to pass around different objects that only have the subset of the signature you want. You could call LogWhatever with and pass in anything that implements the interface. For example: BaseModel bm = new BaseModel(){CrDate=DateTime.Now,UserID=123,ApplicationUser = new ApplicationUser()}; or AnotherModelThatImplementsIBaseModel abm = new AnotherModelThatImplementsIBaseModel() {...}; LogWhatever(bm); LogWhatever(abm);
Still i have problems. If we dont have IBaseModel, i think , i can still call BaseModel bm = new BaseModel(){CrDate=DateTime.Now,UserID=123,ApplicationUser = new ApplicationUser()}; We are creating a basemodel and pass it into the method LogWhatever which is getting IBaseModel...
Also, after inherited all of my models from basemodel, should i use generic repository pattern ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.