I'm writing a simple logging mechanism for my app.
I have generic repository:
public class GenericRepository<TEntity>:IRepository<TEntity> where TEntity : class
{
internal Equipment_TestEntities context;
internal DbSet<TEntity> dbSet;
internal Log entry;
public GenericRepository(Equipment_TestEntities context)
{
this.context = context;
this.dbSet = context.Set<TEntity>();
this entry= new Log();
}
public virtual void Insert(TEntity entity)
{
dbSet.Add(entity);
AddLog("insert "+typeof(TEntity)+" "+entity.ToString());
}
private void AddLog(string action)
{
entry.Action = action;
entry.Date = DateTime.Now;
string username = HttpContext.Current.User.Identity.Name;
username = username.Substring(username.LastIndexOf("\\") + 1);
entry.Who = 1;
context.Logs.Add(entry);
}
}
In entry.Action
I want to keep:
- Action eg. Insert
- Which entityUsed eg. User or Role
- Something to identify entity
In 1) I can easily hardcone action
2) I can use TypeOf
and get entity class name
But in 3rd I have a bit problem. In case of insert I can ask db for the newest record but what should I do in Edit/remove cases? Is there any ways to get properties value from those entities?
@Update:
sample part from unitOfWork:
public IRepository<Storage> storageRepository
{
get
{
if (this.StorageRepository == null)
{
this.StorageRepository = new GenericRepository<Storage>(context);
}
return StorageRepository;
}
}
IUnitOfWork: public interface IUnitOfWork : IDisposable { IRepository storageRepository { get; } }
where TEntity : IEntity
) and then define an Id property in that interface? – sroesthis
. You should rather write something like_context = context;
and sorry I don't know the answer for your question. – roskothis.variable
vs_variable
- as long as you're consistent (e.g.this.dbSet
should be used instead of justdbSet
in this case). – dav_i