New Posts New Posts RSS Feed: MEF Import Property
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

MEF Import Property

 Post Reply Post Reply
Author
gregweb View Drop Down
DevForce MVP
DevForce MVP
Avatar

Joined: 10-Sep-2009
Location: Clearwater, Fl
Posts: 253
Post Options Post Options   Quote gregweb Quote  Post ReplyReply Direct Link To This Post Topic: MEF Import Property
    Posted: 08-Feb-2013 at 10:28am
In the DbIntializer, instead of one large seed method, I have it broken out into different classes one for each type, using an Interface, ISeedDb.

I am then trying to import them through a property on the DbInitializer. However, I no imports come through:

public interface ISeedDatabase {
        bool HasRoot { get; }
        void Seed(JetDbContext context);
    }



public class JetDbInitializer : CreateDatabaseIfNotExists<JetDbContext>
    {
        [ImportMany]
        public IEnumerable<ISeedDatabase> Seeds { get; set; }
}

[Export(typeof(ISeedDatabase)), PartCreationPolicy(CreationPolicy.Shared)]
    public class SeedPhoneTypes : ISeedDatabase
    {
        public bool HasRoot { get { return false; }}
        public void Seed(JetDbContext context) {
            var phoneTypes = new List<PhoneNumberType>
                                 {
                                    new PhoneNumberType {Id = CombGuid.NewGuid(), Name = "Home", Default = true},
                                    new PhoneNumberType {Id = CombGuid.NewGuid(), Name = "Work"},
                                    new PhoneNumberType {Id = CombGuid.NewGuid(), Name = "Mobile"}
                                 };
            phoneTypes.ForEach(e => context.PhoneNumberTypes.Add(e));
        }
    }

I am not sure what I am doing wrong.

Greg
Back to Top
mgood View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 18-Nov-2010
Location: Emeryville, CA
Posts: 583
Post Options Post Options   Quote mgood Quote  Post ReplyReply Direct Link To This Post Posted: 08-Feb-2013 at 11:10am
Greg,
Dependencies are only satisfied if the part in question is managed by MEF. If you simply new up your JetDbInitializer, MEF has no awareness of it and won't satisfy any dependencies. You have two options.
 
First option, you can manually satisfy the dependencies by passing the JetDbInitializer instance to Composition.BuildUp(). For example you can do that in the JetDbInitializer ctor by calling Composition.BuildUp(this).
 
The other option, you export the JetDbInitializer and then create the instance by calling Composition.GetInstance<JetDbInitializer>().
 
[Export, PartCreationPolicy(CreationPolicy.NonShared)]
public class JetDbInitializer : CreateDatabaseIfNotExists<JetDbContext>
{
[ImportMany]
public IEnumerable<ISeedDatabase> Seeds { get; set; }
}


Edited by mgood - 08-Feb-2013 at 11:11am
Back to Top
gregweb View Drop Down
DevForce MVP
DevForce MVP
Avatar

Joined: 10-Sep-2009
Location: Clearwater, Fl
Posts: 253
Post Options Post Options   Quote gregweb Quote  Post ReplyReply Direct Link To This Post Posted: 08-Feb-2013 at 6:17pm
Thanks, got that working.
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down