FreshMvvm n=2 – IOC and Constructor Injection

Here’s the next video in the  FreshMvvm n+1 series. The idea of this series is to get the video’s out to the eager listeners (you) as quickly as possible, therefore these videos are done live with almost no editing.

Episode Title: n=2 – IOC and Constructor Injection

In this episode we discuss:

  • Review FreshMvvm’s built in IOC container
  • Setup a decoupled Data Service with interface
  • Use the PageModel Constructor Injection
  • Use a 3rd Party Library in the IOC Container

I hope you enjoy.

 

 

9 Responses

    1. I wanted a built in IOC container for the simple use of users. I tested TinyIOC vs AutoFaq, TinyIOC was simple to use and with TinyIOC you don’t need to register PageModel/ViewModel before creating them.

    1. Hi Brandon. In a normal situation I create a new async Task method and kick it off to run in the background from the init method. Then it’s easier to unit test.

      eg.
      async Task LoadQuotes()
      {

      }

      I only did this for the sample, probably a bit of misleading education I would fix it up in a future video.

  1. I noticed at 6:54 you changed the Init method to async however, this method returns void. According to Microsoft best practices, async void is highly frowned upon: https://msdn.microsoft.com/en-us/magazine/jj991977.aspx

    Would it make sense then to change the signature of the Init in the base to be async Task and have that be the default? That way any consumer can call it synchronously or async. This would also make testing significantly easier as async void is very difficult to test and unsupported in many frameworks.

    1. Hi Bardia Noohi, you are correct but I’ve only done this for the purposes of the demo. In a normal situation I create a new async Task method and kick it off to run in the background from the init method. Then it’s easier to unit test.

      eg.
      async Task LoadQuotes()
      {

      }

      I’ve thought about changing the method signature, I will test this out and possibly update.

      I will do a new video with some ways I tackle this and with the unit test included.

      It was wrong of me to write this code in a demo I will be sure to avoid shortcuts in the future. :)

  2. Hi Michael

    FreshMVVM has been a huge time saver! And, yes, it “just works”!

    Was following along with your videos and ran into the same Null Reference Exception while trying to display a “Loading” user dialog in iOS. I was wondering if you ever found how to resolve this issue.

    I’ve updated all packages, including Acr.UserDialogs to the latest, and am still getting the error. Research over on GitHub aritchie/userdialogs has not yielded any solution thus far.

    Thanks,
    Jim

    1. As is often the case, right after I ask a question, I stumble upon the answer…

      I was able to resolve the issue by moving the ShowLoading() call to the page models ViewIsAppearing event handler. Left the HideLoading() call where it originally was, at the end of the page model’s Init() method as show in the video.

      -Jim

Leave a Reply