开发者

Question About NerdDinner Controller Constructors

I've been looking at the Nerd Dinner app, 开发者_StackOverflowmore specifically how it handles its unit tests.

The following constructors for the RSVPController are confusing my slightly

public RSVPController()
    : this(new DinnerRepository()) {
}

public RSVPController(IDinnerRepository repository) {
    dinnerRepository = repository;
}

From what I can tell the second one is used by the unit tests so it can use Fake repositories. What I cant work out is what the first constructor does. It doesn't seem to ever set the dinnerRepository variable, it seems to imply its inheriting from something but I really don't get it.

Can anyone explain?

Thanks


The first constructor is passing the "default" IDinnerRepository implemenation (namely DinnerRepository) to the second constructor.

It does so because that empty constructor is used by the MVC Controller Factory. In other words, when the application actually executes, it uses the first constructor with the default repository implementation. When a unit test wants to test the Controller, then an mocked IDinnerRepository can be passed to the controller.


The first constructore calls the second constructur with a new DinnerRepository. Thats what this is doing:

: this(new DinnerRepository())

It calls the second constructor and assigns the dinnerRepository variable with the new instance of the DinnerRepository.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