EF审核到位代码重复

时间:2015-12-03 18:58:28

标签: entity-framework

我有一些非常相似的代码,希望减少重复的数量。

除了DbSet<>之外,以下两种方法在各方面都相似。被操作。一个在PPD上运行,另一个在ChestXRays上运行。

我如何删除重复数量,因为我有更多DbSet<>我需要采取类似行动。

    public CommandResult CreditPpdService(GenericCreditDto vm)
    {
        var insertModified = _hlthOidContext.PPDs
            .AsNoTracking()
            .FirstOrDefault(c => c.PPDId == vm.TheServiceId);
        if (insertModified != null)
        {
            var flagOriginalAsDeleted = _hlthOidContext.PPDs
                .FirstOrDefault(c => c.PPDId == vm.TheServiceId);
            if (flagOriginalAsDeleted != null)
            {
                flagOriginalAsDeleted.IsDeleted = true;
            }
            _hlthOidContext.PPDs.Add(insertModified);
            _hlthOidContext.Entry(insertModified).CurrentValues.SetValues(vm);
            _hlthOidContext.SaveChanges();
        }
        var results = new CommandResult();
        return results;
    }

和另一种方法

        public CommandResult CreditChestXRayService(GenericCreditDto vm)
    {
        var insertModified = _hlthOidContext.ChestXRays
            .AsNoTracking()
            .FirstOrDefault(c => c.ChestXRayId == vm.TheServiceId);
        if (insertModified != null)
        {
            var flagOriginalAsDeleted = _hlthOidContext.ChestXRays
                .FirstOrDefault(c => c.ChestXRayId == vm.TheServiceId);
            if (flagOriginalAsDeleted != null)
            {
                flagOriginalAsDeleted.IsDeleted = true;
            }
            _hlthOidContext.ChestXRays.Add(insertModified);
            _hlthOidContext.Entry(insertModified).CurrentValues.SetValues(vm);
            _hlthOidContext.SaveChanges();
        }
        var results = new CommandResult();
        return results;
    }

其中_hlthOidContext是

public partial class HLTH_OIDContext : DbContext
{
    static HLTH_OIDContext()
    {
        Database.SetInitializer<HLTH_OIDContext>(null);
    }

    public HLTH_OIDContext()
        : base("Name=HLTH_OIDContext")
    {
        this.Configuration.LazyLoadingEnabled = false;
    }

    ...
    public DbSet<ChestXRay> ChestXRays { get; set; }
    public DbSet<PPD> PPDs { get; set; }
    ...

更多信息

    public interface ISoftDelete
{
    bool IsDeleted { get; set; }         
}

public partial class ChestXRay : ISoftDelete { }
...
public partial class PPD : ISoftDelete { }

1 个答案:

答案 0 :(得分:0)

这是一个想法,我将对您当前的代码进行一系列重构。如果&#34; Id&#34;每种类型(ChestXRayIdPPDId等)的属性都相同,因为您也可以将Id提取到公共接口中。

首先将您的DbSets和FirstOrDefault谓词重构为变量:

public CommandResult CreditChestXRayService(GenericCreditDto vm)
{
    DbSet<ChestXRays> dbSet = _hlthOidContext.ChestXRays;
    Expression<Func<ChestXRays, bool>> predicate = c => c.ChestXRayId == vm.TheServiceId;

    var insertModified = dbSet
        .AsNoTracking()
        .FirstOrDefault(predicate);
    if (insertModified != null)
    {
        var flagOriginalAsDeleted = dbSet
            .FirstOrDefault(predicate);
        if (flagOriginalAsDeleted != null)
        {
            flagOriginalAsDeleted.IsDeleted = true;
        }
        dbSet.Add(insertModified);
        _hlthOidContext.Entry(insertModified).CurrentValues.SetValues(vm);
        _hlthOidContext.SaveChanges();
    }
    var results = new CommandResult();
    return results;
}

然后提取泛型类型参数并参数化这些变量。

您需要IsDeleted属性的接口,并将您的泛型类型限制为此接口(从评论看起来,这通过ISoftDelete实现)。

private CommandResult CreditService<T>(GenericCreditDto vm, DbSet<T> dbSet, Expression<Func<T, bool>> predicate) 
    where T : class, ISoftDelete
{
    var insertModified = dbSet
        .AsNoTracking()
        .FirstOrDefault(predicate);
    if (insertModified != null)
    {
        var flagOriginalAsDeleted = dbSet
            .FirstOrDefault(predicate);
        if (flagOriginalAsDeleted != null)
        {
            flagOriginalAsDeleted.IsDeleted = true;
        }
        dbSet.Add(insertModified);
        _hlthOidContext.Entry(insertModified).CurrentValues.SetValues(vm);
        _hlthOidContext.SaveChanges();
    }
    var results = new CommandResult();
    return results;
}

使用您的任何特定类型调用此通用方法:

public CommandResult CreditChestXRayService(GenericCreditDto vm)
{
    return CreditService(vm, _hlthOidContext.ChestXRays, c => c.ChestXRayId == vm.TheServiceId);
}

public CommandResult CreditPpdService(GenericCreditDto vm)
{
    return CreditService(vm, _hlthOidContext.PPDs, c => c.PPDId == vm.TheServiceId);
}