Monday, 4 February 2019

Xamarin Forms Dependency Injection



Calling native platform code in your portable class library (PCL) is achievable via Dependency Injection. 

It’s a common question for people starting out, who are using a PCL or .NET Standard Library for developing their Xamarin apps. Dependency Injection involves creating an interface that can be commonly applied across all native platforms, then coding the actual implementation, or the native platform code, on each platform. 



This interface and instance is then given to a container. The PCL or .NET Standard Library, then goes to the container with only the interface known, to retrieve the instance. It doesn’t know where the instance came from, just that it has a specific interface.
Wikipedia definition “ - In software engineering, dependency injection is a technique whereby one object (or static method) supplies the dependencies of another object. A dependency is an object that can be used (a service).
Before understanding what it means in programming, let’s first see what it means in general as it will help us understand the concept better.
Dependency or dependent means relying on something for support. Like if, I say we are relying too much on mobile phones than it means we are dependent on them.
So before getting to dependency injections, first let’s understand what a dependency in programming means.
When class A uses some functionality of class B, then its said that class A has a dependency of class B.



In C#, before we can use methods of other classes, we first need to create the object of that class (i.e. class A needs to create an instance of class B).


So, transferring the task of creating the object to someone else and directly using the dependency is called dependency injection.

Why should I use dependency injection?

Let’s say we have a car class, which contains various objects such as wheels, engine, etc.
class Car{


private Wheels wheel = new MRFWheels();


private Battery battery = new ExcideBattery();


...


...

}
Here the car class is responsible for creating all the dependency objects. Now, what if we decide to ditch MRFWheels in the future and want to use Yokohama Wheels?
We will need to recreate the car object with a new Yokohama dependency. But when using dependency injection (DI), we can change the Wheels at runtime (because dependencies can be injected at runtime rather than at compile time).
You can think of DI as the middleman in our code who does all the work of creating the preferred wheels object and providing it to the Car class.
It makes our Car class independent from creating the objects of Wheels, Battery, etc.

There are basically three types of dependency injection:

  1. constructor injection: the dependencies are provided through a class constructor.
  2. setter injection: the client exposes a setter method that the injector uses to inject the dependency.
  3. interface injection: the dependency provides an injector method that will inject the dependency into any client passed to it. Clients must implement an interface that exposes a setter method that accepts the dependency.we will learn this in detail below using example of Xamarin forms applications.
class Car
{


private Wheels wheel;


private Battery battery;





/*Somewhere in our codebase we instatiate the objects required by this class.


There are two methods for implementing DI:


1. Constructor based


2. Setter based


*/





// Constructor Based


Car(Wheel wh, Battery bt) {


this.wh = wh;


this.bt = bt;


}





// Setter Based


void setWheel(Wheel wh){


this.wh = wh;


}


...


...


// Rest of code

}
So now its the dependency injection’s responsibility to:
  1. Create the objects
  2. Know which classes require those objects
  3. And provide them all those objects
If there is any change in objects, then DI looks into it and it should not concern the class using those objects. This way if the objects change in the future, then its DI’s responsibility to provide the appropriate objects to the class.


Interface

The first thing we must do to enable dependency injection is define an interface. For this example, lets say we have to get a unique identifier for the mobile phone. There are specific calls you can make on a each platform to get this, but its not available in our PCL. Hence we would define this interface to return a string of unique identifier.
public interface IDeviceInfo
{
    string GetUniqueIdentifier();
}
This interface would be placed in your PCL and hence accessible from your PCL and each platform project.

Implementation

Next we need to actually define the implementation of this interface in each platform.

iOS

public class DeviceInfo : IDeviceInfo
{
    public string GetUniqueIdentifier()
    {
        return UIDevice.CurrentDevice.IdentifierForVendor.AsString();
    }
}

Android

public class DeviceInfo : IDeviceInfo
{
    public string GetUniqueIdentifier()
    {
        return Android.Provider.Settings.Secure.GetString(Xamarin.Forms.Forms.Context.ContentResolver,
                                                          Android.Provider.Settings.Secure.AndroidId);
    }
}

