Published April 30, 2017 by with 10 comments

Platform Specific Abstractions In Xamarin Forms

In this article, we will learn how to use abstractions to implement platform specific functionality in our xamarin forms shared code. Therefore, xamarin forms has some useful API s but it does not expose everything from the platform. In fact, it's very likely there'll be some feature that you need to use which is not available to your shared code for these cases we have to put a little architecture thought into our application. We want to use the API is available in the platform specific project but somehow invoke them from our shared code.



 As an example consider dialling the phone all three platforms have support for this feature but each one accomplishes it differently to fully control it we would want to write that code uniquely per platform. Let’s explore what xamarin forms has to offer here platform features not expose those xamarin forms can be used but will require some architectural design.



 First, you want to create some form of abstractions that might be an interface or base class that is something to represent the feature that you want to use in your shared code. The best practice is to build an abstraction implemented by the target platform which defines the platform specific functionality .Here we define an IDialer interface which describes how our code will dial the phone on each platform then in each of the platform specific projects we'll implement that interface using the platform specific API which is available to us in those projects. Our shared code will always use the interface which means it's not tied directly to the implementation or platform-specific code projects implement the shared Dialer interface using the platform specific api to locate the implementation. We can use a variety of techniques often it's either a variation of the service locator design pattern or the dependency injection design pattern. It really does not matter how you bind to the implementation the key thing is the separation being used here when creating your own abstractions. You can of course roll your own locator object or provide a singleton property that your platform-specific code assigns.

Alternatively, xamarin forms has two built-in mechanisms.



 The first is a generalized messaging service called Messaging Center that as a publish/subscribe event system. You can send messages from anywhere in your shared code and have some handler likely in each of the platform libraries receive the message and then process it in the platform specific way. This is exactly what page does when creating alerts and action sheets.
The second way is to use a dependency service API. This is specifically designed to locate and bind interfaces or abstract classes to implementations.


 When using the dependency service the first step is to define our abstraction. The IDialer interface in this case must be in our shared code since all parties need to know about this class or interface.




Next, we will implement the abstraction in each of our platform specific projects. Here we define it for iOS but we would also want to do an implementation in the Android and uwp projects. Notice that it does not need to be public, external users won't know about the class directly. They will only reference it by the abstraction.



Next in each of the platform, specific projects we let the dependency service know about our implementation using the dependency attribute. This is declared the assembly level as you can see here and identifies the implementation, which is then registered with the dependency service xamarin forms. You can also use a new register method to add specific instances or types to the dependency service and code vs. declaratively with an attribute. This allows you to make runtime decisions about the implementation of the service .Or to instantiate a service implementation with the parameterised constructor.




Finally, anywhere in your app you can request the abstraction from the dependency service. It will return the first registered class that implements the specific interface or derives from the specified base class .If no classes found null is returned this means that you can implement a service for one platform but leave it off for another if it's not possible to implement or it's optional. 
Read More
Published April 28, 2017 by with 9 comments

SQLite In Xamarin

Now that we know where to store a database on each of our platforms let's move on to integrating a SQLite database into our Project A SQLite is a lightweight local database and it's become the industry standard for mobile apps also it's open source .It does not run on a server and doesn't require any configuration . SQLite database is stored in a single file in the local file system with those reads and writes being performed against this file then obviously, SQLite is cross-platform making it an ideal choice to use across your Android iOS and Windows apps.

Now the SQLite native libraries are built into Android and iOS but they are not present in Windows and this means that if you are targeting any of the Windows platforms then it must be added to your application. Now we are going to use a nugget package, which includes a version of the SQLite runtime when it is added to a Windows application.

However, there is one extra step you will need to perform for Windows, which we will talk about in just a minute but first let's take a deeper look at SQLite now the SQLite engine exposes a C++ API, which is an access by dot net through a c-sharp wrapper.

We can then use a third-party library that provides a higher level c-sharp API to that SQLite database and there are several great options to choose from the SQLite mono libraries using a  dot net style which is fairly well known and relatively easy to work with .However as performance is not as strong as some of the other approaches it does work on iOS and Android but there's no official support for Windows Phone or uwp and generally it's not considered a modern approach and it's not preferred anymore there's also a Microsoft created lightweight library for SQLite and it's fairly new and it's the high-performance wrapper directly over the C++ API but there's minimal documentation it does support iOS Android and those phone doesn't support you WP and finally we have SQLite net and this is the most common approach being used today it's simple to use performance is strong supports all of our target platforms and it's very stable and very widely used and since this is the most common approach this will be the one we're going to focus on today


 but just keep in mind you have other options sequel a net is an ORM or object relational mapping and ORM simplify the process of defining database schemas by mapping model objects in your source code to tables in your database now the schema is ID through attributes and we'll discuss these in the next section

However, ORM is remove the need to write sequel statements and they allow us to interact their data using c-sharp expressions to use sequel like net.

We need to add a nugget package and you must add the components to each of your projects so this would include each platform specific project and anything else that need to access the SQLite database. There are several published implementations so make sure to install the version labelled sqlite - net PCL

Moreover, it should have a single author Frank Krueger and when you add the component, you will get two packages.

the sqlites-net PCL is the core assembly and this includes everything to identify the data entities as well as the high-level classes used to create query and work with those entities .Now a second component sequel like PCL raw that provides the raw c-sharp wrapper around that C++ API this is used by the core assembly to access the database and unless you need to talk to the underlying API you can generally ignore this component and just let it do its work and remember that SQLite native libraries are built into Android and iOS but they're not present in Windows .So in Windows the SQLite runtime is provided in that included raw nugget  package now that database engine is written in C++ and it's included as a binary and that binary has a dependency on the visual C++ runtime


and you'll need to add that reference and to do that you'll open the references go to extensions and select visual C++ 2013 runtime now currently you need to use a 2013 version even when later versions are available as this is what the SQLite pcl raw is compiled against and here we're showing Windows Phone 8.1 but this same requirement applies to Windows 8 and uwp and if you forget to add this reference you will get a type initialization error from SQLite being unable to load the runtime DLL


Read More