How to convert this code so it now uses the Dependency Injection pattern?
Ok, So I have the following situation. I originally had some code like this:
public class MainBoard {
private BoardType1 bt1;
private BoardType2 bt2;
private BoardType3 bt3;
...
private readonly Size boardSize;
public MainBoard(Size boardSize) {
this.boardSize = boardSize;
bt1 = new BoardType1(boardSize);
bt2 = new BoardType2(boardSize);
bt3 = new BoardType3(boardSize);
}
}
Now, I decided to refactor that code so the classes' dependencies are injected, instead:
public class MainBoard {
private IBoardType1 bt1;
private IBoardType2 bt2;
private IBoardType3 bt3;
...
private Size boardSize;
public MainBoard(Size boardSize, IBoardType1 bt1, IBoardType2 bt2, IBoardType3 bt3) {
this.bt1 = bt1;
this.bt2 = bt2;
this.bt3 = bt3;
}
}
My question is what to do about Board Size? I mean, in the first case, I just passed the class the desired board size, and it would do everything to create the other kinds of boards with the correct size. In the dependency injection case, that might not be more the case. What do you guys do in this situation? Do you put any kind of checking on the MainBoard
's constructor so to make sure that the correct sizes are being passed in? Do you just assume the class' client will be responsible enough to pass the 3 kinds of boards with the same size, so there is no trouble?
Edit
Why am I doing this? Because I need to Unit-Test MainBoard. I need to be able to set the 3 sub-boards in certain states so I can test that my MainBoard is doing what I expect it to.
Thanks
I would say the boardSize
parameter is unnecessary in the second case, but I would add an assertion to ensure that the 3 board sizes are really equal.
But overall, the second case seems dubious to me. I would suggest the first approach, unless you really really need to inject different kinds of boards to the main board. Even so, I would consider using eg a board factory instead of passing 3 board parameters to the constructor, eg
public interface IBoardBuilderFactory {
public IBoardType1 createBoardType1(Size boardSize);
public IBoardType2 createBoardType2(Size boardSize);
public IBoardType3 createBoardType3(Size boardSize);
}
This would ensure the consistency of the 3 boards regarding both "board family" and size.
We should know more about the context / domain model, specicifcally the relationship of the main board and the child boards in order to decide whether or not DI is the right choice here.
It is questionable whether Dependency Injection (or Dependency Inversion) should be applied in this case at all. It appears to me that your MainBoard
is responsible for managing the lifecycle of the BoardTypes
it created in the first sample. If you now do inject your BoardTypes this responsibility must be handled by the consumers of MainBoard
.
It is a tradeof between flexibility and additional duties on the consumer side.
On the other hand, if it is sensible to have the lifecycle of BoardType
s be handled externally , it is fine to use Dependency Inversion. Your constructor on MainBoard
then needs to ensure it's dependencies are properly statisfied . This would include checking that their Size
is equal.
The main advantage of dependecy injection is the insulation from changes to the objects being injected. So in your case the one variable apparent is the size. You would inject the boards into the main board so that the main board no longer needs to know or worry about the size. Also, unless your application needs to maintain 3 distinct behaviors between the different board types I would suggest using a single abstract definition for the board type interface.
public class MainBoard {
private IBoardType bt1;
private IBoardType bt2;
private IBoardType bt3;
public MainBoard(IBoardType bt1, IBoardType bt2, IBoardType bt3) {
this.bt1 = bt1;
this.bt2 = bt2;
this.bt3 = bt3;
}
}
It becomes the responsibility of the thing that does the injection (injection framework or assembling code) to ensure these boards are given the proper size. This can be done in a number of ways one example being the main board and injected boards all derive their size from a single external source. Maybe your app, in this case, sizes the injected boards relative to the main board.
So you could have external logic such as:
public class BoardAssembler {
public static MainBoard assembleBoard(Size size) {
Size innerBoardSize = deriveSizeOfInternalBoards(size);
return new MainBoard(new BoardType1(innerBoardSize), new BoardType2(innerBoardSize), new BoardType3(innerBoardSize));
}
}
Essentially what you're after is the inversion of the construction logic wherever MainBoard is constructed. Start there and extract everything into a factory or some evil singleton factory or static method. Ask yourself, "where is the MainBoard created?" Also ask, "what compoenents and parameters are needed?" Once you've moved all the instantiation logic into a factory it may become simpler to maintain Mainboard and all of its dependencies.
链接地址: http://www.djcxy.com/p/77756.html上一篇: 依赖注入容器
下一篇: 如何将此代码转换为现在使用依赖注入模式?