, . , , ?
, , - .
EmployeeType Manager, IBonusService:
public class EmployeeType : Enumeration
{
public static Func<IBonusService> BonusService { private get; set; }
private static EmployeeType _manager = null;
public static EmployeeType Manager {
get
{
if (_manager == null) _manager = new ManagerType(BonusService());
return _manager;
} }
public static readonly EmployeeType Servant
= new EmployeeType(1, "Servant");
public static readonly EmployeeType AssistantToTheRegionalManager
= new EmployeeType(2, "Assistant to the Regional Manager");
private EmployeeType(int value, string displayName) :
base(value, displayName) { }
public virtual decimal BonusSize { get { return 0; } }
private class ManagerType : EmployeeType
{
private readonly IBonusService service;
public ManagerType(IBonusService service) : base(0, "Manager")
{
this.service = service;
}
public override decimal BonusSize {
get { return this.service.currentManagerBonus; } }
}
}
:
[Test]
public void EmployeeType_Manager_HasBonusService()
{
Container container = new Container();
container.Register<IBonusService, BonusServiceStub>();
EmployeeType.BonusService = () => container.GetInstance<IBonusService>();
BonusServiceStub.constructed = false;
container.Verify();
Assert.That(BonusServiceStub.constructed, Is.True);
EmployeeType manager = EmployeeType.Manager;
Assert.That(manager.BonusSize == 999m);
}
public class BonusServiceStub : IBonusService
{
public static bool constructed = false;
public BonusServiceStub() { constructed = true; }
public decimal currentManagerBonus { get { return 999m; } }
}
Func<IBonusService> IBonusService , ( ..) .