Search code examples
c#wpfprismmefprism-4

Prism v4 - Loading only some Modules from directory based on Role - MEF


What I'm trying to accomplish:

  • Load Views / Services based on the current user's Role while having all dlls in a single directory
  • Views can be created multiple times (separate windows)

I currently have my modules loading from a directory using the following code:

protected override void ConfigureAggregateCatalog()
{
    base.ConfigureAggregateCatalog();

    AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(Bootstrapper).Assembly));
    AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(AutoPopulateExportedViewsBehavior).Assembly));

    // Load modules from folder
    // Create Modules folder if it doesn't exist
    string modulesFolder = "Modules";
    if (!Directory.Exists(@".\" + modulesFolder))
    {
        Directory.CreateDirectory(@".\" + modulesFolder);
    }
    AggregateCatalog.Catalogs.Add(new DirectoryCatalog(modulesFolder));
}

I've tried searching around for examples of what I'm trying to do and have found them for MEF, but none with MEF + Prism so I'm wondering if its the same idea or if Prism has something built in as well.

I've seen that for regular MEF the best solution (please correct me if this is incorrect!) is to create a Custom Export Attribute (MEF Export Metadata) such as:

[MetadataAttribute]
[AttributeUsage(AttributeTargets.Class, AllowMultiple=false)]
public class UserLevelAttribute : ExportAttribute
{
    public UserLevelAttribute() : base(typeof(IModule)) { }
    public UserLevel User { get; set; }
}

public enum UserLevel    {
    Basic,
    Administrator
}

Is this the correct way or is there something in Prism that helps with this? What is the best way to only load the modules for the userlevel? In regular MEF I'd do an [ImportMany] and Lazy load them, is this still valid with Prism, if so, where should I do that?

Thank you


Solution

  • I'm actually looking at role management myself today and from a post on the Prism forum from one of the developers (Guido Maliandi):

    The subject of authentication and authorization isn't supported in Prism out of the box.

    So we have to roll our own, but Guido Maliandi has done a blog post showing one way of doing "Authentication and role based authorization in Prism v4" which utilizes a service to get a list of Module Names for the Module Manager to load in.