UWP

public class DeviceInfo: IDeviceInfo
{
    public string GetUniqueIdentifier()
    {
        var token = HardwareIdentification.GetPackageSpecificToken(null);
        var hardwareId = token.Id;
        var dataReader = Windows.Storage.Streams.DataReader.FromBuffer(hardwareId);
 
        byte[] bytes = new byte[hardwareId.Length];
        dataReader.ReadBytes(bytes);
 
        return BitConverter.ToString(bytes);
    }
}

Dependency Injection Container Registration

Next we want to register this implementation with the Dependency Injection Framework. For this example, I will just use Xamarin Forms inbuilt dependency injection container. Place the following above each implementation of the DeviceInfo, above the namespace declaration.
[assembly: Xamarin.Forms.Dependency(typeof(DeviceInfo))]
namespace Mobile.Platform
{
   ...
You can also register it via a line of code, and this is actually need for UWP, as the assembly registration has issues with UWP under Native Compilation.
Xamarin.Forms.DependencyService.Register<DeviceInfo>();

Retrieve Dependency

Lets now retrieve that dependency in code. All we need to do is call this line, referencing the interface we want the implementation of.
DependencyService.Get<IDeviceInfo>().GetUniqueIdentifier();
Now we can call the GetUniqueIdentifier and it will return a string. The PCL or .NET Standard Library doesn’t know anything about the platform, and just interact with the instance via the interface definition.

AutoFac

The Xamarin Forms Dependency Service is a nice simple dependency injection framework, but it lacks many features, including the ability to do constructor injection. Using something more advanced such as AutoFac offers you many more features. There are many other Dependency Injection Frameworks out there, and they all provide very similar functionality.
To create the container, implement the following code.
ContainerBuilder _builder = new ContainerBuilder();
Now you can add dependencies as needed.
_builder.RegisterInstance<IDeviceInfo>(new DeviceInfo()).SingleInstance();
Finally you need to call the Build function to finish the building of the container.
_builder.Build();
The Container should be accessible in the PCL and native platforms, as is the interface. Similar to Xamarin Forms implementation, you can also get the

Constructor Injection

The benefits of using a Dependency Injection Framework such as AutoFac, is you can inject dependencies into a constructor. For example.
public class MyViewModel
{
    private readonly IDeviceInfo _deviceInfo;
    public MyViewModel(IDeviceInfo deviceInfo)
    {
        _deviceInfo = deviceInfo;
    }
}
If you had registered your ViewModel and DeviceInfo, in AutoFac, when AutoFac creates an instance of your ViewModel, it will automatically detect constructors with parameters and try to fill them in as needed. This has immense benefits for readability and unit testing. No longer is a dependency in your class, hidden via a line of code, its clearly apparent, as you can only create the class, if you inject the dependency into it.

Other Frameworks

You might be wondering what other frameworks are out there. First they are some MVVM frameworks that come with one included such as FreshMVVM and MVVMLight. If you prefer to keep your MVVM frameworks and Dependency Injection Framework separate, you can also use Ninject, TinyIoc, Unity, Castle Project, StructureMap and Spring.NET, just to name a few. You can do some research to find the one that best meets your needs. Personally, I prefer AutoFac, as its recent speed improvements, along with powerful interface, features and maturity make it great to work with.

Inversion of control —the concept behind DI

This states that a class should not configure its dependencies statically but should be configured by some other class from outside.

Benefits of using DI

  1. Helps in Unit testing.
  2. Boiler plate code is reduced, as initializing of dependencies is done by the injector component.
  3. Extending the application becomes easier.
  4. Helps to enable loose coupling, which is important in application programming.

Disadvantages of DI

