动态菜单创建的IoC [英] Dynamic menu creation IoC

查看:137
本文介绍了动态菜单创建的IoC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有任何人知道我怎么可以创建我怎么能使用类似AutoFac让我动态允许DLL的创建有自己的形式和菜单项来调用它们在运行时。

I am wondering if anyone out there knows how I could create how could i use something like AutoFac to let me dynamically allow dll's to create there own forms and menu items to call them at run time.

所以,如果我有一个,

Employee.dll

Employee.dll


  • 新入门表

  • 证书表格

Supplier.dll

Supplier.dll


  • 供应商中的详细信息

  • 产品形式

在我的winform应用程序会创建一个这样的菜单,并在每一个点击负荷relavent形式了。

In my winform app it would create a menu with this and when each one clicked load the relavent form up


  • 新入门

  • 证书

供应商


  • 供应商详细信息

  • 产品

因此,我可以添加一个新的类库项目,当它加载了它只是把它添加到菜单。

So I can add a new class library to the project and it would just add it to menu when it loads up.

希望有意义,有人可以帮我。

Hope that make sense and someone can help me out.

干杯

艾丹

推荐答案

您必须做的第一件事情是让你的核心应用程序扩展。让我们看一个简单的例子。

The first things you have to do is to make your core application extensible. Let's see a simple example.

您必须允许外部组件来创建你的主要的应用程序项条目。要做到这一点,你可以创建一个在你的主应用一个 IMenuBuilder 接口。

You will have to allow your external assembly to create item entry in your main app. To do this you can create a IMenuBuilder interface in your main app.

public interface IMenuBuilder
{
    void BuildMenu(IMenuContainer container); 
}

这界面将允许外部组件,使用 IMenuContainer 以创建 MenuEntry 。此接口可以这样定义的:

This interface will allow external assembly to use a IMenuContainer to create MenuEntry. This interface can be defined like this :

public interface IMenuContainer 
{
    MenuStrip Menu { get; }
}

在您的主要形式,你将不得不实施 IMenuContainer ,并呼吁所有的 IMenuBuilder 接口,以允许他们创建菜单项。

In your main form, you will have to implement IMenuContainer and call all the IMenuBuilder interface to allow them to create menu entry.

public partial class MainForm : Form, IMenuContainer
{
    public MenuStrip Menu 
    {
        get 
        {
           return this.mnsMainApp; 
        }
    }

    private void MainForm_Load(Object sender, EventArgs e)
    {
        ILifetimeScope scope = ... // get the Autofac scope
        foreach(IMenuBuilder menuBuilder in scope.Resolve<IEnumerable<IMenuBuilder>())
        {
            menuBuilder.BuildMenu(this); 
        }
    }
}

在每个外部程序集,将必须实现尽可能多的 IMenuBuilder ,并根据需要有一个 Autofac 的模块。在此模块中,您将注册的 IMenuBuilder

In each external assembly, you will have to implement as much IMenuBuilder as needed and one Autofac Module. In this module you will register those IMenuBuilder.

public class XXXModule : Module
{
    protected override void Load(ContainerBuilder builder)
    {
        builder.RegisterType<XXXMenuBuilder>()
               .As<IMenuBuilder>();
    }
}



最后,在你的核心应用程序,你将不得不注册由外部组件中的所有模块:

Finally, in your core app, you will have to register all your modules provided by external assembly :

ContainerBuilder builder = new ContainerBuilder();
String path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

IEnumerable<Assembly> assemblies = Directory.GetFiles(path, "*.dll")
                                            .Select(Assembly.LoadFrom);

builder.RegisterAssemblyModules(assemblies);

这篇关于动态菜单创建的IoC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