Use Unity Property Dependency Injection with EventAggregator











up vote
0
down vote

favorite












I have WPF Project with Prism 7.1, Unity DI 5.8.11 and .NET framework 4.7



I have BaseViewModel which all the ViewModel classes will inherit from



public abstract class BaseViewModel : BindableBase
{
[Dependency]
protected IEventAggregator EventAggregator { get; set; }

// just default constructor, no other constructor is needed
}


and here is an example of one of the ViewModel class



public class TablesViewModel : BaseViewModel
{
public TablesViewModel()
{
EventAggregator.GetEvent<OperatorChangedEvent>().Subscribe(.....);
}
}


and the register of the type as the following



protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
containerRegistry.RegisterInstance<IEventAggregator>(new EventAggregator());
}


Now, what happened is the following: First the constructor of the BaseViewModel is called then the constructor of the TablesViewModel then the Dependency property is set by the Unity DI, this is a logical sequence of events, but it does not fit for me.
the constructor of the TablesViewModel is giving a null reference Exception because the EventAggregator property is still null.



I do not want to use the constructor dependency injection, this will force me to create a lot of non-default constructor for all the ViewModel classes.
and at the same time, I need to subscribe to the EventAggregator at the constructor ( because there is no other good place to do that if there is please told me).



How can I solve this










