none
What is the difference between MEF and MAF framework RRS feed

  • Question

  • Hi 

    As I understood from the various sites that MEF does not provides isolation between the assemblies while MAF can provide isolation, but implementing the MAF is quite expansive, because of less speed. The host application can also implement MAF with MEF supports. Here my question is how to justify which framework (MEF/MAF) should be used for developing host application?

     

    Regards

    Ravi

    Monday, December 12, 2011 6:30 AM

Answers

  • MAF is quite a bit more complicated - and not nearly as nice from a development point of view.  However, it's a true "add in" solution, in that it provides complete isolation, but does so at a huge cost.  It's quite difficult to work with (comparatively), incurs extra overhead dealing with AppDomain boundaries, and is less flexible.

     

    MEF was intended for application extensibility, and is quite easy to use.  IF you're going to use this in the context of WPF, you likely won't want the isolation (which is MAF's primary benefit), so you likely want MEF.  MEF is perfect for extensibility - it's more like a dependency injection solution vs. a full isolation package, which gives it a nice ease of use and a huge amount of power and flexibility in terms of the types you can use (basically, anything).

     

    What is your application goals?  What do you need in terms of extensibility?


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Monday, December 12, 2011 6:34 AM
    Moderator

All replies

  • MAF is quite a bit more complicated - and not nearly as nice from a development point of view.  However, it's a true "add in" solution, in that it provides complete isolation, but does so at a huge cost.  It's quite difficult to work with (comparatively), incurs extra overhead dealing with AppDomain boundaries, and is less flexible.

     

    MEF was intended for application extensibility, and is quite easy to use.  IF you're going to use this in the context of WPF, you likely won't want the isolation (which is MAF's primary benefit), so you likely want MEF.  MEF is perfect for extensibility - it's more like a dependency injection solution vs. a full isolation package, which gives it a nice ease of use and a huge amount of power and flexibility in terms of the types you can use (basically, anything).

     

    What is your application goals?  What do you need in terms of extensibility?


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Monday, December 12, 2011 6:34 AM
    Moderator
  • Hi Reed,

     

    Thanks for your replay. I have developed prototype host application with MAF and I am facing same problem as you said.

    Actually, I wanted to design host application which can load and unload  assembly dynamically to increase the application performance. As I said, I have done with MAF but its very slow. The same thing can be achieved with MEF??. Is yes, then please share some sample code.

     

    regards

    Ravi

    Monday, December 12, 2011 6:44 AM
  • MEF can't really unload assemblies as they're loaded into the main AppDomain.  Once you load an assembly in the existing AppDomain, it's there until the AppDomain is shut down.

     

    That being said, loading and unloading assemblies dynamically has nothing to do with performance - what is it you're actually trying to do (more specifically)?  There may be a way to do it using MEF...

     


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Monday, December 12, 2011 5:04 PM
    Moderator
  • We are making host application, which will load 10000-20000 of UI assemblies. In such case we need to support loading and unloading of UI assemblies in host application. Is any other ways are there is MEF framework to achieve this?

     

    Regards

    Ravi 

    Wednesday, December 14, 2011 10:30 AM
  • We are making host application, which will load 10000-20000 of UI assemblies. In such case we need to support loading and unloading of UI assemblies in host application. Is any other ways are there is MEF framework to achieve this?

     

    Regards

    Ravi 

    Dynamically unloading user interface assemblies, at runtime, is quite difficult using any technology.  You run into issues, since using a user interface typically means pulling it into your AppDomain, which, in turn, means that it can't be unloaded without unloading your primary AppDomain.

     

    If you truly need to do this, you might want to consider alternative designs, whereby you open the "assemblies" as their own process, etc.  This is more robust, as well.

     

    What is your actual goal here?  Tens of thousands of user interfaces sounds a bit crazy, frankly - any application that complex should likely be split into multiple applications, as it's going to be far too complex to document or train users on...

     


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Wednesday, December 14, 2011 7:15 PM
    Moderator
  • Hi Rvslife,

    How about your issue?

     

    Best regards,


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, December 20, 2011 9:44 AM
    Moderator
  • > We are making host application, which will load 10000-20000 of UI assemblies. In such case we need to support loading and unloading of UI assemblies in host application.
     
     
    below is an example, which creates assembly from source code,  creates AppDomain, then loads created assembly and shows WPF-window.
    it should be noted that AppDomain.Unload is fast enough. but first Run after CreateDomain is slower than the rest.  
     
    using System;
    using System.CodeDom.Compiler;
    using System.Drawing;
    using System.IO;
    using System.Reflection;
    using System.Windows.Forms;
    using Microsoft.CSharp;
    
    namespace WindowsFormsApplication6
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                this.Size = new Size(500, 450);
                var tb = new RichTextBox { Parent = this, Dock = DockStyle.Fill };
                this.Menu = new MainMenu();
                AppDomain ad = null;
                Proxy p = null;
                CreateAssembly("test.dll", tb.AppendText);
                this.Menu.MenuItems.Add("CreateAndRun", (s, e) =>
                {
                    if (ad == null || p == null)
                    {
                        ad = AppDomain.CreateDomain(String.Empty);
                        p = Proxy.Create(ad);
                        tb.AppendText("\nCreated");
                    }
                    tb.AppendText("\nRun");
                    p.Run("test.dll");
                });
                this.Menu.MenuItems.Add("Unload", (s, e) =>
                {
                    tb.AppendText("\nUnload");
                    p = null;
                    if (ad != null)
                        AppDomain.Unload(ad);
                    ad = null;
                });
                this.FormClosing += delegate { File.Delete("test.dll"); };
            }
    
            void CreateAssembly(string fname, Action<string> callback)
            {
                var path = 
                    @"C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\Profile\Client\";
                var assemlies = new[] { 
                        "System.dll",  path+"System.Xaml.dll", path+"WindowsBase.dll", 
                        path+"PresentationFramework.dll",  path+"PresentationCore.dll"
                    };
                var cs = @"
                    public class Test {
                        public static void Run() {
                            var w = new System.Windows.Window() { Width=500.0 };
                            w.Content = new System.Windows.Controls.TextBlock { Text = ""hello"" };
                            w.ShowDialog(); }}";
                var res = Compile(fname, assemlies, cs);
                foreach (CompilerError err in res)
                    callback(err.ErrorText + Environment.NewLine);
            }
            CompilerErrorCollection Compile(string path, string[] assemblies, string cs)
            {
                var csp = new CSharpCodeProvider();
                var cps = new CompilerParameters();
                cps.ReferencedAssemblies.AddRange(assemblies);
                cps.GenerateInMemory = false;
                cps.OutputAssembly = path;
                return csp.CompileAssemblyFromSource(cps, cs).Errors;
            }
        }
    
        public class Proxy : MarshalByRefObject
        {
            public static Proxy Create(AppDomain ad)
            {
                var pt = typeof(Proxy);
                return (Proxy)ad.CreateInstanceAndUnwrap(pt.Assembly.FullName, pt.FullName);
            }
            public void Run(string file)
            {
                var a = Assembly.Load(File.ReadAllBytes(file));  // todo 
                var t = a.GetType("Test", false, true);
                var bf = BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.Static;
                t.GetMethod("Run", bf).Invoke(null, null);
            }
        }
    }
    
      
       
    Wednesday, December 21, 2011 7:12 PM