DI-关注点分离指导

时间:2013-11-12 10:03:05

标签: c# dependency-injection

我可以做一些关于“关注点分离”的指导。我正在编写假日预订应用程序以尝试更好地了解我的DI和TDD知识,我有两个独立的存储库“Allocation”和“PublicHoliday”它们都有自己的业务逻辑类AllocationLogic和PublicHolidayLogic。

AllocationLogic有一个名为CalculateWorkingDays的方法,此方法计算用户必须为给定范围(分配对象的“开始日期”和“结束日期”属性)分配假期的日期。

PublicHolidayLogic有一个名为PublicHolidaysForTheYear的方法,它可以获得当年的所有银行假期。而不是将此方法中的代码复制到CalculateWorkingDays方法,我想从CalculateworkingDays方法中的PublicHolidayLogic类中调用它。

我的问题是可以在CalculateworkingDays方法中创建publicHolidayLogic类的实例,通过Allocation构造函数传递所需的存储库吗?或者我应该在接口中传递给分配构造函数中的publicholidaylogic。

我希望前者,但我已附上我的代码,任何帮助将不胜感激

- 公众假期班

 public class PublicHolidayLogic : IPublicHolidayLogic
{
    private IPublicHolidayRepository _publicHolidayRepository;

    public PublicHolidayLogic(IPublicHolidayRepository publicHolidayRepo)
    {
        _publicHolidayRepository = publicHolidayRepo;
    }


    public System.Collections.Generic.IEnumerable<Domain.Models.PublicHoliday> 
        PublicHolidaysForTheYear(int year, int countryId)
    {
        var returnedDates = _publicHolidayRepository.Enumerable()
            .Where(t => t.StartDate.Year == year && t.CountryId == countryId).ToList();

        List<Domain.Models.PublicHoliday> _result = new List<Domain.Models.PublicHoliday>();

        foreach(Domain.Models.PublicHoliday p in returnedDates)
        {
            if (!_result.Any(t => t.Name == p.Name && t.StartDate == p.StartDate))
              _result.Add(p);
        }

        return _result.AsEnumerable();
    }
}

- 分配类

 public class AllocationLogic : IAllocationLogic
{
    private IAllocationRepository _allocationRepository;
    private IPublicHolidayRepository _publicHolidayRepository;

    public AllocationLogic(IAllocationRepository allocationRepo, 
                           IPublicHolidayRepository publicHolidayRepository)
    {
        _allocationRepository = allocationRepo;
        _publicHolidayRepository = publicHolidayRepository;
    }

    public int CalculateWorkingDays(Domain.Models.Allocation allocation)
    {
        //TODO A Better way of doing this would be nice.
        List<DateTime> _dates = new List<DateTime>();
        List<DateTime> _result = new List<DateTime>();

        //Monday To Friday Only

        for (DateTime i = allocation.StartDate; i <= allocation.EndDate; i = i.AddDays(1))
        {
            if (i.DayOfWeek != DayOfWeek.Saturday 
                && i.DayOfWeek != DayOfWeek.Sunday 
                && !_dates.Contains(i))

                _dates.Add(i);
        }

        //Remove Bank Holidays
        if (_publicHolidayRepository != null)
        {
            IEnumerable<Domain.Models.PublicHoliday> _holidays 
                = new PublicHolidayLogic(_publicHolidayRepository)
                    .PublicHolidaysForTheYear(allocation.StartDate.Year, allocation.User.CountryId);

            if (_holidays.Count() > 0)
            {
                foreach (DateTime d in _dates)
                {
                    if (!_holidays.Any(t => t.StartDate == d))
                    {
                        _result.Add(d);
                    }
                }
            }
            else
            {
                _result.AddRange(_dates);
            }
        }
        else
        {
            _result.AddRange(_dates);
        }
        return _result.Count;

    }
}

- 分配测试类

[TestClass]
public class AllocationLogicTests
{

    [TestMethod]
    public void CalculateWorkingDaysOnly()
    {
        //Holiday
        var allocation = new Allocation { 
            StartDate = new DateTime(2013, 1, 7), 
            EndDate = new DateTime(2013, 1, 18) 
        };
        var user = new User { CountryId = 1 };

        var allocationLogic = new AllocationLogic(null,null);

        allocation.User = user;
        int result = allocationLogic.CalculateWorkingDays(allocation);

        Assert.AreEqual(10, result);

    }

    [TestMethod]
    public void CalculateWorkingDaysWithBankHoliday()
    {

        //Holiday
        var allocation = new Allocation { 
            StartDate = new DateTime(2013, 1, 7), 
            EndDate = new DateTime(2013, 1, 18) 
        };
        var publicHoliday = new List<PublicHoliday> { 
            new PublicHoliday { CountryId = 1, StartDate = new DateTime(2013, 1, 10), 
            Name = "My Bank Holiday" } 
        };
        var user = new User { CountryId = 1 };

        var mock = new Mock<IPublicHolidayRepository>();
        mock.Setup(s => s.Enumerable()).Returns(publicHoliday.AsEnumerable());


        allocation.User = user;

        var allocationLogic = new AllocationLogic(null,mock.Object);

        int result = allocationLogic.CalculateWorkingDays(allocation);

        Assert.AreEqual(9, result);


    }
}

2 个答案:

答案 0 :(得分:1)

  

我的问题是可以在CalculateworkingDays方法中创建publicHolidayLogic类的实例,通过Allocation构造函数传递所需的存储库吗?或者我应该在接口中传递给分配构造函数中的publicholidaylogic。

后者。您不希望将实现耦合到另一个实现。在你的世界里,你结合了合约,然后继续这样做。

很可能不是传递存储库

public AllocationLogic( IAllocationRepository allocationRepo, 
                        IPublicHolidayRepository publicHolidayRepository )
...

你最终将获得传递服务

public AllocationLogic( IPublicHolidayLogic publicHolidayService )
...

答案 1 :(得分:0)

您将关注点与分离混淆。您正在通过在另一个内创建一个实例来将PublicHolidayLogic与AllocationLogic紧密耦合。

实际上,所有AllocationLogic都需要访问该方法。将AllocationLogic的构造函数更改为具有类型Func<int, int, IEnumerable<PublicHoliday>>的额外参数,并将其存储在具有相同类型的私有字段中,例如,称为_publicHolidayFunc。然后在创建两个类的代码中,执行以下操作:

var publicHoliday = new PublicHolidayLogic(publicHolidayRepo);
var allocationLogic = new AllocationLogic(allocationRepo, 
                                          publicHolidayRepo, 
                                          publicHoliday.PublicHolidaysForTheYear);
...

最后将方法更改为:

if (_publicHolidayRepository != null)
{
    var holidays = _publicHolidayFunc(allocation.StartDate.Year, allocation.User.CountryId);

    if (_holidays.Count() > 0)
    {
        ...

然后使用DI注入所需的方法,将两个类彼此分离。