1

I have the following project structure in my solution:

Project.Repository (Class Library)

Project.Core (Class Library)

Project.Web (MVC5 web front end)

Project.Scheduler (Console Application)

I am using Unity to handle my DI.

My problem

I have a method which sits in the Core layer, this method has a dependency on an IProcessDataService interface which is being handled in my Unity config.

    //Service            
    container.RegisterType<IProcessDataService, ProcessDataService>(new PerRequestLifetimeManager());

I have most recently created my Console Application project which will basically invoke the said method and will be run on a windows schedule.

How do I go about accessing this method from my Console Application's static Main method considering I have a dependency to my core interface?

public class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Begin Automation Process");

            //Run method from Core layer which has a dependency on IProcessDataService

        }
    }
Liam
  • 27,717
  • 28
  • 128
  • 190
JsonStatham
  • 9,770
  • 27
  • 100
  • 181
  • First you'll have to initialize a new Unity container, and register your mappings. Then you'll have to use a service locator pattern to resolve the IProcessDataService dependency. http://stackoverflow.com/questions/22961463/how-can-i-set-up-unity-3-to-work-in-a-console-application – general exception May 12 '15 at 10:57
  • 1
    please don't use the service locator pattern. Just wire up your container to produce your object graph and get the root object and start from there. – Sam Holder May 12 '15 at 11:00
  • @SamHolder but he wont be able to inject any dependency into the static Main method ? If your using container.Resolve that is basically a service locator. – general exception May 12 '15 at 11:01
  • 2
    @generalexception correct, but he doesn't want to, the main method is the root of the app, so it is where all configuration of the container should happen, and will resolve the object which he wants to call the method on. The container will provide the IProcessDataService. – Sam Holder May 12 '15 at 11:04
  • I usually use `HierarchicalLifetimeManager` instead of `PerRequestLifetimeManager` for this reason. More [here](http://stackoverflow.com/a/25671643/1370166). – TylerOhlsen May 13 '15 at 04:11

1 Answers1

3

your console app's main method is your composition root and you should be doing all of your wiring up in there.

Then you can resolve your object from the container and call the method on it.

Community
  • 1
  • 1
Sam Holder
  • 32,535
  • 13
  • 101
  • 181
  • I basically rolled the contents of UnityConfig.cs and UnityMvcActivator.cs into my own ConfigureUnity() method, and called this from my entry point (Main). I was then able to create my required object by doing: ProcessDataService service = container.Resolve(); And all my dependencies were handled. Make sure you transfer anything from the web.config into the app.config that is required for these dependencies. – JsonStatham May 15 '15 at 12:35