Unity IoC with Windows Froms

So, I have a software architecture like this (LibraryManager):

The data access layer has a class that must manage data and communicate with the master

public interface ILibraryDataManager
{
    //some code...
}

public class LibraryDataManager:ILibraryDataManager
{
     //some code...
}

      

Implementation of the primitive, do not dwell on your attention ... Further, in the main class of the project implementation: MessageService - the ability to display messages anywhere in the program

interface IMessageService
{
     //some code
}
    class MessageService : IMessageService
{
     //some code
}

      

LoginService and MainService - the logic for implementing the logic and the main functions of the application

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...
}

      

Further, respectively, the interface of the IView interface and its derived interfaces and classes that implement them:

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...
}

      

We will now implement the link element - presenters:

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); 
    }

      

And now all I need to register with the container and try to run the application:

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        UnityContainer container = new UnityContainer();
        container.RegisterType<ILibraryDataManager, LibraryDataManager>()
                    .RegisterType<IMessageService, MessageService>()
                    .RegisterType<ILoginService, LoginService>()
                    .RegisterType<ILoginView, FormLogin>()
                    .RegisterType<IMainView, MainForm>();

        var obj = container.Resolve<MainPresenter>();
        obj.Run();
    }
}

      

However, the line obj.Run ()

does not reach execution as in the previous line

var obj = container.Resolve<MainPresenter>();

      

excludes an exception from such content:

Microsoft.Practices.Unity.ResolutionFailedException was unhandled HResult = -2146233088 Message = Error resolution failed, enter = "Library.MainPresenter", name = "(none)". An exception occurred during: while resolving. Exception: InvalidOperationException - The current type, Library.IMainService, is an interface and cannot be constructed. Are you missing type mapping? ----------------------------------------------- While exceptions, the container was:

Resolution of the .MainPresenter library, (none) Resolution of the "mainService" parameter of the constructor Library.MainPresenter (Library.IMainView mainView, Library.IMessageService messageService, Library.IMainService mainService) Resolution of the library .IMainService, (none)

As I understand it, this is based on describing an error when creating the MainPresenter and passing its parameters to the UnityContainer trying to create an interface object, which of course is not possible. But I added to this question "Interface - class" in a container and take it, Unity has to create the corresponding object and then pass it a reference to the interface, and the result is completely different.

Sorry for my clumsy english: \

+3


source to share


1 answer


Two problems.

You are missing registration for "IMainService" in "MainService", for example:



container.RegisterType<ILibraryDataManager, LibraryDataManager>()
         .RegisterType<IMainService, MainService>()

      

and your class is MainService

not declared public.

+1


source







All Articles