【问题标题】:How can follow DRY principle for business code业务代码如何遵循 DRY 原则
【发布时间】:2013-06-29 00:28:58
【问题描述】:

考虑这段代码:

    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();
    }

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

【问题讨论】:

  • GoldPrice 和 CurrencyPrice 是继承自一个共同的祖先还是实现相同的接口?
  • Jefri,我不是要你改变他们的定义,只是要检查他们是否有共同的祖先(F12 是你的朋友;))

标签: c# dry


【解决方案1】:

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

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();
}

【讨论】:

    【解决方案2】:

    看看Strategy Pattern

    【讨论】:

    • 一般来说,不鼓励仅链接的答案。尝试添加链接中包含的内容的摘要。
    • 特别是因为链接可能会变坏。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-08
    • 1970-01-01
    • 2017-03-18
    • 1970-01-01
    • 2020-09-11
    • 1970-01-01
    相关资源
    最近更新 更多