In my database there are 5 fields at the end of each table:
- State
- CreatedDate
- CreatedBy
- ModifiedDate
- ModifiedBy
I am using the entity framework to generate the POCO objects for the database.
I have Dal layer to handle all of the CRUD operations.
It’s a pain to constantly copy and paste the same code to fill in the 5 fields.
I am wondering if anyone has a generic method that can handle any entity object and fill the fields in for me.
You should implement a common base class that has these properties, and derive your POCO classes from that base class.
You can automatically handle setting things like the Create/ModifiedDate and User by overriding SaveChanges() in your context class. That frees the object consumers from the need to set those properties everywhere the classes are consumed.
Here’s an example of that sort of code from one of my projects (in my case, objects that have a LastModified property implement an interface IHasLastModified):