  1. It’s a bit complex to learn, and if overused can lead to management issues and other problems.
  2. Many compile time errors are pushed to run-time.
  3. Dependency injection frameworks are implemented with reflection or dynamic programming. This can hinder use of IDE automation, such as “find references”, “show call hierarchy” and safe refactoring.
You can implement dependency injection on your own (Pure Vanilla) or use third-party libraries or frameworks.

Friday, 1 February 2019

Android Activity Lifecycle

When a user navigates through an Android App, a series of events occurs. For example, when a user launches an app, e.g., the Facebook App, it starts and becomes visible on the foreground to the user, onCreate() → onStart() → onResume().
If another activity starts, e.g., a phone call comes in, then the Facebook app will go to the background and the call comes to the foreground. We now have two processes running.
onPause()  --- > onStop()
When the phone call ends, the Facebook app returns to the foreground. Three methods are called.
onRestart() --- > onStart() --- > onResume()



There are 7 lifecycle processes in an Android activity. They include −
  • onCreate − It is called when the activity is first created.
  • onStart − It is called when the activity starts and becomes visible to the user.
  • onResume − It is called when the activity starts interacting with the user. User input takes place at this stage.
  • onPause − It is called when the activity runs in the background but has not yet been killed.
  • onStop − It is called when the activity is no longer visible to the user.
  • onRestart − It is called after the activity has stopped, before starting again. It is normally called when a user goes back to a previous activity that had been stopped.
  • onDestroy − This is the final call before the activity is removed from the memory.


Xamarin ,Native & PhoneGap Difference

Xamarin Comparison with other Cross platform


Xamarin is built for C# coding, so typically attracts fans who appreciate the power of C# on mobile devices. They also check the ability to leverage Visual Studio and the complete lack of JavaScript as pros.Xamain performance is better than other two.

 

Ionic is most popular with Angular developers, who list rapid prototyping and its ability to reuse front end Angular code on mobile as pros.
Phone gap is backed by Adobe; developers call it easy and developer friendly and appreciate that it’s not bound to a specific framework.

Feature Xamarin Native PhoneGap
Code Portability Xamarin developer worked on single PCL project and share among the all platform for development Native Apps developer worked on single platform only.So developer need to worked each platform individually. Phonegap application also developed on the single codebase and that shared across different platform. But, need configuration to work on different platform.
Development Cost Development cost is less as compare to native development. Because single developer can work for the different platform. Development cost is high for the native application. Since, we have to employ developer for each platform to work on. Phonegap application needs a designer and developer for the development.So, its cost far less than the native and more than Xamarin development.
Development Time Development time is far less than the native development in Xamarin. Since, single codebase shared among different platform. Development time for the native application is high as development time vary for different platform. Development cost is less than the native application and more or equal to Xamarin development.
Development Language Development language remain same the each platform. For each Platform, development language differ. Here development language remain same. But, developer needs to work in collaboration with designer for development.
Maintenance Maintenace cost is far less than the native as a single developer can look after the enhancement or maintenance. Maintenance cost for the native is high as we have have deal with different developer for each platform. Maintenance cost is less than the native application. but, it cost more than the xamarin as we have to employ designer and developer for the work.
Code Re-usability Since single codebase is shared among different platform. So, single codebase can work for different platform. No Code Reusability cannot be accomplish in the native application Phonegap application also share the codebase for different platform.
Distribution App store Application distribution is done from App store App Store
Offline Mode Xamarin application also support offline mode through caching. Offline mode is supported in native application Offline Mode is not supported in phonegap application.
Cross Platform Support Yes No Yes
User Interface User interface is more or less same as the native application. Rich user interface is accomplish in native application. Web development technology is employed for development, so, a mobile end Rich UI can't be met in phoneGap.
Device Level Access Device level access is less than the native application. But, it is far more better than the other cross platform development. Device level access is high, as development is done natively for each platform. Device level access is far less than native application.

Conclusion

Xamarin is the best platform to develop the hybrid application. Xamarin is more focus on code sharing, easy to manage the user interface, time saving and cheapest as compared to other cross-platform technology and also Xamarin provides more benefits compare to other hybrid technology like Code re-usability, Maintainability, test-ability etc. That’s why we can say that Xamarin is best Cross-platform technology to develop the hybrid application.

Complete Guide: Building a Live Cricket Streaming App for 100M Users

Comprehensive guide to building a scalable live cricket streaming platform for 100M users, covering backend infrastructure, streaming techno...