DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

Trending

  • Mastering Time Series Analysis: Techniques, Models, and Strategies
  • Exploratory Testing Tutorial: A Comprehensive Guide With Examples and Best Practices
  • Implementing a Serverless DevOps Pipeline With AWS Lambda and CodePipeline
  • Automating the Migration From JS to TS for the ZK Framework

Trending

  • Mastering Time Series Analysis: Techniques, Models, and Strategies
  • Exploratory Testing Tutorial: A Comprehensive Guide With Examples and Best Practices
  • Implementing a Serverless DevOps Pipeline With AWS Lambda and CodePipeline
  • Automating the Migration From JS to TS for the ZK Framework
  1. DZone
  2. Coding
  3. Frameworks
  4. Dependency Injection Frameworks are Not Dependency Injection

Dependency Injection Frameworks are Not Dependency Injection

Dave Bush user avatar by
Dave Bush
·
Mar. 17, 15 · Interview
Like (0)
Save
Tweet
Share
6.13K Views

Join the DZone community and get the full member experience.

Join For Free

as you start your journey down the road of unit testing you will discover that part of what makes code testable is this concept of dependency injection .  as you explore further, you will see people mentioning various dependency injection frameworks. you may naturally assume that to implement dependency injection, you will need to select an use a dependency injection framework.

but, dependency injection has nothing to do with using a dependency injection framework.  the frameworks are there because:

1.  much of our existing code is code that has too many dependencies and the framework helps us break those dependencies without having to refactor too much of our code and

2. to give us a way to easily swap out one object for another when our code is structured in such a way as to not have dependencies at all.

so, what then is dependency injection?

i once heard this maxim that explains it best,

classes should either create stuff, or do stuff, but no one class should do both.

much of our code looks something like this:

private static void receivesamlresponse(
    out samlresponse samlresponse, 
    out string relaystate)
{
    // receive the saml response over the 
    //specified binding.
    xmlelement samlresponsexml;

    serviceprovider.receivesamlresponsebyhttppost(
        httpcontext.current.request, 
        out samlresponsexml, out relaystate);
    samlresponse resp = new samlresponse(samlresponsexml);
    xmlelement samlassertionelement = 
        resp.getsignedassertions()[0];

    // verify the response's signature.
    xmldocument doc = new xmldocument();
    //metadata file path (holds the description key).
    doc.load(httpcontext.current.server.mappath("~/saml.xml"));
    xmlelement root = doc.documentelement;

    if (!samlassertionsignature.verify(
        samlassertionelement, readmetadata(root)))
    {
        samlresponse = null;
        relaystate = null;
        return;
    }
    // deserialize the xml.
    samlresponse = new samlresponse(samlresponsexml);
}

yes, this is real code from a system i worked on.  the original code was written three or four years ago and this specific code is code i was given by another company.  i just used copy and paste inheritance to get it working in our code.

that’s not to say i haven’t written cod that has just as many problems.

there are several things that are wrong with this code, but for now all i want to focus on is the dependency injection issue.

all this code is trying to do is to deserialize the encrypted samlresponse object that was posted to the login form.  at least this code isn’t in the login form!  it has that much going for it.

but here are places where it is dependent on too much:

  • serviceprovider is a static class and called directly.
  • receivesamlresponsebyhttppost is dependent on the request object that we retrieve from httpcontext.
  • i create a new samlresponse object right before i call getsignedassertions()
  • i create a new xmldocument object so i can load the saml.xml file
  • samlassertionsignature is static and called directly

in fact, this method is one huge dependency mess.  and i’m very aware of the mess that it is in because the company we wrote this code for just recently switched providers.  as we tried to get this working, i had no way of testing this code in isolation without adding code directly into this method.  we got it working, but it didn’t have to be that hard.

so, here’s what i’d do to this code.

  • since serviceprovider and samlassertionsignature are calls to a third party api, i would wrap them in a none static class that i can instantiate.
  • i would have the class that ultimately calls this method either pass in the dependent objects to the constructor, pass them in to the method that calls this, or set properties in the class.  this is what it means to inject dependencies.
  • i would look for some way to avoid creating a new samlresponse object.  i’m not looking at the api documentation, but it would be great if i could new up the object and call a method, or set a property to give it the samlresponsexml variable.  if i had to, i’d probably wrap samlresponse in another class so that i could gain this functionality.
  • finally, i would make each of the dependent object based on an interface so that i could swap them out.  in the case of some of these classes, i may need to wrap them in another class that i have control over so that i can implement an interface.  for example, as things stand, i would not be able to create a fake request object because request is a system class that does not implement an interface.

advantages

after making all of these changes, i would be able to create a test harness for this code, create fake versions of the objects, and verify that this method does what we intend for it do do.

where this would have been particularly helpful is over this last week when we were trying to get this all working with the new provider.  in that case, i could have faked out the request object with what they were sending us and run it through a debugger to figure out what wasn’t quite right.

no dependency injection framework

finally, you’ll notice that no where in this code did i have to use a dependency injection framework to get this all working.

other places talking about dependency injection

  • design patterns – dependency injection (microsoft)
  • working effectively with legacy code


Dependency injection Framework Object (computer science)

Published at DZone with permission of Dave Bush, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • Mastering Time Series Analysis: Techniques, Models, and Strategies
  • Exploratory Testing Tutorial: A Comprehensive Guide With Examples and Best Practices
  • Implementing a Serverless DevOps Pipeline With AWS Lambda and CodePipeline
  • Automating the Migration From JS to TS for the ZK Framework

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: