What is dependency injection interface in Xamarin forms?


Isn't it fun,

to instantly listen to a blog on the go? PLAY !

 
 

injection-interface

Table of Content

What is a Dependency injection?

In Dependency injection, there is no need to create a separate interface for all native platforms. This interface can give to a container. In Dependency injection, we can move to the creation and binding of the dependent object outside of the class. At runtime injecting dependency into an object that another class is responsible for it.

In Xamarin there is one framework are available for implementing an automatic dependency injection and it’s called IoC Container. It will manage the object’s creation and injects dependency into the class. Dependency injection is a version of IoC(Inversion of Control). The Xamarin application can use the Model-View-View Model (MVVM) pattern.

There are three types of classes available in Dependency injection.

1)Client Class: The client class is also called the dependent class which is based on the service class

2)Service Class: The service class is also called dependency. The client class can get the service from the service class.

3)Injector Class: The use of the injector class is to inject the service class object into the client class.

The figure can show the relationship between these classes.

classes

Injector class can create an object of service class and, inject object to the client object. In this pattern responsibility of creation, an object of the service class of the client class can separate.

There are three ways to injector class inject a dependency.

Constructor Injection: The constructor injection provides dependency through the constructor.

Property Injection: This injection dependency can provide public property.

Method Injection: In this injection, dependency can provide a class method or an interface method.

Now we can see the example of Dependency Injection.

publicclassLoginViewModel :BaseViewModsel
{

privateIUserName _userName;

publicLoginViewModel(IUserNameuserName)
        {

            _userName = userName;

        }

    }

The LoginViewModel is a constructor that can receive an IUsername as an argument, injected by another class. The IUsername object is responsible for the class, and inserting into LoginViewModel class, it’s called dependency injection container.



Need for Dependency Injection

In Dependency Injection, we can reduce the line of code and achieve code re-usability. And also, well-maintained code is very important. Besides, it also improves User-friendliness for the app.

If one class does not depend on other classes that’s called Loosely Couple Code. In Loosely Couple Code have not group responsibility they have a single responsibility. One of the ways to achieve the Loosely Couple Code using the interface. By implementing its classes can communicate with the interface.

Now, we can see how to implement dependency injection in our Xamarin project.

Step-1:Create a new Xamarin project here DependencyInjection is our project name.

dependency_Injection

After the creation of the project just add some folders like Model, View, ViewModel, Interface, and service. These all folders we can create for our better understanding.

Step-2:Now we can implement one interface in the interface folder here IEmployeeData is our interface name.

usingSystem.Collections;

namespaceDependencyInjection.Interface
{
	publicinterfaceIEmployeeData
    {
		IEnumerableGetEmployee();
    }
}

And also add one namespace usingSystem.Collections; for Using the IEnumerable method.

Step-3 :Now, create a one model class in the model folder here Employee is our model name.

namespaceDependencyInjection.Model
{
publicclassEmployee
    {
		publicstringEmployeeName { get; set; }
		publicstring Technology { get; set; }
    }
}

Step-4 :Now, create one class in the service folder here EmployeeService is our service class name.

	usingDependencyInjection.Interface;
	usingDependencyInjection.Model;
	usingSystem.Collections;
	usingSystem.Collections.Generic;

	namespaceDependencyInjection.Service
	{
		publicclassEmployeeService : IEmployeeData
		{
			publicIEnumerableGetEmployee()
			{
				returnnew List
				{
					new Employee{ EmployeeName = "George", Technology = "Android" },
					new Employee{ EmployeeName = "Thomas", Technology = "MVC"},
					new Employee{ EmployeeName = "Steffan", Technology = "Angular"},
					new Employee{ EmployeeName = "Marco", Technology = "Xamarin"},
					new Employee{ EmployeeName = "Devin", Technology = "PHP"},
				};
			}
		}
	}

Here we can inherit interface class IEmployeeData.

Step-5 :Now, we can create a one view page for showing data here EmployeeDataViewPage is our view page name.
 


<contentpage.content>
<stacklayout>

<listview.itemtemplate>
<datatemplate>
<viewcell>
<stacklayout>
<label fontsize="Medium" text="{BindingEmployeeName}">
<label fontsize="Medium" text="{Binding Technology}">
</label></label></stacklayout><label fontsize="Medium" text="{BindingEmployeeName}"><label fontsize="Medium" text="{Binding Technology}">
</label></label></viewcell><label fontsize="Medium" text="{BindingEmployeeName}"><label fontsize="Medium" text="{Binding Technology}">
</label></label></datatemplate><label fontsize="Medium" text="{BindingEmployeeName}"><label fontsize="Medium" text="{Binding Technology}">
</label></label></listview.itemtemplate><label fontsize="Medium" text="{BindingEmployeeName}"><label fontsize="Medium" text="{Binding Technology}">

</label></label></stacklayout><label fontsize="Medium" text="{BindingEmployeeName}"><label fontsize="Medium" text="{Binding Technology}">
</label></label></contentpage.content><label fontsize="Medium" text="{BindingEmployeeName}"><label fontsize="Medium" text="{Binding Technology}">
</label></label>


