Unity IoC with Windows Froms

本文关键字:Froms Windows with IoC Unity | 更新日期: 2023-09-27 17:50:17

所以我有这样一个程序体系结构(LibraryManager(:

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

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

实现一个原语,不要停留在注意力上。。。接下来,在主项目实现类中:MessageService-能够在程序中的任何位置显示消息

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

LogiService和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>();

flies对以下内容表示异议:

Microsoft.Practices.Unity.ResolutionFailedException未处理HResult=-2146223088消息=解析依赖关系失败,键入="Library.MainPresenter",name="(无("。解析时发生异常。异常为:InvalidOperationException-当前类型Library.IMainService是一个接口,无法构造。您是否缺少类型映射?-----------------------------------------------发生异常时,容器为:

解析库.MainPresenter,(无(解析参数构造函数Library.MainPresenter的"mainService"(Library.IMainViewmainView,Library.IMessageService messageService,Library.MainServicemainService(解析库。IMainService,(无(

根据我对MainPresenter创建过程中错误的描述,并将试图创建接口对象的UnityContainer参数传递给它,这当然是不可能的。但我之前已经添加了,"Interface-class"在一个容器中并接受它,Unity必须创建一个相应的对象,然后将接口引用传递给它,结果截然不同。

抱歉我的英语不好:''

Unity IoC with Windows Froms

两个问题。

您缺少"IMainService"到"MainService"的注册,例如:

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

并且您的CCD_ 2类未声明为公共类。