title: Unity IoC with Windows Froms
body: <p>So i have such an program architecture (LibraryManager):</p>
  
  <p>Data Access Layer has class, that has to manage data and communicate with the presenter</p>
  
  <pre><code>public interface ILibraryDataManager
  {
      //some code...
  }
  
  public class LibraryDataManager:ILibraryDataManager
  {
       //some code...
  }
  </code></pre>
  
  <p>Implementation of a primitive, do not dwell on the attention ...
  Next, in main project implementing class:
  MessageService - to be able to display messages anywhere in the program</p>
  
  <pre><code>interface IMessageService
  {
       //some code
  }
      class MessageService : IMessageService
  {
       //some code
  }
  </code></pre>
  
  <p>LoginService and MainService - logging implementation logic and the basic functions of the application</p>
  
  <pre><code>public interface ILoginService
  {
      //some code
  }
  class LoginService : ILoginService
  {  
       private readonly IMessageService messageServise;
       private readonly ILibraryDataManager libraryDBManger;
       public LoginService(IMessageService messageServise, ILibraryDataManager libraryDataManager)
       {
            this.messageServise = messageServise;
            this.libraryDBManger = libraryDataManager;
       }
          //some code...
  }
  
  public interface IMainService
  {
       //some code
  }
  class MainService : IMainService
  {     
      private readonly IMessageService messageService;
      private readonly ILibraryDataManager libraryDBManger;
  
      public MainService(ILibraryDataManager libraryDataManager, IMessageService messageService)
      {
           this.libraryDBManger = libraryDataManager;
            this.messageService = messageService;
       }
          //some code...
  }
  </code></pre>
  
  <p>Further, respectively IView interface and its derivatives interfaces and classes that implement them:</p>
  
  <pre><code>public interface IView
  {
       //some code...
  }
  
  public interface ILoginView: IView
  {
       //some code...      
  }
  
  public partial class FormLogin : Form, ILoginView
  {
       //some code...
  }
  public interface IMainView: IView
  {
       //some code...
  }
  
  public partial class MainForm : Form, IMainView
  {
       //some code...
  }
  </code></pre>
  
  <p>Now we realize the link element - presenters:</p>
  
  <pre><code>public interface IPresenter
  {
      void Run(); // этот метод должен запускать соответствующую форму (IView), переданную презентеру в конструкторе
  }
  
  class LoginPresenter : IPresenter
  {
      private readonly ILoginView loginView;
      private readonly ILoginService loginService;
      private readonly IMessageService messageService;
  
      public LoginPresenter(ILoginView loginView, ILoginService loginService, IMessageService messageService)
      {
          this.loginView = loginView;
          this.loginService = loginService;
          this.messageService = messageService;
      }
      public void Run()
      {
          loginView.Show();
      }
      //some code...
  }
  
  class MainPresenter : IPresenter
  {
      private readonly IMainView mainView;
      private readonly IMessageService messageService;
      private readonly IMainService mainService;
  
      public MainPresenter (IMainView mainView, IMessageService messageService, IMainService mainService)
      {
          this.mainService = mainService;
          this.mainView = mainView;
          this.messageService = messageService;
      }
  
      public void Run()
      {
          Application.Run(mainView as Form); 
      }
  </code></pre>
  
  <p>And now it's all i need to register in the container and try to run the application:</p>
  
  <pre><code>static class Program
  {
      /// &lt;summary&gt;
      /// The main entry point for the application.
      /// &lt;/summary&gt;
      [STAThread]
      static void Main()
      {
          Application.EnableVisualStyles();
          Application.SetCompatibleTextRenderingDefault(false);
  
          UnityContainer container = new UnityContainer();
          container.RegisterType&lt;ILibraryDataManager, LibraryDataManager&gt;()
                      .RegisterType&lt;IMessageService, MessageService&gt;()
                      .RegisterType&lt;ILoginService, LoginService&gt;()
                      .RegisterType&lt;ILoginView, FormLogin&gt;()
                      .RegisterType&lt;IMainView, MainForm&gt;();
  
          var obj = container.Resolve&lt;MainPresenter&gt;();
          obj.Run();
      }
  }
  </code></pre>
  
  <p>However, the line <code>obj.Run ()</code> does not reach fulfillment, as during the previous row</p>
  
  <pre><code>var obj = container.Resolve&lt;MainPresenter&gt;();
  </code></pre>
  
  <p>flies exception with such content:</p>
  
  <blockquote>
    <p>Microsoft.Practices.Unity.ResolutionFailedException was unhandled
    HResult=-2146233088 Message=Resolution of the dependency failed, type
    = "Library.MainPresenter", name = "(none)". Exception occurred while: while resolving. Exception is: InvalidOperationException - The current
    type, Library.IMainService, is an interface and cannot be constructed.
    Are you missing a type mapping?
    ----------------------------------------------- At the time of the exception, the container was:</p>
    
    <p>Resolving Library.MainPresenter,(none) Resolving parameter
    "mainService" of constructor Library.MainPresenter(Library.IMainView
    mainView, Library.IMessageService messageService, Library.IMainService
    mainService) Resolving Library.IMainService,(none)</p>
  </blockquote>
  
  <p>As I understand it based on the description of the error during the creation of MainPresenter and transfer it UnityContainer parameters of attempting to create an interface object, which of course is impossible. But I have added before that regard, "Interface - class" in a container and take it, Unity must create a corresponding object, and then pass the interface reference to it, and the result is quite different.</p>
  
  <p>Sorry for my clumsy English :\</p>