3

About IOC, I read below definition & registering interface which really I am not able to make out

We don’t need to include our own IOC container, FreshMvvm comes with a IOC container built-in. It’s using TinyIOC underneath, but with different naming to avoid conflicts.

Interface rgistration with FreshIOC, If you see this code, In starting of application making this implementaion

public App()  
{  
    InitializeComponent();  

    //Interface rgistration with FreshIOC  
    FreshIOC.Container.Register<IContactRepository, ContactRepository>();  
    FreshIOC.Container.Register<IValidator, ContactValidator>();  

    //var mainPage = FreshPageModelResolver.ResolvePageModel<AddContactPageModel>();  
    //MainPage = new FreshNavigationContainer(mainPage);  
} 

Why do we need registering interface, If not registering than what would be real implementation of it? Is there any advantages of implementing this principle. This article I am following.

R15
  • 13,982
  • 14
  • 97
  • 173
  • I am trying to understand what it is you do not understand about the already provided answer and the concept of DI and IoC? I want to understand what needs explaining. – Nkosi Oct 18 '18 at 09:57
  • I am not understanding why are we doing that -- `Interface rgistration with FreshIOC` & If not registering interface than How it used to be done. Is there any benefit of using IOC? These all question I am asking for sake of understanding only. Lately I started venture into MVVM. You might observing it from your point of understanding :) Thank you. – R15 Oct 18 '18 at 10:04
  • Ok I can work with that. – Nkosi Oct 18 '18 at 10:06
  • Quick question do you already understand the concept of dependency inversion and dependency injection? – Nkosi Oct 18 '18 at 10:06
  • Sorry to say no, I have never come across it. – R15 Oct 18 '18 at 10:07

2 Answers2

4

If you've used DependencySerices in Xamarin.Forms, you already most of it. Explaining from the perspective of Xamarin.Forms

Let's assume your ContentPage needs a Network class to check if there is network connectivity, the traditional way of doing it would be using new keyword and get the instance, so that you can call upon its methods.

public MyContentPage : ContentPage
{
    private Network network;
    public MyContentPage()
    {
        //..
        network = new Network();
    }
}

public Network()
{
    public bool HasConnectivity() { ... }
}

There is nothing wrong in this, but what if the Network class needs a Log class inside it? And the MyContentPage also needs Log class and a Dialog class? And this needs to be done in all your 50 other pages?? Dependency Injection addresses these and many more!

You create interface and its implementation, then register them with a container. Then the container resolve all the dependencies for you!

public MyContentPage : ContentPage
{
    private INetwork _network;
    private IDialog _dialog;
    public MyContentPage(INetwork network, IDialog dialog)
    {
        //..
        _network = network;
        _dialog = dialog;
    }
}    

public Network(ILog log)
{
    public bool HasConnectivity() { ... }
}

If you've registered all the dependencies, Container will take care of dependency graph and resolve them for you. If the Container was unable to resolve the graph, probably because you didn't register or may be cyclic dependencies, it'll throw out exception.

This seems to be totally unnecessary at first but as your app grows MVVM coupled with DI can be more powerful and easier for development.

What I've explained is just a small part of DI, you can read more about IoC and DI in this awesome Martin Fowler post

dss539
  • 6,804
  • 2
  • 34
  • 64
memsranga
  • 1,088
  • 7
  • 13
  • Ultimately you seem to do same thing in `MyContentPage` constructor, what you are doing first part of code using `new` keyword. I mean to say in first code snippet you have written 1 line of code & in 2nd too you are writing 1 line of code than what difference does it make. – R15 Oct 19 '18 at 06:15
  • It is not about the number of lines of code! If you want to replace `Log` class with `ConsoleLog` class in all your 50 pages, you have to make changes in all the pages. But through DI, you have loose coupling, just register ILog with ConsoleLog!! Similarly, you can test them easily too! – memsranga Oct 19 '18 at 07:31
  • 2
    @Arvindraja as Shan says, Martin Fowler's post is extremely helpful to understand `Inversion of Control` and `Dependency Injection` This is an oversimplification, but it might help to first think of IoC as "no class can call `new OtherClass()`" Now... why the heck would you want to write code like that? That's a different question :) So instead of `new OtherClass()` you just do `IocFramework.GiveMe()` and let the IoC figure out what to do. Then you go one step further and just accept an `OtherClass` in your own class constructor. – dss539 Oct 19 '18 at 18:37
  • 1
    Now you will rightly ask "why bother with this?!"... Because it changes who is "in control" of constructing and keeping track of the `OtherClass` object. By changing that, you open up a lot of possibilities for testing, and you greatly simplify your code base.... unless it's tiny. If you have only have like 10 classes, using an IoC framework isn't really a huge win. – dss539 Oct 19 '18 at 18:41
  • @Shan - It is just changing the class names, Or other plus points do we have? – R15 Oct 22 '18 at 09:34
  • @Arvindraja its not just a name change, you change the implementation of the class based on ILog interface. If ILog has writeToLog method, Log class may write it to file where as ConsoleLog class may write to debug console only. You can plug any implementation you want without any effort, as long as it implements the interface – memsranga Oct 22 '18 at 10:06
3

The built-in IOC container is an abstraction of TinyIOC.

To register a type in the TinyIOC container through the FreshMvvm abstraction:

FreshIOC.Container.Register<ISomeInterface, SomeImplementation>();

To later use it:

// Gets object of type SomeImplementation
var instanceOfConcreteType = FreshIOC.Container.Resolve<ISomeInterface>();

Here is a post discussing what IOC is: What is Inversion of Control?

Ted Nyberg
  • 7,001
  • 7
  • 41
  • 72