Reputation: 17422
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.
Upvotes: 3
Views: 752
Reputation: 1108
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
Upvotes: 4
Reputation: 7391
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?
Upvotes: 3