One Stop Solution for Xamarin Mobile App Development

Your Search ends here.


injection-interface

Table of Content

What is a Dependency injection?

In Dependency injection, there is no need to create a separate interface for all native platforms. This interface can give to a container. In Dependency injection, we can move to the creation and binding of the dependent object outside of the class. At runtime injecting dependency into an object that another class is responsible for it.

In Xamarin there is one framework are available for implementing an automatic dependency injection and it’s called IoC Container. It will manage the object’s creation and injects dependency into the class. Dependency injection is a version of IoC(Inversion of Control). The Xamarin application can use the Model-View-View Model (MVVM) pattern.

There are three types of classes available in Dependency injection.

1)Client Class: The client class is also called the dependent class which is based on the service class

2)Service Class: The service class is also called dependency. The client class can get the service from the service class.

3)Injector Class: The use of the injector class is to inject the service class object into the client class.

The figure can show the relationship between these classes.

classes

Injector class can create an object of service class and, inject object to the client object. In this pattern responsibility of creation, an object of the service class of the client class can separate.

There are three ways to injector class inject a dependency.

Constructor Injection: The constructor injection provides dependency through the constructor.

Property Injection: This injection dependency can provide public property.

Method Injection: In this injection, dependency can provide a class method or an interface method.

Now we can see the example of Dependency Injection.

publicclassLoginViewModel :BaseViewModsel
{

privateIUserName _userName;

publicLoginViewModel(IUserNameuserName)
        {

            _userName = userName;

        }

    }

The LoginViewModel is a constructor that can receive an IUsername as an argument, injected by another class. The IUsername object is responsible for the class, and inserting into LoginViewModel class, it’s called dependency injection container.



Need for Dependency Injection

In Dependency Injection, we can reduce the line of code and achieve code re-usability. And also, well-maintained code is very important. Besides, it also improves User-friendliness for the app.

If one class does not depend on other classes that’s called Loosely Couple Code. In Loosely Couple Code have not group responsibility they have a single responsibility. One of the ways to achieve the Loosely Couple Code using the interface. By implementing its classes can communicate with the interface.

Now, we can see how to implement dependency injection in our Xamarin project.

Step-1:Create a new Xamarin project here DependencyInjection is our project name.

dependency_Injection

After the creation of the project just add some folders like Model, View, ViewModel, Interface, and service. These all folders we can create for our better understanding.

Step-2:Now we can implement one interface in the interface folder here IEmployeeData is our interface name.

usingSystem.Collections;

namespaceDependencyInjection.Interface
{
	publicinterfaceIEmployeeData
    {
		IEnumerableGetEmployee();
    }
}

And also add one namespace usingSystem.Collections; for Using the IEnumerable method.

Step-3 :Now, create a one model class in the model folder here Employee is our model name.

namespaceDependencyInjection.Model
{
publicclassEmployee
    {
		publicstringEmployeeName { get; set; }
		publicstring Technology { get; set; }
    }
}

Step-4 :Now, create one class in the service folder here EmployeeService is our service class name.

	usingDependencyInjection.Interface;
	usingDependencyInjection.Model;
	usingSystem.Collections;
	usingSystem.Collections.Generic;

	namespaceDependencyInjection.Service
	{
		publicclassEmployeeService : IEmployeeData
		{
			publicIEnumerableGetEmployee()
			{
				returnnew List
				{
					new Employee{ EmployeeName = "George", Technology = "Android" },
					new Employee{ EmployeeName = "Thomas", Technology = "MVC"},
					new Employee{ EmployeeName = "Steffan", Technology = "Angular"},
					new Employee{ EmployeeName = "Marco", Technology = "Xamarin"},
					new Employee{ EmployeeName = "Devin", Technology = "PHP"},
				};
			}
		}
	}

Here we can inherit interface class IEmployeeData.

Step-5 :Now, we can create a one view page for showing data here EmployeeDataViewPage is our view page name.
 


<contentpage.content>
<stacklayout>

<listview.itemtemplate>
<datatemplate>
<viewcell>
<stacklayout>
<label fontsize="Medium" text="{BindingEmployeeName}">
<label fontsize="Medium" text="{Binding Technology}">
</label></label></stacklayout><label fontsize="Medium" text="{BindingEmployeeName}"><label fontsize="Medium" text="{Binding Technology}">
</label></label></viewcell><label fontsize="Medium" text="{BindingEmployeeName}"><label fontsize="Medium" text="{Binding Technology}">
</label></label></datatemplate><label fontsize="Medium" text="{BindingEmployeeName}"><label fontsize="Medium" text="{Binding Technology}">
</label></label></listview.itemtemplate><label fontsize="Medium" text="{BindingEmployeeName}"><label fontsize="Medium" text="{Binding Technology}">

</label></label></stacklayout><label fontsize="Medium" text="{BindingEmployeeName}"><label fontsize="Medium" text="{Binding Technology}">
</label></label></contentpage.content><label fontsize="Medium" text="{BindingEmployeeName}"><label fontsize="Medium" text="{Binding Technology}">
</label></label>


One Stop Solution for Xamarin Mobile App Development

Your Search ends here.