none
Problems with Import - I think

    Question

  • Hello

    I am trying to do an application with Silverlight 4, Jounce 1 and MEF. For now, fake business objects help me to test the architecture.

    I have a view “ServeurBleuView” who has a one textbox for the value NomServeurBleu. It is databound to a property in ServeurViewModel.

     

    Here is the code behind for the view, just the Jounce stuff :

    namespace Console.View

    {

        [ExportAsView(Global.CONFIG_SERVEURBLEU_VIEW)]

        [ExportViewToRegion(Global.CONFIG_SERVEURBLEU_VIEW, Global.MAIN_REGION)]

        public partial class ServeurBleuView : UserControl

        {

            public ServeurBleuView()

            {

                InitializeComponent();

            }

            [Export]

            public ViewModelRoute Binding

            {

                get { return ViewModelRoute.Create(Global.CONFIG_SERVEURBLEU_VIEWMODEL, Global.CONFIG_SERVEURBLEU_VIEW); }

            }

        }

    }

     

    ServeurBleuViewModel imports a fake business object, type ConnexionServeurBleu.

    namespace Console.ViewModel
    {
        [ExportAsViewModel(Global.CONFIG_SERVEURBLEU_VIEWMODEL)]
        public class ServeurBleuViewModel : BaseViewModel
        {
           [Import]
           public ConnexionServeurBleu _maConnSb { get; set; }
           
          public ServeurBleuViewModel()
            {
                NomServeurBleu = _maConnSb.NomServeurBleu;
                UrlSb = _maConnSb.UrlServeurBleu;
            }
      
         public string NomServeurBleu { get; set; }
         }
    }

     

    The class ConnexionServeurBleu exports itself and imports the parameter of its constructor (type iDonneesSb)

     namespace Console.ObjetsMetiersFake

    {
        [Export]
        public class ConnexionServeurBleu
        {
            private IDonneesSb _connecteurDonnees;
     
            [ImportingConstructor]
            public ConnexionServeurBleu(IDonneesSb connecteurDonnees)
            {
                _connecteurDonnees = connecteurDonnees;
                NomServeurBleu = connecteurDonnees.NomServeurBleu;
            }
    
    
            public string NomServeurBleu { get; set; }
        }
     }

     

    The class DonnesSbXml implements the interface IDonneesSb and exports itself.

     namespace Console.ObjetsMetiersFake

    {
        [Export(typeof (IDonneesSb))]
        public class DonneesSbXml : IDonneesSb
        {
            public DonneesSbXml()
            {
                Initialise();
            }
     
            public string NomServeurBleu { get; set; }
     
            public void Initialise()
            {
                NomServeurBleu = "Serveur Bleu n° 1";
            }
        }

    }

     It seems to me that Jounce take all the composition stuff and should load all the dependencies (ServeurBleuViewModel-> ConnexionServeurBleu -> DonneesSbXml (IDonneesSb interface).

     But I have an error message when I try to use ServeurBleuView:

    http://screencast.com/t/ERuRBGAM4

     

    Mais the solution is easy, but I do not fin dit.

     

    Thank you for your help!

     

    Richard

    Thursday, February 23, 2012 11:46 AM

Answers

  • In the CodePlex someone gave me a solution:

        [ExportAsViewModel(Global.CONFIG_IMPRIMANTEBIDON_VIEWMODEL)]
        public class ImprimanteBidonViewModel : BaseViewModelIPartImportsSatisfiedNotification
        {
            private readonly ILog _logger;
     
            [Import]
            public IImprimanteBidon monImprimanteBidon { getset; }
     
            [ImportingConstructor]
            //public ImprimanteBidonViewModel(IImprimanteBidon monImprimanteBidon, ILog logger)
            public ImprimanteBidonViewModel(ILog logger)
            {
      
                _logger = logger;
            }
     
            public void OnImportsSatisfied()
            {
                if (monImprimanteBidon == nullreturn;
                NomImprimanteBidon = monImprimanteBidon.NomImprimante;
                UrlSb = monImprimanteBidon.UrlImprimante;
                Utilisateur = monImprimanteBidon.NomUtilisateur;
                SourceDonnees = monImprimanteBidon.ProvenanceDonnees;
            }
    Friday, March 02, 2012 5:11 AM

All replies

  • Richard, have you tried placing a breakpoint in the constructors for the objects in the dependency chain? MEF has to create those to resolve dependencies. You are right, they all cascade so it should resolve but something is going awry, and probably happens when MEF creates the instance. Try setting the breakpoints and that may help you narrow it down.

    Thursday, February 23, 2012 11:59 AM
  • Hello and thank you for your help, Jeremy !

    It is

     [Import]

           public ConnexionServeurBleu _maConnSb { get; set; }

    who does not import the

    {
        [Export]
        public class ConnexionServeurBleu
        {

     

     

    I tried with  [Import("conn.sb")] and   [Export ("conn.sb")], but it changes nothing.
    Thursday, February 23, 2012 12:21 PM
  • Hi,

    i've searched the word Jounce .

    I'm just know something about MEF:

    http://blogs.msdn.com/b/brada/archive/2009/07/20/simple-example-using-managed-extensibility-framework-in-silverlight.aspx

    I suggest you ot ask the developer directly:

    http://jounce.codeplex.com/discussions

    Monday, February 27, 2012 1:25 AM
  • Hi Anna, and thanks for your answers.

    It happens that the developper of Jounce is Jeremy Likeness, who has posted an answer on this topic.

    I hope he will be (or another person) able to help me. It seems that the ViewModel does not import the business objetc, I tried with another BO with the same result. The others importations are ok.

    Edit : I don't find the solution :( . I do not understand why

     [Export(typeof (IDonneesSb))]
     public class DonneesSbXml : IDonneesSb

    and

     [ImportingConstructor]
    public ConnexionServeurBleu(IDonneesSb connecteurDonnees)



    work but

    but the import in the viewmodel does not. The objetcs above are imported because there in an import in the viewModel, but this import does not work.

    Monday, February 27, 2012 10:53 AM
  • Well, I guess it will be difficult.

    I found a way to get things working, somehow.

    My code is now

     

    [ExportAsViewModel(Global.CONFIG_IMPRIMANTEBIDON_VIEWMODEL)]

        public class ImprimanteBidonViewModel : BaseViewModel

        {

           private readonly ILogger _logger;

     

            [ImportingConstructor]

            public ImprimanteBidonViewModel(IImprimanteBidon monImprimanteBidon)

            {

                NomServeurBleu = monImprimanteBidon.NomImprimante;

                UrlSb = monImprimanteBidon.UrlImprimante;

                Utilisateur = monImprimanteBidon.NomUtilisateur;

                SourceDonnees = monImprimanteBidon.ProvenanceDonnees;

            }

     

    As you see, I deleted  the [Import] and I use now and [ImportingConstructor]. I did the same the same thing with others objects and it works. But I do not understand why.  The [Import] should work, right?

     

    And now I have another problem. I made a Logger class, who implements the interface ILog.

     

    When I use it with one class only, like in

     

            [ImportingConstructor]

            public ImprimanteBidon(IDonneesImprBid connecteurDonnees, ILog logger)

            {

     

    it works, the logger is imported and I can use it. But if want to use it the the viewModel too, like here

             [ImportingConstructor]

            public ImprimanteBidonViewModel(IImprimanteBidon monImprimanteBidon, ILog logger)

            {

     

    I get this error message : http://screencast.com/t/RGpxQ5QdslF

     

     

    I do not understand why. I did not specify that the ILog object should not be shared, I just wrote

         [Export(typeof(ILog))]

        public class FileLogger : ILog

        {

           

    So, am I right to think that the instance of the ILog should shared by both ImprimanteBidon and ImprimanteBidonViewModel?

     

    It is the same logger I would like to use with several classes. Any idea why it does not work?

     

    I begin to doubt it is a good idea for me to use Silverlight and Jounce. Silverlight, Jounce and MEF seems to be great tools, but maybe it takes a lot of time to learn them.

    Thank you for your help

     

    Tuesday, February 28, 2012 12:27 PM
  • In the CodePlex someone gave me a solution:

        [ExportAsViewModel(Global.CONFIG_IMPRIMANTEBIDON_VIEWMODEL)]
        public class ImprimanteBidonViewModel : BaseViewModelIPartImportsSatisfiedNotification
        {
            private readonly ILog _logger;
     
            [Import]
            public IImprimanteBidon monImprimanteBidon { getset; }
     
            [ImportingConstructor]
            //public ImprimanteBidonViewModel(IImprimanteBidon monImprimanteBidon, ILog logger)
            public ImprimanteBidonViewModel(ILog logger)
            {
      
                _logger = logger;
            }
     
            public void OnImportsSatisfied()
            {
                if (monImprimanteBidon == nullreturn;
                NomImprimanteBidon = monImprimanteBidon.NomImprimante;
                UrlSb = monImprimanteBidon.UrlImprimante;
                Utilisateur = monImprimanteBidon.NomUtilisateur;
                SourceDonnees = monImprimanteBidon.ProvenanceDonnees;
            }
    Friday, March 02, 2012 5:11 AM