We have worked on a project for 1 month and there are 6 entities without any relationship with other entities. They are all simple entities.
We have created 6 different classes for operations on each entity. SaveOrUpdateEntity()
methods of classes are almost same as you think. It is something like that:
public static ErrorType SaveOrUpdateEntity(Entity entity, int userID)
{
try
{
using (DataEntities ctx = new DataEntities())
{
if (entity != null)
{
if (entity.entityID == 0)
{
entity.CreateDate = DateTime.Now;
entity.CreatedBy = userID;
ctx.Entry(entity).State = EntityState.Added;
}
else
{
entity.ModifyDate = DateTime.Now;
entity.ModifiedBy = userID;
ctx.Entry(entity).State = EntityState.Modified;
}
}
ctx.SaveChanges();
}
return ErrorType.NoError;
}
catch (Exception ex)
{
return ErrorType.SaveError;
}
}
It would be very helpful, if SaveOrUpdateEntity()
method is shorter and more generic by overriding SaveChanges()
method. According to other questions, articles, and posts about overriding SaveChanges()
method, implementing interface that stores state of entities is a good solution but I am also wondering other solutions.
Because I am newbie, all answers would be very appreciated.
Thank you.
See Question&Answers more detail:os