Created
September 3, 2014 02:38
-
-
Save darrencauthon/9a02cca19aeeeb548555 to your computer and use it in GitHub Desktop.
DI Sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ProductController < ApplicationController | |
def index | |
@products = Product.all | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ProductController : Controller | |
{ | |
private IProductRepository _productRepository; | |
public ProductController(IProductRepository productRepository) | |
{ | |
_productRepository = productRepository; | |
} | |
public ActionResult Index() | |
{ | |
products = productRepository.getAll(); | |
return View(products); | |
} | |
} |
This may be a perfectly good way to code it, and to test it. It's simple, I like it, and it'll probably do just fine. IMO, however, it is not Dependency Injection, simply because there is no injection going on.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@hotgazpacho It does not call the filters... but that's what I want! I want to be able to unit test the controller action in isolation from anything else.
I am a rare bird 🐦 as I use Rails yet I unit test my code. I know there are debates about unit testing versus integration tests, but to me -- without unit tests, I can't TDD, and if I can't TDD I'm not able to architect and verify my output.
I also use few filters.... if things get complicated, I'll string an integration test through them. But if things are complicated, I'm doing something else wrong. 😆