Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Unity DI with a Console Application

Im trying to get Unity to work with my console application, however.. all the properties that I try to dependency inject are still set to null.

This is my code:

Program.cs

namespace .Presentation.Console
{
    class Program
    {
        static void Main(string[] args)
        {
            var mainThread = new MainThread();
        }
    }
}

MainThread.cs

namespace xxxx.Presentation.Console
{
    public class MainThread
    {
        public IUnitOfWork UnitOfWork { get; set; }

        public IMapper Mapper { get; set; }

        public MainThread()
        {
            Mapper.RegisterMappings();
        }
    }
}

App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/>
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1" />
  </startup>
  <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
    <alias alias="IDataAccess" type="UnityFramework.IDataAccess, UnityFramework" />
    <namespace name="UnityFramework" />
    <assembly name="UnityFramework" />
    <container>
      <types>

        <type type="IMapper" mapTo="xxxx.Core.Parse.ParseMapper" />

      </types>
    </container>
  </unity>
</configuration>

App.config is set to Copy Always

Where Mapper is returned as null in this case (and I assume UnitOfWork is as well)

Do I need to do anything else? Add something to the app.config? Am I missing something?

Thanks in advance!

Br, Inx

like image 548
Inx51 Avatar asked Dec 15 '22 16:12

Inx51


1 Answers

Unity only supplies dependencies for components obtained via Resolve or while resolving sub-dependencies. The "root" component or components must be obtained from the container manually.

Using new Program would not automatically provide the dependencies because it bypasses the Unity container.

static class ProgramEntry
{
    static void Main(string[] args)
    {
        var unity = CreateUnityContainerAndRegisterComponents();
        // Explicitly Resolve the "root" component or components
        var program = unity.Resolve<Program>();
        program.Run();
    }
}

public class Program
{
    readonly Ix _x;

    // These dependencies will be automatically resolved
    // (and in this case supplied to the constructor)
    public Program(IMapper mapper, Ix x)
    {
        // Use dependencies
        mapper.RegisterMappings();

        // Assign any relevant properties, etc.
        _x = x;
    }

    // Do actual work here
    public void Run() {
        _x.DoStuff();
    }
}

I prefer code-based registration for most tasks.

  • I recommend not using attributes, although they 'will work' if following the Resolve pattern above. The "root" objects must be manually resolved.

    The problem with attributes is these add a dependencies on Unity - so much for "inverting"!

    Constructor Injection (as shown) is automatic/default. See Setter / property injection in Unity without attributes if Property Injection is preferred.

  • I would probably resolve a Factory (or Func<UoW>) to create a UoW and supply it down the call-stack context (ie. pass it to methods) when applicable. The application may have many different UoWs during a single run. In this case you may also be interested in creating scopes.

  • I would also probably use a factory to create a pre-configured IMapper object when it is resolved instead of using RegisterMappings afterwards.

like image 128
user2864740 Avatar answered Dec 29 '22 10:12

user2864740