我想注入构造函数参数IActionLogger actionLogger,但希望其他参数largeBucket、smallBucket和amountToRetrieve是上下文敏感的(不确定这是否是正确的术语)。
问题:
我是否应该将这些构造函数参数设置为自动属性,并将IActionLogger actionLogger参数保留在构造函数中?
基本上,计算会根据largeBucket、smallBucket和amountToRetrieve变量而有所不同?我把这些变量放在构造函数中,因为我需要事先做一些设置。
public class BucketActionsHandler : IBucketActionsHandler
{
private List<IAction> _actions = new List<IAction>();
private Bucket _largeBucket;
private Bucket _smallBucket;
private IActionLogger _actionLogger;
private int _amountToRetrieve;
public BucketActionsHandler(Bucket largeBucket, Bucket smallBucket, int amountToRetrieve, IActionLogger actionLogger)
{
_largeBucket = largeBucket;
_smallBucket = smallBucket;
_amountToRetrieve = amountToRetrieve;
_actionLogger = actionLogger;
_actions.Add(new LastAction(largeBucket, smallBucket, amountToRetrieve));
_actions.Add(new EmptySmallerBucketAction(largeBucket, smallBucket, amountToRetrieve));
_actions.Add(new EmptyLargeBucketAction(largeBucket, smallBucket, amountToRetrieve));
_actions.Add(new FillLargeBucketAction(largeBucket, smallBucket, amountToRetrieve));
_actions.Add(new FillSmallBucketAction(largeBucket, smallBucket, amountToRetrieve));
_actions.Add(new TransferToLargeBucketAction(largeBucket, smallBucket, amountToRetrieve));
_actions.Add(new TransferToSmallBucketAction(largeBucket, smallBucket, amountToRetrieve));
}
private IAction GetNextAction()
{
foreach (var action in _actions)
{
if (action.SatisfiedCondition())
{
return action;
}
}
return null;
}
public void CalculateSteps()
{
IAction nextAction;
do
{
nextAction = GetNextAction();
nextAction.Execute();
if (nextAction == null)
{
throw new InvalidOperationException("No valid action available");
}
} while(!(nextAction is LastAction));
}
}
我应该让这些构造函数参数成为一个自动属性吗
不,因为这将允许您在注入或创建该服务后更改该服务,这是一件坏事,因为服务应该是无状态的,或者至少其内部状态更改不应该影响应用程序的正确性。当您更改服务的状态时,应用程序代码会强制此服务为瞬态服务(每次请求时都应该注入新实例),而应用程序不应该在意。这将控制和决定哪些终身服务脱离了Composition Root(应用程序的启动路径),这阻碍了可维护性。
相反,使用一个工厂:
public interface IBucketActionsHandlerFactory
{
IBucketActionsHandler Create(
Bucket largeBucket,
Bucket smallBucket,
int amountToRetrieve);
}
您可以将这个工厂注入到需要它的服务中,并让该服务提供适当的上下文变量:
public class SomeService
{
private IBucketActionsHandlerFactory factory;
private IBucketRepository repository;
public SomeService(IBucketActionsHandlerFactory factory,
IBucketRepository repository)
{
this.factory = factory;
this.repository = repository;
}
public void Handle(int amountToRetrieve)
{
var largeBucket = this.repository.GetById(LargeBucketId);
var smallBucket = this.repository.GetById(SmallBucketId);
var handler = this.factory.Create(largeBucket, smallBucket,
amountToRetrieve);
handler.CalculateSteps();
}
}
工厂将控制创建新的IBucketActionsHandler
实现:
public class BucketActionsHandlerFactory
: IBucketActionsHandlerFactory
{
private Container container;
public class BucketActionsHandlerFactory(
Container container)
{
this.container = container;
}
public IBucketActionsHandler Create(
Bucket largeBucket, Bucket smallBucket,
int amountToRetrieve)
{
return new BucketActionsHandler(
largeBucket, smallBucket, amountToRetrieve,
this.container.Get<IActionLogger>());
}
}
BucketActionsHandlerFactory
应该是CompositionRoot的一部分,在这种情况下,可以将容器/内核注入这个工厂(它是DI基础设施的一部分)。
通过这种方式,应用程序不知道它得到了什么类型的处理程序,但仍然能够在其当前上下文中获得BucketActionsHandler
。
或者,可以将largeBucket
、smallBucket
和amountToRetrieve
变量提供给CalculateSteps
方法。这允许您消除对工厂的需求:
public class BucketActionsContext
{
public Bucket LargeBucket { get; set; }
public Bucket SmallBucket { get; set; }
public int AmountToRetrieve { get; set; }
}
public class BucketActionsHandler : IBucketActionsHandler
{
private IActionLogger _actionLogger;
public BucketActionsHandler(IActionLogger actionLogger)
{
_actionLogger = actionLogger;
}
public void CalculateSteps(
BucketActionsContext context)
{
IAction nextAction;
do
{
nextAction = this.GetNextAction(context);
if (nextAction == null)
{
throw new InvalidOperationException(
"No valid action available");
}
nextAction.Execute();
}
while(!(nextAction is LastAction));
}
private IAction GetNextAction(
BucketActionsContext context)
{
return (
from action in this.GetActions(context)
where action.SatisfiedCondition()
select action)
.FirstOrDefault();
}
private IEnumerable<IAction> GetActions(
BucketActionsContext context)
{
Bucket largeBucket = context.LargeBucket;
Bucket smallBucket = context.SmallBucket;
int amountToRetrieve = context.AmountToRetrieve;
yield return new LastAction(largeBucket, smallBucket, amountToRetrieve);
yield return new EmptySmallerBucketAction(largeBucket, smallBucket, amountToRetrieve);
yield return new EmptyLargeBucketAction(largeBucket, smallBucket, amountToRetrieve);
yield return new FillLargeBucketAction(largeBucket, smallBucket, amountToRetrieve);
yield return new FillSmallBucketAction(largeBucket, smallBucket, amountToRetrieve);
yield return new TransferToLargeBucketAction(largeBucket, smallBucket, amountToRetrieve);
yield return new TransferToSmallBucketAction(largeBucket, smallBucket, amountToRetrieve);
}
}
您可以手动解析IActionLogger
并手动将其注入构造函数,同时适当地传递其他参数
或
在解析BucketActionsHandler
时,您可以进行依赖项重写(这就是Unity中的调用方式),这将迫使它使用传递的值作为注入的依赖项。