Using Ninject in a plugin like architecture

前端 未结 9 2108
半阙折子戏
半阙折子戏 2020-12-07 10:05

I\'m learning DI, and made my first project recently.

In this project I\'ve implement the repository pattern. I have the interfaces and the concrete implementations.

相关标签:
9条回答
  • 2020-12-07 10:40

    you can easily do it with normal C# reflection, you don't need any extra technology.

    There are quite a few examples on the web, e.g. http://www.codeproject.com/KB/cs/c__plugin_architecture.aspx

    In general in your main application, you need to load the assembly implementing the plugin, e.g.:

    ass = Assembly.Load(name);
    

    and then you need to create an instance of your plugin. If you know the name of the class it would look like this:

    ObjType = ass.GetType(typename);
    IPlugin plugin = (IPlugin)Activator.CreateInstance(ObjType);
    

    and then you just use it.

    0 讨论(0)
  • 2020-12-07 10:40

    Take a look at Managed Extensibility Framework. http://www.codeplex.com/MEF

    0 讨论(0)
  • 2020-12-07 10:45

    There are multiple ways to go about this and you already have accomplished the main goal to achieve this in having concrete implementations through pre-defined interfaces. Realistically, if your interfaces remain stable, you should be able to build off of your core application.

    I am not sure how the implementation would work with Ninject, however. You can do this with the Provider Model or with reflection - although I think reflection is overkill, if you don't absolutely need to do it.

    With the provider model approach, you place the file in the /bin folder, or any other folder that you are probing, and adjust the .config file to reflect the presence of the provider. If you have a specific "plugin" folder, you can create a method called at the startup of the application and periodically, otherwise, to scan for new or removed instances and reload the providers.

    This would work in ASP.NET, under C# or VB. However, if you are doing some sort of other application, you would need to consider another approach. The provider is really just Microsoft's spin on the Strategy Pattern.

    0 讨论(0)
提交回复
热议问题