ASP.NET Boilerplate Plugin Module or Dynamic Module

Refresh

April 2019

Views

456 time

1

I am a .NET developer and currently, I am trying to learn ASP.NET Boilerplate. I came across PlugIn Modules and I got the idea that it can be used for module dependency, but they have these lines that I am trying to understand:

The AbpBootstrapper class defines the PlugInSources property which can be used to add sources to dynamically load plugin modules. A plugin source can be any class implementing the IPlugInSource interface. The PlugInFolderSource class implements it to get the plugin modules from assemblies located in a folder.

So after trying to implement IPlugInSource interface:

using Abp.Modules;
using Abp.PlugIns;
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;

public class EmpDetails : IPlugInSource
{
    public string EmpName { get; set; }
    public string Address { get; set; }
    public string Email { get; set; }

    public List<Assembly> GetAssemblies()
    {
        throw new NotImplementedException();
    }

    public List<Type> GetModules()
    {
        throw new NotImplementedException();
    }
}

My doubt is: what operation do I have to perform inside GetAssemblies() and GetModules() methods, as in which Assemblies and Type do I have to return? I have referred to the official site document in which I could not find if they provided an example properly. Thanks in advance.

1 answers

4

You are not expected to implement IPlugInSource.

The documentation provides a clear example of how to add plugin sources in the Startup class:

services.AddAbp<MyStartupModule>(options =>
{
    options.PlugInSources.AddFolder(@"C:\MyPlugIns");
});

To clear your doubt, see GetAssemblies and GetModules methods from FolderPlugInSource:

public class FolderPlugInSource : IPlugInSource
{
    public string Folder { get; }

    public SearchOption SearchOption { get; set; }

    private readonly Lazy<List<Assembly>> _assemblies;

    public FolderPlugInSource(string folder, SearchOption searchOption = SearchOption.TopDirectoryOnly)
    {
        Folder = folder;
        SearchOption = searchOption;

        _assemblies = new Lazy<List<Assembly>>(LoadAssemblies, true);
    }

    public List<Assembly> GetAssemblies()
    {
        return _assemblies.Value;
    }

    public List<Type> GetModules()
    {
        var modules = new List<Type>();

        foreach (var assembly in GetAssemblies())
        {
            try
            {
                foreach (var type in assembly.GetTypes())
                {
                    if (AbpModule.IsAbpModule(type))
                    {
                        modules.AddIfNotContains(type);
                    }
                }
            }
            catch (Exception ex)
            {
                throw new AbpInitializationException("Could not get module types from assembly: " + assembly.FullName, ex);
            }
        }

        return modules;
    }

    private List<Assembly> LoadAssemblies()
    {
        return AssemblyHelper.GetAllAssembliesInFolder(Folder, SearchOption);
    }
}