Skip to content

Instantly share code, notes, and snippets.

@darrencauthon
Created September 3, 2014 02:38
Show Gist options
  • Save darrencauthon/9a02cca19aeeeb548555 to your computer and use it in GitHub Desktop.
Save darrencauthon/9a02cca19aeeeb548555 to your computer and use it in GitHub Desktop.
DI Sample
class ProductController < ApplicationController
def index
@products = Product.all
end
end
public class ProductController : Controller
{
private IProductRepository _productRepository;
public ProductController(IProductRepository productRepository)
{
_productRepository = productRepository;
}
public ActionResult Index()
{
products = productRepository.getAll();
return View(products);
}
}
@hotgazpacho
Copy link

Question for you, though, @darrencauthon. Does calling controller.index like that, as opposed to the more common apraoch of using the TestRequest class, still invoke the before/after/around filters for the action?

@darrencauthon
Copy link
Author

@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. 😆

@RonJeffries
Copy link

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