Reputation: 7197
I have a code, where chessboard and piece are injected with interfaces. DiagonalDirections is enumeration. So far, so good. I don't inject dependencies, neither I use hidded dependencies.
private void addToAllowedPositions(IChessboard chessboard, IPiece piece, DiagonalDirections direction)
{
int horizontalIndex = piece.Position.IndexHorizontal;
int verticalIndex = piece.Position.IndexVertical;
while (AreIndexesValid(setHorizontalIndexDirection(direction, ref horizontalIndex), setVerticalIndexDirection(direction, ref verticalIndex)))
{
IPosition currentPosition = new Position(horizontalIndex, verticalIndex);
IPiece currentPiece = chessboard.GetPiece(currentPosition);
if (currentPiece == null)
{
allowedPositions.Add(currentPosition);
}
else if (currentPiece.Color != piece.Color)
{
allowedPositions.Add(currentPosition);
break;
}
else break;
}
}
Problem is with this line of code:
IPosition currentPosition = new Position(horizontalIndex, verticalIndex);
I really don't know how can I use interface here? New Position must be created each time. That is my only dependency, which I don't know how to isolate. I also have a problem, when I want to unit test this piece of code. If anything is changed in Position class, it is immediately being reflected also in this method, on existing unit tests.
Upvotes: 1
Views: 151
Reputation: 99957
You can add a factory method to the IChessboard
interface:
IPosition currentPosition = chessboard.CreatePosition(horizontalIndex, verticalIndex);
Upvotes: 2