views:

35

answers:

2

I have a check for registration in my RegistrationController:

public class RegistrationController : Controller
{
 private readonly IAmARegistrationRepository _RegistrationRepository;
 public RegistrationController(IAmARegistrationRepository registrationRepository)
 {
  _RegistrationRepository = registrationRepository;
 }
 public  bool IsRegistered(string userName)
 {
  return _RegistrationRepository.IsRegistered(userName);
 }
}

How can I check this from my HomeController? Is there a way to access the ControllerBuilder to pull in the currently open controller if there is one? or to at least be able to generate one using the customer controller factory I've loaded into the ControllerBuilder in my Global.asax.cs?

+4  A: 

I think you're overthinking this. You already have a method on your repository that you can use. Why create another controller just to invoke a method that simply defers to the repository method? Create an instance of the repository in your HomeController and use the IsRegistered method on it.

tvanfosson
I was thinking for separation of concerns it would be best for registration methods to live in the registration controller and that the single call to check on registration wasn't a good enough reason to constructor inject the `RegistrationRepository`. Perhaps the `RegistrationRepository` should be a singleton somewhere.
Maslow
I wouldn't go the singleton route. You obviously have a need for registration data in the HomeController so I would inject it there. I don't think a repository is a good candidate for a singleton and making it one will make your tests harder.
tvanfosson
Depending on the size of your application, this is a good candidate for dependency injection.
Chuck Conway
+2  A: 

One solution (of many) would be to implement the common function IsRegistered in a BaseController and have both HomeController and RegistrationControlle Inherit from your BaseController.

Myster
+1 I had't thought of that, but i'm just going to go with tvanfosson's idea.
Maslow