Using DependencyService in Xamarin.Forms to Write Platform-Specific Code

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we're going to learn all about the dependency service in xamarin forms so the dependency service is a dependency injection container that is built in right into xamarin forms um it is kind of limited because mostly its use case is to just you know allow you to reach platform-specific code so ios specific code android-specific code on your shared code layer which is pretty cool we're going to see that in our video but for other dependency injection scenarios it might be a little bit limited so if you don't know about dependency injection you might want to check out my other video dependency injection for absolute beginners it should pop up on your screen right now if you don't know what dependency injection is go check that out first but for anyone else come right through we're going to learn about dependency service in xamarin forms [Music] so here we are in a file new xamarin forms project on the left you can see it running in visual studio for mac you can see all the example that is needed for showing our page on the right that is running in a ios simulator so let's update our little title here first and make it the dependency [Music] service sample and if i save this then with hot reload you can automatically see the ios simulator updating so as long as you only make changes in xaml you can see this updating almost instantly on the ios simulator android emulator or even on your physical devices so that is very very cool i will implement a little bit of ui later on but for now let's just dive straight into the dependency service that's what we're here for so let's go into our main page code behind and i'm just going to go to a arbitrary place here just to show you the dependency service right here so the dependency service is kind of like your dependency injection container that is built into xamarin forms and it is specifically i think for you know reaching that platform specific code in your shared code what that exactly means we will see in a minute if you don't know what a dependency injection container is please go back to my dependency injection for absolute beginners video it should pop up on your screen right now so go through that one first hopefully it will become more clear for the rest i'm going to assume that you know a little bit about dependency injection already and here we can see the dependency service so the dependency service in this case is just a static class that is built into xamarin forms with a couple of methods here is the get which allows us to get a platform specific implementation of the type that we specify so what that exactly means we will see in a little bit we can register a implementation so we can register a concrete class or you know something that is implemented by a interface so we can specify this is the interface and this is the concrete implementation and it will try to create that concrete implementation whenever we ask for a interface so you know then we have that separation of concerns and we can better reuse our classes whenever we just let it implement this interface we can register a singleton so there we can influence the lifetime of our objects so it's good to know by default whenever you do a get it will be returned as a singleton so the first time you do a get on a certain class and it's not available yet it will make a new implementation and by it i mean the dependency service it will make a new instance of that concrete implementation return that to you and each time when you subsequently call that get you will get that same instance back to you so there will always be at most one instance of that object in the lifetime of your app so you can influence that um we can also get like if you go back to the get we have an overload where you can specify the dependency fetch target so this is the global instance which is like the default behavior which will give you that global instance that that singleton basically or you can go to the new instance where you will get a new instance each time but beware that you will have to take care of the lifetime of that object so if you're going to create a hundred new instances they are not going to be cleaned up for you that is something that you will have to do yourself typically this is not something that you need so just use the default behavior unless you have a very good reason to not do that so that's the register singleton it has a very specific method to do that as well and we have the resolve to be perfectly honest i don't know exactly what the difference is between the get and resolve so if you do know please let me know in the comments or if you want me to find out then also let me know in the comments and i will dive into it for you and let you know but get and resolve mostly i think do the same thing but you know so there's two methods for it so there has to be some kind of difference right but there we go that is what the dependency service can do for you now let's whip up a little small example for you to get that point across so here in my shared code i'm going to go to our solution the shared project right here i'm going to add a new file which is going to be a interface the i device info service here we go so the naming is totally up to you some find this more of like a service kind of pattern some just use the concrete implementation class to just you know idevice info and then you have the device info it's totally up to you what you think is better i'm going to go with the service paradigm here so empty interface idevice info service here we go and we have a public interface so that's very cool and an interface as you know is just a contract it just sums up the methods that are available inside of a class and it doesn't care about the implementation which is going to be very useful for the xamarin forms paradigm right because we just want to know which methods are here and from here we don't care how it's implemented and that comes in very handy because if we're running on ios the implementation is very different from when we're running on android right because the code is just completely different the namespaces are different we'll see that in a little bit and this is also the reason why you typically have to install xamarin forms plug-ins new gets onto all of your projects because basically the nuget that you install on your shared project will just give you this kind of interface class and the nugets that you install on your platform project will have the actual concrete implementation code that is used to actually execute the functionality that you're after so that is why you have to install all of those plugins on all of your projects that you're using so see now you understand a little bit about the background here here in this interface we're going to say okay string get device info so this will whoops not info actually models we are going to return the model of the device that we are running on and as long as you you know return a type that is understood in your shared project so it can be simple type strings ins whatever but it can definitely also be more complex types as long as they live in like your shared project or something that is referenced by your shared project then you can do that as well and you return that from your platform project you know in that same kind of way that is understood by your shared project you can do anything you want so that is really cool i realize this sounds a little bit abstract so hopefully it will become clear but this is definitely one of those things that you probably have to try out a couple of times yourself so you know how this all fits together so please do that also the official microsoft docs pages are linked in the video description so go check them out as well now let's go into our platform specific project so first to android i'm going to show you this on ios but to be complete let's do it on android as well i'm going to add a new class and i'm going to say device info service again you might want to add the platform name in here that is something that people like to do it's totally up to you you don't have to do that it's not bound to any naming scheme or whatever it's just whatever is clears to you and fits your strategy and is the most clear to you and your team so i'm just going to keep it as device info service here we go do new and of course this has to implement the i device info service so here we go we don't need this constructor because that is generated for us and i'm going to hoover over this interface name and go to say implement interface and it's going to give us a method that is public string getdevicemodel just as we've defined but it's going to throw a not implemented exception right now so this is the thing that we need to implement now i know for android this is going to be android.os.build.model this is the way to return the device model on android and you can see immediately this is very specific to android because this is an android namespace android object and and it will return the model so this is really what it's all about with the dependency servers basically because you know we're reaching into the platform specific code and we're basically making that available into our shared code so we're just making a small step to our platform specific ones xamarin forms already provide you with a lot of cross-platform stuff they also do it like with this same mechanism but you know there are things that are not provided by xamarin forms and you can make it very easily accessible on that shared layer yourself by going through this with the dependency service so this is how to do it on android let's also create our implementation on ios and then i'll circle back to how to use that with the dependency service so here we're going to do device info service again the name can be the same because we are in a different project again don't need the constructor and we're going to say i device info service let it generate it for us again but on ios this time i know it's going to have to return the ui kit dot ui device dot shared no not the shared dot device current device there we go dot model so there we go this is how a model is returned on ios and in this case you know it's it's simple string so that should be easy but if you want to return a more complex type instead of a string you can totally do that just say return new complex type fill it with all the properties that you need and you can take that back to your shared project and it will understand that perfectly so that is really cool but it also works for simple strings as we're going to do now so if we go back actually let me go back to the main page here and show you the dependency service methods again so the thing that we still want to do with like our device info services is um register them with the dependency service right so the way to do this normally would be like register and we can say we have an overload of this so we could say i device info service and here we would say device info service see here is where you get in trouble if you choose the same names and this would basically register like a concrete implementation with this interface right so whenever we ask for this interface it will know to return this actual concrete implementation now in this case it doesn't know the implementation because the reference is going from the shared project to well from the platform project to our shared project and not the other way around because we would have circular reference things so it doesn't know about the actual classes in our platform projects and that is exactly the problem that examiner informs team had to solve but this is under the covers what is going to be called whenever i'm going to call the code that i'm going to show you right now so just remember this nothing magic is going to happen so this is just what is going to be called so let's see um stop talking in abstracts here and let's see what this is actually about so if i go to a platform specific implementation here we're going to add a attribute here and this is the way that you're going to find on the internet a lot when you're searching for this and we're going to say assembly xamarin.forms.dependency here we go so these attributes attributes are usually just shortcuts for executing some code if you go look in the xamarin forms source then you can go to this dependency attribute and you can see that it probably calls that register that i've just shown you so here you can just say dependency and we will have to specify only the type of the thing that we want to register the concrete type so we're going to say type of there we go and we're going to say device info service there we go we have to add the right using here so do that and now with this it's going to call the dependency service dot register with this concrete class and because this class implements the idevice info service it can implement multiple interfaces and it will just register that for you as well but now it knows whenever we ask for like the i device info service it can give us back this concrete class and it has to execute this code whenever we call the getdevicemodel method so does that still make sense it will hopefully become clear in a little bit so we have this one this does the register and we also have to do that for android to make sure it works there as well also add the right using here but now the one in android so there we go and to make this all visible i'm going to go back to our main page right here remove all the labels we don't need that and i'm going to just going to keep one label right here which is going to be the x name is device model label there we go actually remove all this text here don't want to confuse the user so there we go device model label go into our code behind and we are going to say text is so here's where we want to show our model right so now we need to get it from our dependency service so i'm going to save our device info service is dependency service dot get you can also do dot resolve as i mentioned but we're going to do dot get and here we're going to say i device info service so here we go nothing fancy going on and with this we're getting our concrete implementation of the idevice info service now the cool thing is of course we're only running one platform on you know a device per time so um if we run on ios it will register the concrete implementation for ios for us and on android it will automatically register the concrete implementation on android for us so you know there's only one registry made at a time but of course you can also implement some code to you know do some registration yourself and depending on some logic maybe you're doing some a b testing and you want to at the one time do the one implementation and then the other time do the other so we have to make sure that a implementation is ready for us right so we want to check if device info service not is no of course you can also skip this but just to show you that it can be null and whenever you're going to call the next thing then it will crash with a no reference exception so you might want to implement some error handling make it more robust or you know if you think like hey i want my application to crash whenever it's not shown whenever it's not registered so i know that there's some work to do then of course you can do that too and what we can do now on our device info service we can call the device model we know that method is available on our interface we don't care how it's implemented but we just know it's there and we can call it and we will get a string back that we can now show into our label okay so i've changed mostly code so we're going to have to stop running our project here and i'm going to restart it again actually let's put in a breakpoint here and we can see what is going on so what is going to happen is whenever we run this code we're running on ios so it will go to our ios project with this attribute it will register this device info service as you know the implementation for our idevice info service and then whenever it comes back to our main page right here with the dependency service we are going to get a concrete implementation of the idevice info service uh we'll see through a break point that that is going to be the ios one and whenever we have that actual implementation we can call upon that with the get device model we're going to reach into that platform specific code and we are going to see the model of this device so step over this and you can see here that this device info service is of the type xf device service sample ios device info service so this is our ios implementation it's not null so if i just continue running here you can see it gives us the model which is an iphone in this case and that seems accurate so we've successfully registered something in our dependency service we've gotten that out of the dependency service and we have called a method that will give us the expected results which is very very cool now of course all the methods that i've just shown you um also allow you to do manual registrations in the dependency service so you can do some dependency injection with this as well i don't think unless you you know do it manually dependency injection like in the constructor and that kind of thing is not supported so you know it isn't smart enough to register a whole bunch of dependencies and then say i want to resolve a class that actually need a dependency in the constructor it doesn't see that and it doesn't allow you to do that so the dependency service is kind of limited in what it can do but in other videos if that's what you want to see let me know which framework you want to see with xamarin forms let me know in the comments and i'll see if i can make a video for that as well other frameworks are more mature and can do other fancy stuff that will even make it easier to write code and and not have to register and figure out all the things yourself but at least now you know how to use the dependency service there you have it that's how to use the dependency service in xamarin forms was it useful to you did you already know it was it a little bit clear because you know these things are just a little bit hard to explain i feel but you know with a piece of code hopefully some explanations i'm jumping around i'm showing you a bunch of things hopefully that got the point across if not please let me know in the comments i'll be glad to help you with any questions you might still have of course i've mentioned this a couple of times if you want to see other dependency injection frameworks or maybe other code scenarios things anything let me know in the comments and i'll be happy to create videos for you if you've liked this video please don't forget to give it a thumbs up somewhere around here if you've liked more on this channel or you're curious to see what's coming next subscribe to my channel and ding that bell to be notified automatically whenever new stuff comes available thank you for your support i'll be looking forward to making four videos for you and i'll see you for the next one
Info
Channel: Gerald Versluis
Views: 4,064
Rating: undefined out of 5
Keywords: xamarin, xamarin forms, xamarin.forms, xamarin tutorial, dependencyservice in xamarin forms, dependencyservice.get returns null android, xamarin dependency service example, dependency service xamarin forms, dependency service xamarin not working, xamarin forms 101, Xamarin.Forms 101, xamarin forms tutorial for beginners step by step, xamarin forms tutorial, xamarin forms tutorial for beginners, xamarin forms cross platform tutorial
Id: 7WfLqpL-i-o
Channel Id: undefined
Length: 19min 50sec (1190 seconds)
Published: Mon Dec 28 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.