如何在DbContext中设置创建者为实体

问题描述 投票:1回答:1

进一步由用户How to set created date and Modified Date to enitites in DB first approach提出并由用户LP13回答的堆栈溢出问题Ogglas。>>

我正在编写一个测试项目,以学习新的开发方法并碰壁。我正在尝试实施Ogglas提供的答案,但是我不确定如何在AutoFac中注册“包装器”?

Ogglas的代码示例

public interface IEntity
{
    DateTime CreatedDate { get; set; }

    string CreatedBy { get; set; }

    DateTime UpdatedDate { get; set; }

    string UpdatedBy { get; set; }
}

public interface ICurrentUser
{
    string GetUsername();
}

public class ApplicationDbContextUserWrapper
{
    public ApplicationDbContext Context;

    public ApplicationDbContextUserWrapper(ApplicationDbContext context, ICurrentUser currentUser)
    {
        context.CurrentUser = currentUser;
        this.Context = context;
    }
}

public class ApplicationDbContext : DbContext
{

    public ICurrentUser CurrentUser;

    public override int SaveChanges()
    {
        var now = DateTime.Now;

        foreach (var changedEntity in ChangeTracker.Entries())
        {
            if (changedEntity.Entity is IEntity entity)
            {
                switch (changedEntity.State)
                {
                    case EntityState.Added:
                        entity.CreatedDate = now;
                        entity.UpdatedDate = now;
                        entity.CreatedBy = CurrentUser.GetUsername();
                        entity.UpdatedBy = CurrentUser.GetUsername();
                        break;
                    case EntityState.Modified:
                        Entry(entity).Property(x => x.CreatedBy).IsModified = false;
                        Entry(entity).Property(x => x.CreatedDate).IsModified = false;
                        entity.UpdatedDate = now;
                        entity.UpdatedBy = CurrentUser.GetUsername();
                        break;
                }
            }
        }

        return base.SaveChanges();
    }
}

我的AutoFac EFModule

public class EFModule : Module
{
    protected override void Load(ContainerBuilder builder)
    {
        builder.REGISTERWRAPPERCLASS?
        builder.RegisterType(typeof(MyDbContext)).As(typeof(IMyDbContext)).As(typeof(DbContext)).InstancePerLifetimeScope();
        builder.RegisterType(typeof(UnitOfWork)).As(typeof(IUnitOfWork)).InstancePerRequest();
        builder.Register(_ => new HttpClient()).As<HttpClient>().InstancePerLifetimeScope();
    }
}

我已使用以下教程作为设置项目的指南Tutorial Guide Project我非常感谢您提供的任何帮助。谢谢。

通用存储库

public abstract class GenericRepository<T> : IGenericRepository<T> where T : BaseEntity
{
    protected DbContext GenericDbContext;
    protected readonly IDbSet<T> GenericDbset;

    protected GenericRepository(DbContext genericDbContext)
    {
        GenericDbContext = genericDbContext;
        GenericDbset = genericDbContext.Set<T>();
    }

堆栈溢出问题的进一步说明如何设置创建日期和修改日期以在数据库LP13中提出并由用户LP13提出并由用户Ogglas回答。我正在编写一个测试项目以学习...

c# dependency-injection autofac wrapper
1个回答
0
投票
builder.RegisterType<ApplicationDbContextUserWrapper>().AsSelf().
© www.soinside.com 2019 - 2024. All rights reserved.