具有 Windows 表单的 Unity IoC

Unity IoC with Windows Froms

所以我有这样一个程序架构(LibraryManager):

数据访问层有 class,它必须管理数据并与演示者通信

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

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

执行一个原语,注意不要纠结... 接下来,在主项目中实现 class: MessageService - 能够在程序的任何地方显示消息

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

LoginService和MainService——日志实现逻辑和应用的基本功能

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

进一步分别是IView接口及其派生接口和实现它们的classes:

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

现在我们实现 link 元素 - 演示者:

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

现在我只需要在容器中注册并尝试 运行 应用程序:

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

但是,第 obj.Run () 行没有像前一行那样完成

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

具有以下内容的例外情况:

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:

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

据我了解,根据MainPresenter创建时报错的描述,向其传递UnityContainer参数,试图创建界面对象,这当然是不可能的。但是我之前已经补充了这方面,"Interface - class"在一个容器里拿走,Unity必须创建一个对应的对象,然后把接口引用传给它,结果就大不一样了。

对不起我笨拙的英语:\

两个问题。

您缺少对 'MainService' 的“IMainService”注册,例如:

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

并且您的 MainService class 未声明为 public。