I'm looking for an approach to the following problem:

I have a reusable library. It contains a controller class, in the MVC sense. Let's say it's called UserAdministrationController, and it's for editing users. This class requires an object implementing IUserStorage, for storing its data. The library doesn't need to know the details of how this object works.

Now I want to write an application that includes the library. In it, I'll provide an implementation of IUserStorage. To instantiate this, I'll need to pass it some dependencies, like a database object, and other things that my application only knows about at runtime.

But how would I then pass this object from my application to the library? I'm not instantiating the UserAdministrationController directly from my application; it gets created by the library itself.

Hope this makes sense. I'm trying to keep my description abstract and not specific to a particular framework or language.

  • 1
    It doesn't really make lots of sense. Which language? Commented Dec 2, 2011 at 20:53

3 Answers 3


You'll need to open up the library so that there's a Seam that allows the consumer to (re)define how UserAdministrationController instances are created. This could, for example, be an Abstract Factory that consumers can implement. This doesn't prevent you from providing a default implementation in your library, but enables consumers to extend or change the behavior if they need to.

Here are more details about writing DI frindly libraries: https://stackoverflow.com/questions/2045904/dependency-inject-di-friendly-library/2047657#2047657

(...and in case you get other answers that tell you that a DI Container is the answer: applications use DI Containers; libraries absolutely should not. A DI Container is part of an application's infrastructure, so should not be allowed to invade any library. It's perfectly possible to do DI without a container, so by implementing a library using only the patterns and principles of DI, you leave it up to various applications if the want to use a container at all, and if so, which one.)


The specific answer is fundamentally language specific. The general answer is you'll need to look into dependency injection which handles the job of injecting dependencies (IUserStorage) into your UserAdministrationCntroller.

  • But don't DI frameworks generally define the dependencies statically? Would they allow me to create the IUserStorage at runtime?
    – JW01
    Commented Dec 2, 2011 at 21:09
  • Two ways to skin this -- first, most DI frameworks I'm familiar with allow one to specify much about the instantiation of a dependency. Second you can have different static initialization setups for different environments if that is what it takes. Commented Dec 2, 2011 at 21:46

If the reusable library is an already existing library and it constructs the UserAdministrationController internally, then the library should already have an its own mechanism by which it provides the IUserStorage during initiallization. You just need find out how the library is doing it and use that.

If you were setting out to write the reusable library yourself then there would be a question of choosing DI libraries or language features for doing this, but it sounds like the library is already written and you are are just trying to use it. In that case all those decisions have already been made by the library writers; you just need to find out what they are and use them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.