1

Given this code:

namespace Eisk.Controllers
{
    public class EmployeesController : Controller
    {
        DatabaseContext _dbContext;

        public EmployeesController(DatabaseContext databaseContext)
        {
            _dbContext = databaseContext;
        }

        public ViewResult Index()
        {
            var employees = _dbContext.EmployeeRepository;

            return View(employees.ToArray());
        }

Note that the constructor doesn't new up a database.

When accessed from a unit test I can inject a databaseContext and the controller will use that for the duration of the test. What I can't figure out is where this code is getting the database context it's using at run time. If I could find that out I might be able to figure out how to circumvent that behavior and have it use a mocked/in memory DB instead.

More explanation: I don't have access to a legacy database for this application right now so I'm trying to Mock up an in memory data source that gets filled from xml files. That's why I need to be able to circumvent the default database context creation.

More Information: Thanks for all the help so far you wonderful people you.

Steven seems to have directed me down the correct path. In the Global.asax file is this call:

DependencyInjectorInitializer.Init();

Following that through the code I get to:

        public static void Initialize()
    {
        _container = new UnityContainerFactory().CreateConfiguredContainer();     
        var serviceLocator = new UnityServiceLocator(_container);
        ServiceLocator.SetLocatorProvider(() => serviceLocator);
        DependencyResolver.SetResolver(new UnityDependencyResolver(_container));
    }

At least that gets me headed in the right direction. Now I have to figure out how Unity is going about creating the context so I can do my intervention.

  • Let me plug the EISK MVC Employee Info Starter Kit here. It's a well thought out system developed by Mohammad Ashraful Alam Et al. that includes a well fledged example of how many of the new technologies fit together. MVC 5, Entity Framework 6, Unity, Authentication, OpenAuth, DI, Moq, and a couple of other things. Can be used as a template, general learning, or training. Employee Info Starter Kit
Ashraf Alam
  • 3,500
  • 32
  • 31
user1582732
  • 59
  • 10
  • check your web.config file , I assume that is what you are asking .... you mean what connection string that context is using? – Scott Selby Nov 27 '14 at 03:22
  • What do you mean _doesn't new up a database_? What DI framework are you using - Ninject, StructureMap, Castle Windsor ? –  Nov 27 '14 at 03:33
  • I don't think this is necessarly a DI framework question , I thought it was at first , but it doesn't say IDatabaseContext , so all you need to do is navigate to DatabaseContext code and see whats in there , it will specificy which connection string to use ,if nothing is in that class then if should have the same name in the web.config – Scott Selby Nov 27 '14 at 03:37
  • If your code works, find all instances of class DatabaseContext. One with initialization code will answer your question. As stephen pointed out, it should be somewhere in your DI initialization class. @ScottSelby - Could DatabaseContext be an abstract class that make DI coming into picture? – SBirthare Nov 27 '14 at 04:53
  • Scott Selby is correct in that this isn't really a DI framework question. I think this is very specific to Microsofts MVC in that inheriting from the Controller is somehow doing the work for me. That's pretty much the question. How is Controller doing this? – user1582732 Nov 27 '14 at 18:38

1 Answers1

3

With the default configuration of ASP.NET MVC, a controller should have a default constructor (i.e. a public constructor with no parameters). If not ASP.NET MVC will throw the following exception:

Type 'Eisk.Controllers.EmployeesController' does not have a default constructor

If this however works, this means that you (or another developer) overwrote the default configuration by either using a custom IControllerFactory or custom IDependencyResolver. Most developers do this by using an open source Dependency Injection library (such as Simple Injector, Autofac or Castle Windsor). If you pull in the NuGet MVC integration packages for such library, it will usually do this configuration for you. So somebody on your team might have done this for you.

My advice is: talk to your team and ask them how they did this and which container they used and where you can find the configuration for that.

Steven
  • 166,672
  • 24
  • 332
  • 435
  • I believe you've got it Steven. In the Global.asax file Unity is setup. More below. – user1582732 Nov 27 '14 at 18:46
  • "talk to your team..." Um, we didn't write this. I'm using the EISK Employee Info Starter Kit as a template. It has proven to be exceptionally helpful in quickly bringing me up to speed in understanding MVC, EntityFramework, DI, and several other things. find it here: https://eisk.codeplex.com/ – user1582732 Nov 27 '14 at 18:59