C# 如何遵循商业代码的DRY原则

C# 如何遵循商业代码的DRY原则,c#,dry,C#,Dry,考虑以下代码: public void AddPrice(int exchangeTypeId, decimal price) { GoldPrice lastPriceValue = UnitOfWork.GoldPrice.Last(x => x.GoldId == exchangeTypeId); if (lastPriceValue == null || lastPriceValue.Value != price) {

考虑以下代码:

    public void AddPrice(int exchangeTypeId, decimal price)
    {
        GoldPrice lastPriceValue = UnitOfWork.GoldPrice.Last(x => x.GoldId == exchangeTypeId);
        if (lastPriceValue == null || lastPriceValue.Value != price)
        {
            UnitOfWork.GoldPrice.Add(
                new GoldPrice
                {
                    Id = Guid.NewGuid().ToString(),
                    EntryDate = DateTime.Now,
                    Value = price,
                    GoldId = exchangeTypeId,
                }
                );
        }
        else
        {
            lastPriceValue.EntryDate = DateTime.Now;
        }

        UnitOfWork.Commit();
    }

在上面的代码中,我有一些业务,例如检查NULL,得到最后的价格……所以请考虑如下:

    public void AddPrice(int exchangeTypeId, decimal price)
    {

        CurrencyPrice lastPriceValue = UnitOfWork.CurrencyPrice.Last(x => x.CurrencyId == exchangeTypeId);
        if (lastPriceValue == null || lastPriceValue.Value != price)
        {
            UnitOfWork.CurrencyPrice.Add(
                new CurrencyPrice
                    {
                        Id = Guid.NewGuid().ToString(),
                        EntryDate = DateTime.Now,
                        Value = price,
                        CurrencyId = exchangeTypeId,
                    }
                );
        }
        else
        {
            lastPriceValue.EntryDate = DateTime.Now;
        }

        UnitOfWork.Commit();
    }

我有两个业务完全相同的函数。如果业务发生变化,我应该更改每个add price函数,那么我如何才能遵循业务代码的DRY原则?

看看。

您可以重构出一组基本的抽象来获取价格

public interface IPrice
{
    string Id { get; set; }
    DateTime EntryDate { get; set; }
    decimal Value { get; set; }
    int ExchangeTypeId { get; set; }
}

public interface IUnitOfWork<T>
    where T: IPrice
{
    T GetLatest(int exchangeTypeId);
    void Add(T price);
    void Commit();
}

public interface IUnitOfWorkFactory
{
    void Register<T>() where T: IPrice;
    IUnitOfWork<T> Get<T>() where T: IPrice;
}

public void AddPrice<T>(int exchangeTypeId, decimal price)
    where T: IPrice, new()
{
    IUnitOfWork<T> unitOfWork = _unitOfWorkFactory.Get<T>();
    IPrice lastPriceValue = unitOfWork.GetLatest(exchangeTypeId);

    if (lastPriceValue == null || lastPriceValue.Value != price)
    {
        unitOfWork.Add(
            new T
            {
                Id = Guid.NewGuid().ToString(),
                EntryDate = DateTime.Now,
                Value = price,
                ExchangeTypeId = exchangeTypeId,
            });
    }
    else
    {
        lastPriceValue.EntryDate = DateTime.Now;
    }

    unitOfWork.Commit();
}
公共接口IPrice
{
字符串Id{get;set;}
DateTime入口日期{get;set;}
十进制值{get;set;}
int-ExchangeTypeId{get;set;}
}
公共接口工作单元
其中T:IPrice
{
T GetLatest(int-exchangeTypeId);
无效添加(T价格);
无效提交();
}
公共接口IUnitOfWorkFactory
{
无效寄存器(),其中T:IPrice;
i工作单元Get(),其中T:i价格;
}
public void AddPrice(int-exchangeTypeId,十进制价格)
其中T:IPrice,new()
{
IUUnitOfWork unitOfWork=\U unitOfWorkFactory.Get();
IPrice lastPriceValue=unitOfWork.GetLatest(exchangeTypeId);
如果(lastPriceValue==null | | lastPriceValue.Value!=price)
{
工作单元。添加(
新T
{
Id=Guid.NewGuid().ToString(),
EntryDate=DateTime.Now,
价值=价格,
ExchangeTypeId=ExchangeTypeId,
});
}
其他的
{
lastPriceValue.EntryDate=DateTime.Now;
}
unitOfWork.Commit();
}

GoldPrice和CurrencyPrice是从一个共同的祖先继承的还是实现了一个相同的接口?Jefri,我不是要你更改它们的定义,只是想检查它们是否有一个共同的祖先(F12是你的朋友;))一般来说,不鼓励只提供链接的答案。尝试添加链接中包含内容的摘要。尤其是因为链接可能会出错。