share|improve this question


























    up vote
    0
    down vote

    favorite












    I have WPF Project with Prism 7.1, Unity DI 5.8.11 and .NET framework 4.7



    I have BaseViewModel which all the ViewModel classes will inherit from



    public abstract class BaseViewModel : BindableBase
    {
    [Dependency]
    protected IEventAggregator EventAggregator { get; set; }

    // just default constructor, no other constructor is needed
    }


    and here is an example of one of the ViewModel class



    public class TablesViewModel : BaseViewModel
    {
    public TablesViewModel()
    {
    EventAggregator.GetEvent<OperatorChangedEvent>().Subscribe(.....);
    }
    }


    and the register of the type as the following



    protected override void RegisterTypes(IContainerRegistry containerRegistry)
    {
    containerRegistry.RegisterInstance<IEventAggregator>(new EventAggregator());
    }


    Now, what happened is the following: First the constructor of the BaseViewModel is called then the constructor of the TablesViewModel then the Dependency property is set by the Unity DI, this is a logical sequence of events, but it does not fit for me.
    the constructor of the TablesViewModel is giving a null reference Exception because the EventAggregator property is still null.



    I do not want to use the constructor dependency injection, this will force me to create a lot of non-default constructor for all the ViewModel classes.
    and at the same time, I need to subscribe to the EventAggregator at the constructor ( because there is no other good place to do that if there is please told me).



    How can I solve this










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have WPF Project with Prism 7.1, Unity DI 5.8.11 and .NET framework 4.7



      I have BaseViewModel which all the ViewModel classes will inherit from



      public abstract class BaseViewModel : BindableBase
      {
      [Dependency]
      protected IEventAggregator EventAggregator { get; set; }

      // just default constructor, no other constructor is needed
      }


      and here is an example of one of the ViewModel class



      public class TablesViewModel : BaseViewModel
      {
      public TablesViewModel()
      {
      EventAggregator.GetEvent<OperatorChangedEvent>().Subscribe(.....);
      }
      }


      and the register of the type as the following



      protected override void RegisterTypes(IContainerRegistry containerRegistry)
      {
      containerRegistry.RegisterInstance<IEventAggregator>(new EventAggregator());
      }


      Now, what happened is the following: First the constructor of the BaseViewModel is called then the constructor of the TablesViewModel then the Dependency property is set by the Unity DI, this is a logical sequence of events, but it does not fit for me.
      the constructor of the TablesViewModel is giving a null reference Exception because the EventAggregator property is still null.



      I do not want to use the constructor dependency injection, this will force me to create a lot of non-default constructor for all the ViewModel classes.
      and at the same time, I need to subscribe to the EventAggregator at the constructor ( because there is no other good place to do that if there is please told me).



      How can I solve this










      share|improve this question













      I have WPF Project with Prism 7.1, Unity DI 5.8.11 and .NET framework 4.7



      I have BaseViewModel which all the ViewModel classes will inherit from



      public abstract class BaseViewModel : BindableBase
      {
      [Dependency]
      protected IEventAggregator EventAggregator { get; set; }

      // just default constructor, no other constructor is needed
      }


      and here is an example of one of the ViewModel class



      public class TablesViewModel : BaseViewModel
      {
      public TablesViewModel()
      {
      EventAggregator.GetEvent<OperatorChangedEvent>().Subscribe(.....);
      }
      }


      and the register of the type as the following



      protected override void RegisterTypes(IContainerRegistry containerRegistry)
      {
      containerRegistry.RegisterInstance<IEventAggregator>(new EventAggregator());
      }


      Now, what happened is the following: First the constructor of the BaseViewModel is called then the constructor of the TablesViewModel then the Dependency property is set by the Unity DI, this is a logical sequence of events, but it does not fit for me.
      the constructor of the TablesViewModel is giving a null reference Exception because the EventAggregator property is still null.



      I do not want to use the constructor dependency injection, this will force me to create a lot of non-default constructor for all the ViewModel classes.
      and at the same time, I need to subscribe to the EventAggregator at the constructor ( because there is no other good place to do that if there is please told me).



      How can I solve this







      wpf dependency-injection unity-container prism eventaggregator






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 9 at 15:02









      Hakam Fostok

      5,08583863




      5,08583863
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted











          I do not want to use the constructor dependency injection, this will force me to create a lot of non-default constructor for all the ViewModel classes.




          You want non-default constructors for classes that have dependencies. That's what dependency injection is all about: the types tell the user through their constructor parameters what he has to give them to operate.



          There are plenty of ways to create view models with non-default constructors, e.g. Prism's ViewModelLocator or Unity's automatic factories. You do not want to resort to using the ServiceLocator unless it is absolutely necessary, but an evil person technically could do something like this:



          public abstract class BaseViewModel : BindableBase
          {
          protected IEventAggregator EventAggregator { get; } = ServiceLocator.Current.GetInstance<IEventAggregator>();
          }





          share|improve this answer





















          • that's it thank you.
            – Hakam Fostok
            Nov 12 at 5:42


















          up vote
          1
          down vote













          A property cannot be set before an instance of the class has been created, i.e. the EventAggregator property won't ever be set before the constructor has been executed.



          If you need to access the event aggregator in the constructor, you should either use constructor dependency injection or retrieve the event aggregator from some static property.






          share|improve this answer























          • Nice suggestion about the static property, because I will not go with the idea of constructor dependency injection, but would not that eliminate the benefits of the DI pattern?
            – Hakam Fostok
            Nov 9 at 16:15










          • do you have any ideas about subscribing the events in some other place than the constructor?
            – Hakam Fostok
            Nov 9 at 16:17










          • ServiceLocator.Current is a static property and yes, this does eliminate the benefits of the DI pattern. So does the "not go with the idea of constructor dependency injection" option.
            – mm8
            Nov 13 at 8:50











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53228207%2fuse-unity-property-dependency-injection-with-eventaggregator%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote



          accepted











          I do not want to use the constructor dependency injection, this will force me to create a lot of non-default constructor for all the ViewModel classes.




          You want non-default constructors for classes that have dependencies. That's what dependency injection is all about: the types tell the user through their constructor parameters what he has to give them to operate.



          There are plenty of ways to create view models with non-default constructors, e.g. Prism's ViewModelLocator or Unity's automatic factories. You do not want to resort to using the ServiceLocator unless it is absolutely necessary, but an evil person technically could do something like this:



          public abstract class BaseViewModel : BindableBase
          {
          protected IEventAggregator EventAggregator { get; } = ServiceLocator.Current.GetInstance<IEventAggregator>();
          }





          share|improve this answer





















          • that's it thank you.
            – Hakam Fostok
            Nov 12 at 5:42















          up vote
          1
          down vote



          accepted











          I do not want to use the constructor dependency injection, this will force me to create a lot of non-default constructor for all the ViewModel classes.




          You want non-default constructors for classes that have dependencies. That's what dependency injection is all about: the types tell the user through their constructor parameters what he has to give them to operate.



          There are plenty of ways to create view models with non-default constructors, e.g. Prism's ViewModelLocator or Unity's automatic factories. You do not want to resort to using the ServiceLocator unless it is absolutely necessary, but an evil person technically could do something like this:



          public abstract class BaseViewModel : BindableBase
          {
          protected IEventAggregator EventAggregator { get; } = ServiceLocator.Current.GetInstance<IEventAggregator>();
          }





          share|improve this answer





















          • that's it thank you.
            – Hakam Fostok
            Nov 12 at 5:42













          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted







          I do not want to use the constructor dependency injection, this will force me to create a lot of non-default constructor for all the ViewModel classes.




          You want non-default constructors for classes that have dependencies. That's what dependency injection is all about: the types tell the user through their constructor parameters what he has to give them to operate.



          There are plenty of ways to create view models with non-default constructors, e.g. Prism's ViewModelLocator or Unity's automatic factories. You do not want to resort to using the ServiceLocator unless it is absolutely necessary, but an evil person technically could do something like this:



          public abstract class BaseViewModel : BindableBase
          {
          protected IEventAggregator EventAggregator { get; } = ServiceLocator.Current.GetInstance<IEventAggregator>();
          }





          share|improve this answer













          I do not want to use the constructor dependency injection, this will force me to create a lot of non-default constructor for all the ViewModel classes.




          You want non-default constructors for classes that have dependencies. That's what dependency injection is all about: the types tell the user through their constructor parameters what he has to give them to operate.



          There are plenty of ways to create view models with non-default constructors, e.g. Prism's ViewModelLocator or Unity's automatic factories. You do not want to resort to using the ServiceLocator unless it is absolutely necessary, but an evil person technically could do something like this:



          public abstract class BaseViewModel : BindableBase
          {
          protected IEventAggregator EventAggregator { get; } = ServiceLocator.Current.GetInstance<IEventAggregator>();
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 9 at 16:59









          Haukinger

          4,8692619




          4,8692619












          • that's it thank you.
            – Hakam Fostok
            Nov 12 at 5:42


















          • that's it thank you.
            – Hakam Fostok
            Nov 12 at 5:42
















          that's it thank you.
          – Hakam Fostok
          Nov 12 at 5:42




          that's it thank you.
          – Hakam Fostok
          Nov 12 at 5:42












          up vote
          1
          down vote













          A property cannot be set before an instance of the class has been created, i.e. the EventAggregator property won't ever be set before the constructor has been executed.



          If you need to access the event aggregator in the constructor, you should either use constructor dependency injection or retrieve the event aggregator from some static property.






          share|improve this answer























          • Nice suggestion about the static property, because I will not go with the idea of constructor dependency injection, but would not that eliminate the benefits of the DI pattern?
            – Hakam Fostok
            Nov 9 at 16:15










          • do you have any ideas about subscribing the events in some other place than the constructor?
            – Hakam Fostok
            Nov 9 at 16:17










          • ServiceLocator.Current is a static property and yes, this does eliminate the benefits of the DI pattern. So does the "not go with the idea of constructor dependency injection" option.
            – mm8
            Nov 13 at 8:50















          up vote
          1
          down vote













          A property cannot be set before an instance of the class has been created, i.e. the EventAggregator property won't ever be set before the constructor has been executed.



          If you need to access the event aggregator in the constructor, you should either use constructor dependency injection or retrieve the event aggregator from some static property.






          share|improve this answer























          • Nice suggestion about the static property, because I will not go with the idea of constructor dependency injection, but would not that eliminate the benefits of the DI pattern?
            – Hakam Fostok
            Nov 9 at 16:15










          • do you have any ideas about subscribing the events in some other place than the constructor?
            – Hakam Fostok
            Nov 9 at 16:17










          • ServiceLocator.Current is a static property and yes, this does eliminate the benefits of the DI pattern. So does the "not go with the idea of constructor dependency injection" option.
            – mm8
            Nov 13 at 8:50













          up vote
          1
          down vote










          up vote
          1
          down vote









          A property cannot be set before an instance of the class has been created, i.e. the EventAggregator property won't ever be set before the constructor has been executed.



          If you need to access the event aggregator in the constructor, you should either use constructor dependency injection or retrieve the event aggregator from some static property.






          share|improve this answer














          A property cannot be set before an instance of the class has been created, i.e. the EventAggregator property won't ever be set before the constructor has been executed.



          If you need to access the event aggregator in the constructor, you should either use constructor dependency injection or retrieve the event aggregator from some static property.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 9 at 15:57

























          answered Nov 9 at 15:20









          mm8

          79.8k81731




          79.8k81731












          • Nice suggestion about the static property, because I will not go with the idea of constructor dependency injection, but would not that eliminate the benefits of the DI pattern?
            – Hakam Fostok
            Nov 9 at 16:15










          • do you have any ideas about subscribing the events in some other place than the constructor?
            – Hakam Fostok
            Nov 9 at 16:17










          • ServiceLocator.Current is a static property and yes, this does eliminate the benefits of the DI pattern. So does the "not go with the idea of constructor dependency injection" option.
            – mm8
            Nov 13 at 8:50


















          • Nice suggestion about the static property, because I will not go with the idea of constructor dependency injection, but would not that eliminate the benefits of the DI pattern?
            – Hakam Fostok
            Nov 9 at 16:15










          • do you have any ideas about subscribing the events in some other place than the constructor?
            – Hakam Fostok
            Nov 9 at 16:17










          • ServiceLocator.Current is a static property and yes, this does eliminate the benefits of the DI pattern. So does the "not go with the idea of constructor dependency injection" option.
            – mm8
            Nov 13 at 8:50
















          Nice suggestion about the static property, because I will not go with the idea of constructor dependency injection, but would not that eliminate the benefits of the DI pattern?
          – Hakam Fostok
          Nov 9 at 16:15




          Nice suggestion about the static property, because I will not go with the idea of constructor dependency injection, but would not that eliminate the benefits of the DI pattern?
          – Hakam Fostok
          Nov 9 at 16:15












          do you have any ideas about subscribing the events in some other place than the constructor?
          – Hakam Fostok
          Nov 9 at 16:17




          do you have any ideas about subscribing the events in some other place than the constructor?
          – Hakam Fostok
          Nov 9 at 16:17












          ServiceLocator.Current is a static property and yes, this does eliminate the benefits of the DI pattern. So does the "not go with the idea of constructor dependency injection" option.
          – mm8
          Nov 13 at 8:50




          ServiceLocator.Current is a static property and yes, this does eliminate the benefits of the DI pattern. So does the "not go with the idea of constructor dependency injection" option.
          – mm8
          Nov 13 at 8:50


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53228207%2fuse-unity-property-dependency-injection-with-eventaggregator%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          How to pass form data using jquery Ajax to insert data in database?

          National Museum of Racing and Hall of Fame

          Guess what letter conforming each word