Unity IoC与Windows Froms

时间:2015-05-22 00:38:39

标签: c# winforms unity-container ioc-container

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

数据访问层具有类,必须管理数据并与演示者进行通信

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

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

原始的实现,不要在关注...... 接下来,在主项目实现类中: 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接口及其衍生物接口和实现它们的类:

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

现在我们实现了链接元素 - 演示者:

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未处理   HResult = -2146233088消息=依赖关系的解析失败,输入   =&#34; Library.MainPresenter&#34;,name =&#34;(none)&#34;。在解决时发生异常:例外情况是:InvalidOperationException - 当前   type,Library.IMainService,是一个无法构造的接口。   你错过了类型映射吗?    - - - - - - - - - - - - - - - - - - - - - - - - 当时例外,容器是:

     

解析Library.MainPresenter,(无)解析参数   &#34; mainService&#34;构造函数Library.MainPresenter(Library.IMainView   mainView,Library.IMessageService messageService,Library.IMainService   mainService)解析Library.IMainService,(无)

据我所知,它基于MainPresenter创建过程中的错误描述,并传递了尝试创建接口对象的UnityContainer参数,这当然是不可能的。但是在此之前我已经添加了&#34; Interface - class&#34;在一个容器中并且接受它,Unity必须创建一个相应的对象,然后将接口引用传递给它,结果是完全不同的。

对不起我笨拙的英语:\

1 个答案:

答案 0 :(得分:1)

两个问题。

您错过了`IMainService&#39;到主服务&#39;如:

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

并且您的MainService类未声明为公开。