Understanding Dependency Injection and Those Pesky Containers

Play Understanding Dependency Injection and Those Pesky Containers

Description

Dependency Injection is one of those terms that advanced programmers throw out with an expectation and assumption of full understanding on the part of the receiver. However, I constantly get asked by attendees, students, and clients to please recommend a good DI product and show them how to use it; and when I proceed to start talking about the subject, it becomes immediately apparent that many don't even know what they're asking about. It's easy to get infatuated by a cool buzz phrase, specially when you hear so many others using it. But to truly understand something you need to start by understanding the problem spaces that it is trying to solve. So let's bring you totally up to speed then. In this session, I explain dependency injection from concept to implementation, and use raw code samples to show you how it works and what problems it solves. Then I get into what a DI container is and some of the characteristics of the ones that are out there, including MEF. I finish by showing you implementation examples in three different platforms.

Tag:

Breakout

Day:

4

Session Type:

Breakout

Code:

DEV-B207

Room:

Room 291

Embed

Download

The Discussion

  • User profile image
    dirq

    I'm not gonna lie, I'm confused by this topic too. How necessary is this for smaller apps?  Is the added complexity worth the testing ability?  Is it really worth making mocks instead of just testing database calls directly?  I know this would anger the unit-testing ideologues, but not all my tests are self-contained - Sometimes I hit a database to check if my web app code is working.

  • User profile image
    Index

    Is there a sound issue here? I lost sound at 51:20

  • User profile image
    teabaggs2

    his mic went out for a bit.  should come back on in about a minute.

  • User profile image
    Oscar Zarate

    @dirq, how small are the "small apps"? Are you ever going to do a bigger one? for a company where they have policies and the database is something hard and expensive to hit?

    And not only that, you can develop your software without using a DB. You can even mock a Repository and show your idea to a customer in a few hours in a small laptop without DB.

    And even better, DDD, the domain is in the center of the universe, not the database. The db is just a way of persistence, not the only one. Don't start your design by thinking about your tables, think about your object. All of these things come together.

    Highly recommended the tag DDD :-)

    BTW, @Miguel great talk. I'd prefer myself a 200 talk without MEF but it was GREAT!
    Another one for Miguel, the bank in Mexico was related to your uncles Fidel and Raul? :-) (Espero no te enojes por la broma).

    SaludOZ,

  • User profile image
    JohnnyG321

    Hi Everyone,

    I attended this session at TechEd (and several others about DI and unit testing and so on) and decided that this is something I have to do.  So I'm all ready to start creating a Portable Class Library for my view models, using DI and unit testing and Moq for my mocks and I discover that Moq apparently doesn't work with WinRT because WinRT doesn't support reflection.

    Naturally I would pick something that doesn't work for my first foray into this area.  So can anyone tell me how to approach unit testing and mocks for PCLs?  I'm still researching, but I would appreciate any guidance I can get here. 

  • User profile image
    Oscar Zarate

    @JohnnyG321: Two things
    1.- You can't generate dynamics proxies in WinRT, so none of the framework that take this approach will work (moq, rhinoMoq, etc). Microsoft has made System.Reflection.Emit namespace is private so forget any form of dynamic code generation and/or MSIL injection (I reckon it's because of the sandbox security "thing").
    Similar for UI Automation Testing.
    The alternative is to use (Microsoft Fakes Framework) Shims http://msdn.microsoft.com/en-us/library/hh549175.aspx (I remember it used to be only in Visual Studio Ultimate)
    2.- A PCL is not a WinRT component. You can use any mock you want when unit testing (and I'm saying UNIT testing).
    You won't be able to do any Integration/UI Automation testing that include "Metro" applications (or is there anything new that I don't know? :-))

    Cheers,

  • User profile image
    JohnnyG321

    Hi Oscar,

    I should have come back to this page sooner.  Thanks for the feedback.  When I wrote this message, I was just beginning to delve into this topic.  I was running into an error that didn't mean anything to me and the very few articles I could find on Bing/Google seemed to indicate that I couldn't use Moq with PCLs.  I was misunderstanding what I was reading.  It was one of those cases where I didn't know what I didn't know.  I have since resolved my mysterious error and have been using Moq with PCLs.  Haven't gotten to the actual WinRT stuff yet.

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.