MEF+Static Class+Import/Export

Topics: Prism v4 - Silverlight 4
Nov 16, 2010 at 7:06 PM

Hy Guys, 

Are there any restrictions for using the MEF with static class?

I have that class:

    public interface IDataService
    {
        ASDomainContext Context { get; }
    }

 [Export(typeof(IDataService))]
    [PartCreationPolicy(CreationPolicy.Shared)]
    public class DataService : IDataService
    {
        private ASDomainContext _context;

        public ASDomainContext Context
        {
            get
            {
                if (_context == null)
                {
                    _context = new ASDomainContext();
                }
                return _context;
            }
        }

        public DataService()
        {
            
        }

 

and i need to use that in a Static class like that:

 public static partial class Helpers
    {

        [Import]
        public static IDataService DataService;

    }

but its always null.

 

How can i do that?

Nov 16, 2010 at 7:44 PM
Edited Nov 16, 2010 at 7:51 PM

Hi,

MEF does not support static imports. This is why your import is not being satisfied. You can find the following forum threads interesting from MEF's forum:

Fernando Antivero
http://blogs.southworks.net/fantivero

 

Nov 17, 2010 at 2:00 PM
Edited Nov 17, 2010 at 2:00 PM

that solution didnt work for me,

 

i tried another one, but still not working.

i changed my Helpers class to non static class:

public partial class Helpers
    {
        private static Helpers _instance;

        public static Helpers Instance
        {
            get { return _instance ?? (_instance = new Helpers()); }
        }

        public Helpers()
        {
            
        }

        private LoginInfo _loginInfo;
        
        public LoginInfo LoginInfoInstance
        {
            get { return _loginInfo ?? (_loginInfo = new LoginInfo()); }
        }

    }

  public class LoginInfo
    {  
        
        [Import]
        public IDataService _dataService;

        
        public LoginInfo()
        {
            
        }
}

The LoginInfo class is not static, and the import is not working. What am i doing wrong here?

 

Nov 17, 2010 at 2:45 PM

Hi,

When you need to satisfy the imports in a particular class (LoginInfo), you will need either export that class or call the SatisfyImports method in the CompositionInitializer class from the constructor.

That said, your code might be similar to the following:

[Export]
public class LoginInfo
{
[Import]
public IDataService _dataService;
public LoginInfo() { }
}
...
container.GetExportedValue<LoginInfo>
...

Or you could use the CompositionInitializer class, which resides in System.ComponentModel.Composition.Initialization assembly:

public class LoginInfo
{
[Import]
public IDataService _dataService;
public LoginInfo()
{
CompositionInitializer.SatisfyImports(this);
}
}

If you need more information about the CompositionInitializer class, you could take a look at the following documentation section: Hosting MEF in Silverlight

Hope this helps.

Fernando Antivero
http://blogs.southworks.net/fantivero

 

Nov 17, 2010 at 5:37 PM

th Fernando,

CompositionInitializer.SatisfyImports(this); 

Works fine..

 

 

Nov 17, 2010 at 5:40 PM

Hi,

I am glad that you find this useful in your scenario. Please, let me know if you have any other question.

Have a great day.

Fernando Antivero
http://blogs.southworks.net/fantivero