Benim varlığımdan bazılarına uygulamamda, varlık üzerinde değişiklik bilgilerini (kullanıcı adı ve değişiklik zamanı) tutmak istiyorum. Bunu başarmak için bir arayüz oluşturdum; Entity Framework Saklama Modifikasyonu Bilgi Tasarım Önerisi?
/// <summary>
/// Marks a trackable entity
/// </summary>
public interface ITrackableEntity
{
/// <summary>
/// Gets or sets the date the entity was modified
/// </summary>
DateTime? ModifiedOn { get; set; }
/// <summary>
/// Gets or sets the user who modified the entity
/// </summary>
string ModifiedBy { get; set; }
}
ve bu arabirimi uygulayan bir temel sınıf oluşturdu;
[Serializable]
public abstract class BaseTrackableEntity : BaseEntity, ITrackableEntity
{
/// <summary>
/// Gets or sets the date the entity was modified
/// </summary>
public DateTime? ModifiedOn { get; set; }
/// <summary>
/// Gets or sets the user who modified the entity
/// </summary>
public string ModifiedBy { get; set; }
}
Ben DBContext
devralınan benim kendi özel DBContext sınıf oluşturulur ve aşağıdaki şekilde SaveChanges işlevi overrode; ...
foreach (DbEntityEntry entry in this.ChangeTracker.Entries())
{
...
...
else if(entry.State == EntityState.Modified && entry.Entity is ITrackableEntity)
{
(entry.Entity as ITrackableEntity).ModifiedOn = DateTime.UtcNow;
(entry.Entity as ITrackableEntity).ModifiedBy = this._webHelper.GetLoggedInUserName();
}
}
...
return base.SaveChanges();
Bu
aslında çalışıyor ama varlık çerçevesi için yeni beri bu bunu yapmak için en iyi yoldur emin değilim. Bu noktada sorum şu; Bu tasarım gelecekte sorunlara neden olacak mı? Daha iyi bir çözüm var mı?
Bu soru codereview.stackexchange.com adresinde daha iyi olur. –