1

seen lots of questions about it without an answer but how do you use DI in a class library using Castle Windsor? People online are saying it's a bad thing/ 'Smell' but how else do you avoid concrete implementations and write unit tests please? I have an example below using NUnit and NSubstitute.

Any help much appreciated.

Many thanks,

Feature code

public class PassengerService
{
    private IPassengerRepository _passengerRepository;

    public PassengerService(IPassengerRepository passengerRepository)
    {
        _passengerRepository = passengerRepository;
    } 
}

Unit test

public class PassengerServiceTests
{
    private PassengerService _passengerService;
    private IPassengerRepository _passengerRepository;

    [SetUp]
    public void Setup()
    {
        _passengerRepository = Substitute.For<IPassengerRepository>();

        _passengerService = new PassengerService(_passengerRepository);
    }

    [Test]
    public void Example_Test()
    {
        var p = _passenger.GetPassenger(Arg.Any<int>());

        // Assert
        _passengerRepository.Receive(1).Get(...);
    }
}

Update

Class libraries don't have a Global.asax file as far as I'm aware so I'm not sure where to write the following Castle Windsor component registrations, i.e.

Registration

container.Register(Component.For<IPassengerRepository>().ImplementedBy<PassengerRepository>().LifestyleTransient());
Jamie
  • 321
  • 1
  • 6
  • 18
  • What exactly is your querstion? I think this is absoluetly fine injected. I´m not familiar with Windsor, however there of course IS a place where you have to set your dependencies. Doing this during setup is absolutely acceptable in this case. – MakePeaceGreatAgain Jan 08 '16 at 09:30
  • Is `IPassengerRepository` something you would like users of your Class Library to be able to provide or is it an implementation detail of your class library and can't be set outside of library? – Michael Jan 08 '16 at 09:30
  • okay great, @himbrombeere if it's fine where do I setup the component registration (shown in my question update) – Jamie Jan 08 '16 at 09:36
  • Why are you using castle and NSsubstituite together? Don´t they both substitute interfaces by concrete classes in any way? – MakePeaceGreatAgain Jan 08 '16 at 09:38
  • Castle Windsor for my dependency injection in my feature code. I'm using NSubstitute in my unit tests to mock out the components I don't care about when writing a particular unit test. – Jamie Jan 08 '16 at 09:45
  • Where have you seen people say that this is code smell? With the library that the company I work for wrote we have the initialization step take an IoC so that the application using the library can request the library to step itself up within the IoC provided. I wouldn't say that this is code smell personally. – Stephen Ross Jan 08 '16 at 09:48
  • http://stackoverflow.com/questions/10546920/what-is-best-practise-when-instantiating-a-castle-windsor-container-in-a-class-l – Jamie Jan 08 '16 at 09:51
  • @Jamie so what do you not like about the accepted answer there? I'm about to type pretty much the same answer – trailmax Jan 08 '16 at 09:52
  • 1
    So that's saying coupling a specific IoC to your library is a smell, which is correct. But providing a generic method to allow your users to initialize the library internally would be slightly different as I'd see it. See something like http://stackoverflow.com/questions/1831455/what-are-the-best-practices-for-class-libraries-using-dependency-injection-for-i – Stephen Ross Jan 08 '16 at 09:58
  • my question is seeking how to get castle windsor implemented in my class library so that I can dependency inject into my classes where necessary. My problem is because the castle windsor examples tend to be asp.net applications which have a global.asax file. Obviously with a class library you dont have this so my questions is where do you setup castle windsor and lifestyles etc..? Many thanks, – Jamie Jan 08 '16 at 10:01
  • On using Dependency Injection for testing: http://stackoverflow.com/a/1465896/126014 FTR, I think the OP design looks just fine! – Mark Seemann Jan 08 '16 at 11:03

0 Answers0