concept EPENDENCY in category .net

appears as: EPENDENCY
Dependency Injection in .NET

This is an excerpt from Manning's book Dependency Injection in .NET.

Introducing SEAMS into an application is extra work, so you should only do it when it’s necessary. There may be more than one reason it’s necessary to isolate a DEPENDENCY behind a SEAM, but they’re closely related to the benefits of loose coupling, discussed in section 1.2.2.

Such DEPENDENCIES can be recognized by their tendency to interfere with one or more of these benefits. They aren’t stable because they don’t provide a sufficient foundation for applications, and I call them VOLATILE DEPENDENCIES for that reason. A DEPENDENCY should be considered Volatile if any of the following criteria is true:

  • The DEPENDENCY introduces a requirement to set up and configure a runtime environment for the application. A relational database is the archetypical example: if we don’t hide the relational database behind a SEAM, we can never replace it by any other technology. It also makes it hard to set up and run automated unit tests. Databases are a good example of BCL types that are VOLATILE DEPENDENCIES: even though LINQ to Entities is a technology contained in the BCL, its usage implies a relational database. Other out-of-process resources such as message queues, web services, and even the file system fall into this category. Please note that it isn’t so much the concrete .NET types that are Volatile, but rather what they imply about the runtime environment. The symptoms of this type of DEPENDENCY are lack of late binding and extensibility, as well as disabled TESTABILITY.
  • The DEPENDENCY doesn’t yet exist, but is still in development. The obvious symptom of such dependencies is the inability to do parallel development.
  • The DEPENDENCY isn’t installed on all machines in the development organization. This may be the case for expensive third-party libraries, or dependencies that can’t be installed on all operating systems. The most common symptom is disabled TESTABILITY.
  • When a class requires an instance of a DEPENDENCY to work at all, we can supply that DEPENDENCY through the class’s constructor, enabling it to store the reference for future (or immediate) use.

    The class that needs the DEPENDENCY must expose a public constructor that takes an instance of the required DEPENDENCY as a constructor argument. In most cases, this should be the only available constructor. If more than one DEPENDENCY is needed, additional constructor arguments can be used.

    Listing 4.1. CONSTRUCTOR INJECTION

    The DEPENDENCY (in the previous listing that would be the abstract DiscountRepository class) is a required constructor argument . Any client code that doesn’t supply an instance of the DEPENDENCY can’t compile. However, because both interfaces and abstract classes are reference types, a caller can pass in null as an argument to make the calling code compile; we need to protect the class against such misuse with a Guard Clause[2].

    The caller supplies the DEPENDENCY as a method parameter in each method call. It can be as simple as this method signature:

    public void DoStuff(ISomeInterface dependency)

    Often, the DEPENDENCY will represent some sort of context for an operation that’s supplied alongside a “proper” value:

    public string DoStuff(SomeValue value, ISomeContext context)

    In this case, the value parameter represents the value on which the method is supposed to operate, whereas the context contains information about the current context of the operation. The caller supplies the DEPENDENCY to the method, and the method uses or ignores the DEPENDENCY as it best suits it.

    If the service uses the DEPENDENCY, it should be sure to test for null references first, as shown in the following listing.

    PROPERTY INJECTION should only be used when the class you’re developing has a good LOCAL DEFAULT and you still want to enable callers to provide different implementations of the class’s DEPENDENCY.

    PROPERTY INJECTION is best used when the DEPENDENCY is optional.