Constructor with Parameters

Dec 16, 2011 at 12:57 AM

Is there a way to pass in different parameter value to Phase1 and Phase2?

[Export(typeof(Phase)), PhaseMetaData("Phase2")]
    public class Phase2 : Phase
    {
         [ImportingConstructor]
         public Phase2(PhaseParameters phaseParams)
         {
             this.Parameters = phaseParams;
         }
    }

 

[Export(typeof(Phase)), PhaseMetaData("Phase1")]
    public class Phase1 : Phase
    {
         [ImportingConstructor]
         public Phase1(PhaseParameters phaseParams)
         {
             this.Parameters = phaseParams;
         }
    }

 

 public class PhaseManager
    {

        [ImportMany(typeof(Phase))]
        private IEnumerable<Lazy<Phase, PhaseMetaData>> phases;

        // keeps track of which phase to load
        private List<string> phaseList = new List<string>();
        private Dictionary<string, Phase> phaseDictionary = new Dictionary<string, Phase>();
       
        public void Initialize()
        {
            ImportPhases();
        }

      
        public void ImportPhases()
        {
            // an aggregate catalog that combines multiple catalogs
            var catalog = new AggregateCatalog();

            // check the current assembly's directory for assemblies with matching Export interface types
            catalog.Catalogs.Add(new AssemblyCatalog(typeof(Phase).Assembly));

            // create the CompositionContainer with the parts in the catalog
            CompositionContainer container = new CompositionContainer(catalog);
 
            // i'M Lost on how to pass in different phase parameter object
            PhaseParameters parameters = new PhaseParameters();
            parameters.Parameter1 = "1";

            container.ComposeExportedValue(parameters);
            // fill the imports of this container object
            container.ComposeParts(this);

           
            var query = phases.Where(p => phaseList.Contains(p.Metadata.Name.ToLower())).Select(v => v.Value);
            foreach (var a in query)
            {

            }
        }

    }

 

Thanks,

Hoay

Dec 16, 2011 at 7:23 PM

You can name your PhaseParameters implementation using a contract name:

public Phase2([Import("phase2parameters")] PhaseParameters phaseParams)
{
this.Parameters = phaseParams;
}

And then export multiple phase parameter instances using property exports somewhere, with the corresponding contract names.

[Export("phase2parameters")]
public PhaseParameters Phase2Params { get { … } }


However, you might also consider just passing the PhaseParameters to whatever methods you expose on Phase. It looks like PhaseParameters could be viewed more as data passed between parts, than a part in itself.

Hope this helps,
Nick

From: hmyap [email removed]
Sent: Thursday, December 15, 2011 4:58 PM
To: Nicholas Blumhardt
Subject: Constructor with Parameters [MEF:283155]

From: hmyap

Is there a way to pass in different parameter value to Phase1 and Phase2?

[Export(typeof(Phase)), PhaseMetaData("Phase2")]
public class Phase2 : Phase
{
[ImportingConstructor]
public Phase2(PhaseParameters phaseParams)
{
this.Parameters = phaseParams;
}
}

[Export(typeof(Phase)), PhaseMetaData("Phase1")]
public class Phase1 : Phase
{
[ImportingConstructor]
public Phase1(PhaseParameters phaseParams)
{
this.Parameters = phaseParams;
}
}

public class PhaseManager
{

[ImportMany(typeof(Phase))]
private IEnumerable<Lazy<Phase, PhaseMetaData>> phases;

// keeps track of which phase to load
private List<string> phaseList = new List<string>();
private Dictionary<string, Phase> phaseDictionary = new Dictionary<string, Phase>();

public void Initialize()
{
ImportPhases();
}


public void ImportPhases()
{
// an aggregate catalog that combines multiple catalogs
var catalog = new AggregateCatalog();

// check the current assembly's directory for assemblies with matching Export interface types
catalog.Catalogs.Add(new AssemblyCatalog(typeof(Phase).Assembly));

// create the CompositionContainer with the parts in the catalog
CompositionContainer container = new CompositionContainer(catalog);

// i'M Lost on how to pass in different phase parameter object
PhaseParameters parameters = new PhaseParameters();
parameters.Parameter1 = "1";

container.ComposeExportedValue(parameters);
// fill the imports of this container object
container.ComposeParts(this);


var query = phases.Where(p => phaseList.Contains(p.Metadata.Name.ToLower())).Select(v => v.Value);
foreach (var a in query)
{

}
}

}

Thanks,

Hoay