Learn how to use the Flux design pattern with Xamarin.
Source code: https://github.com/SuavePirate/Xamarin.Flux
Learn how to use the Flux design pattern with Xamarin.
Source code: https://github.com/SuavePirate/Xamarin.Flux
Learn how to use 4 methods to call platform specific code from shared code in Xamarin. Make calls to the HockeyApp iOS SDK from a Portable Class Library.
Source Code: https://github.com/SuavePirate/Xamarin.HockeyApp.Portable
Using all 4 methods to call platform specific code from shared code in Xamarin. Make calls to the HockeyApp iOS SDK from a Portable Class Library.
Source Code: https://github.com/SuavePirate/Xamarin.HockeyApp.Portable
Using Dependency Injection to call platform specific code from shared code in Xamarin. Make calls to the HockeyApp iOS SDK from a Portable Class Library.
Source Code: https://github.com/SuavePirate/Xamarin.HockeyApp.Portable
Using the Service Locator anti-pattern to call platform specific code from shared code in Xamarin. Make calls to the HockeyApp iOS SDK from a Portable Class Library.
Source Code: https://github.com/SuavePirate/Xamarin.HockeyApp.Portable
Using the Xamarin.Forms DependencyService class to call platform specific code from shared code in Xamarin. Make calls to the HockeyApp iOS SDK from a Portable Class Library.
Source Code: https://github.com/SuavePirate/Xamarin.HockeyApp.Portable
Does your app have sensitive information that belongs to your user? If so, you’re probably taking some action to protect it. Storing it with encryption, locking it behind a passcode, using TouchID, clearing their session when they leave the app, etc.
One thing you might not have considered is a vulnerability when using the app switcher. Could someone take your user’s phone and view the sensitive information by just double tapping the home button?
Let’s protect that data. We’re going to put a blurred view over the app whenever the user leaves (or even just hits the app switcher right away), plus it can look pretty cool!
In our AppDelegate.cs, override the OnResignActivation method:
public override void OnResignActivation(UIApplication uiApplication)
{
var window = UIApplication.SharedApplication.KeyWindow;
var blurView = UIBlurEffect.FromStyle(UIBlurEffectStyle.Light);
var blurEffectView = new UIVisualEffectView(blurView);
blurEffectView.Frame = window.Frame;
blurEffectView.Tag = 808080;
window?.AddSubview(blurEffectView);
base.OnResignActivation(uiApplication);
}
This will add our blurred view whenever they leave. Now to remove it when they come back, override the OnActivated method:
public override void OnActivated(UIApplication uiApplication)
{
var window = UIApplication.SharedApplication.KeyWindow;
window?.ViewWithTag(808080)?.RemoveFromSuperview();
base.OnActivated(uiApplication);
}
And that’s it!
Bonus swift version: Override applicationWillResignActive and applicationDidBecomeActive in your AppDelegate.swift.
func applicationWillResignActive(application: UIApplication) {
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Dark)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = window!.frame
blurEffectView.tag = 808080
self.window?.addSubview(blurEffectView)
}
func applicationDidBecomeActive (application: UIApplication) {
self.window?.viewWithTag(808080)?.removeFromSuperview()
}
For those who just want code: https://github.com/SuavePirate/Xamarin.Onion
Don’t forget part 1 on the general project structure: Onionizing Xamarin Part 1
A strong and scale-able architecture is important in applications, especially in Mobile Apps. APIs and SDKs are constantly changing, new technology is constantly released, and team sizes are always changing. A solid Onion Architecture can save a development team a lot of time by making it simple to change service implementations, restrict access to certain areas, making logic flow easy to follow, and making testing isolated blocks of code easier.
Some of the important topics this will cover:
In this section, we’ll start to dive into the code for our definition layers (or at least what is important).
Let’s get into the Domain and Application layers:
As said before, this is where our core models are, so let’s take one as an example:
User.cs
public class User
{
public int Id { get; set; }
public string Email { get; set; }
public string FullName { get; set; }
public string PasswordHash { get; set; }
}
We’ll focus on our one model, but you could grow your entities out here.
This is where we define our data access layer for consuming our Domain.Models; Stores, DataProviders, and Repositories. These are good places to set up generic definitions so that multiple implementations can be made more easily. Here are examples of each:
IGenericStore.cs and IUserStore.cs
public interface IGenericStore<T>
{
List<T> Data { get; set; }
}
public interface IUserStore : IGenericStore<User>
{
}
If you were to need to define custom methods for the user, you can do that in your IUserStore
Another common practice is to add a manager wrapper for your stores, to make it easier to inject the use of multiple stores in our business layer in the future. In this example, it would look something like this:
IStoreManager.cs
public interface IStoreManager
{
IUserStore UserStore { get; }
IGenericStore<T> Set<T>();
}
IGenericRepository.cs and IUserRepository.cs
public interface IGenericRepository<T>
{
void Add(T entity);
void AddRange(IEnumerable<T> entities);
void Remove(T entity);
void RemoveRange(T entities);
Task<T> FindAsync(Func<T, bool> predicate);
Task<IEnumerable<T>> GetAsync(Func<T, bool> predicate);
Task CommitAsync();
}
public interface IUserRepository : IGenericRepository<User>
{
}
Just as with the Stores, you can define entity specific methods / queries in your specific repository (IUserRepository).
Now that we are through our data definition layers, let’s take a look at the application definition layers, starting with Application.Models. This is where our business models live – our Data Transfer Object Models, Input Models, Output Models, etc. So here is how our Domain.Models.User maps to each of these types:
UserTransferObject.cs
public class UserTransferObject
{
public int Id { get; set; }
public string Email { get; set; }
public string FullName { get; set; }
public UserTransferObject()
{
}
public UserTransferObject(User entity)
{
Id = entity.Id;
Email = entity.Email;
FullName = entity.FullName;
}
}
Note that we have added a constructor that also consumes a Domain.Models.User type. This is completely optional. Many people do not want the Application.Models layer to reference any other layer. Another common way to handle the mapping is via an extension class in the Infrastructure.Business layer, like so:
public static class UserExtensions
{
public static UserTransferObject ToDTO(this User entity)
{
return new UserTransferObject
{
Id = entity.Id;
Email = entity.Email;
FullName = entity.FullName;
}
}
}
The important thing to note in all of this, is that the DTO has properties mapped from the entity that are relevant and SAFE to the application. Notice the PasswordHash field was omitted.
NewUser.cs
public class NewUser
{
public string Email { get; set; }
public string FullName { get; set; }
public string NewPassword { get; set; }
}
This is one of our input models for creating a new User. Notice that it only has the properties required for creating one.
Last but not least, our output. This example uses a generic output Result that holds data from a DTO, errors, and the type of result. The output models you use will depend on the services you’re using, so this is not a catch-all.
Result.cs and ResultType.cs
public class Result<T>
{
public ResultType Type { get; set; }
public IEnumerable<string> Errors { get; set; }
public T Data { get; set; }
public Result(T data)
{
Data = data;
Type = ResultType.Ok;
Errors = new List<string>();
}
public Result(ResultType type, IEnumerable<string> errors)
{
Type = type;
Errors = errors;
}
public Result(ResultType type, string error)
{
Type = type;
Errors = new List<string> { error };
}
}
public enum ResultType
{
Ok,
BadRequest,
Failed,
Unauthorized,
Forbidden,
Invalid
}
Now we have our models, let’s define our business layer.
These are the definitions of our business logic that use our Application.Model layer. We’ll use services here, and like our data definitions, will utilize generic definitions where possible.
IBaseService.cs and IUserService.cs
public interface IBaseService
{
IEnumerable<string> Validate(object model);
}
public interface IUserService : IBaseService
{
Task<Result<UserTransferObject>> CreateUserAsync(NewUser model);
Task<Result<UserTransferObject>> FindByIdAsync(int userId);
Task<Result<UserTransferObject>> RemoveByIdAsync(int userId);
Task<Result<IEnumerable<UserTransferObject>>> GetValidUsers();
}
Notice that each of our consumes either a primitive type, or an input model from our Application.Models and outputs one of our output models.
That’s all there is for the different definition layers. In the next post, we’ll look at implementing these two layers in our Infrastructure.Data and Infrastructure.Business layers. From there, we can look at our actual Xamarin code for consuming these layers and mapping them all together.
Once we’ve gone over all our layers, we will look into replacing different pieces, building tests, and areas where you can add your own flare.
For those who just want code: https://github.com/SuavePirate/Xamarin.Onion
A strong and scale-able architecture is important in applications, especially in Mobile Apps. APIs and SDKs are constantly changing, new technology is constantly released, and team sizes are always changing. A solid Onion Architecture can save a development team a lot of time by making it simple to change service implementations, restrict access to certain areas, making logic flow easy to follow, and making testing isolated blocks of code easier.
Some of the important topics this will cover:
This first post will talk about the general project structure and high level role of each layer in the solution. Later posts will touch on the individual projects’ code, why things are where they are, using the structure to build out tests, and ways to bend or change the structure to work for you.
For this example we will be talking about Xamarin.Forms, but the same patterns can be applied in the exact same way without it.
Lets just take a look at the high level structure and layers of the solution:

This is the lowest level. Projects within Domain should not reference any projects outside of this layer and should also avoid referencing any external libraries.
These are our base data models. This project shouldn’t reference any other projects, even within Domain.
These are the definitions for our data access layer. Repositories, Stores, etc. There should be no implementation in this project and should only reference the Model project.
This layer is what defines our Services and Business logic without implementing any of it. Projects in this layer can only reference Model layers of Domain.
These are our application models such as input models, data transfer objects, as well as any helpers for mapping Domain models to these. This project will only reference the Domain.Models to help map them to DTOs or other models. This, however, is also optional. You could opt to handle mapping externally when the models are needed in business logic rather than in the Application layer.
These are the definitions for our business logic layer. Services, Managers, etc. There should be no implementation in this project and it should only reference the Application.Models project
This is where we implement our data and business logic.
This is the implementation of our Data access layer. Communicate with 3rd party data providers, local storage, databases, etc. Domain.Interfaces should be implemented here.
This is the implementation of our Business logic layer. Communicate with the data layer through contracts from the Domain.Interfaces. This project should NOT reference the Data project. Application.Interfaces should be implemented here
This is where we implement client logic, set up IoC, create ViewModels, and controls. If we are using Xamarin.Forms, this is where the PCL or Shared Library with Xamarin.Forms is. This Layer can reference any of the above layers in order to wire up IoC. If you have multiple Xamarin.Forms projects in one solution, they can both be here.
This is where reusable controls within Xamarin.Forms exist. Pretty straightforward.
This is the section where the Native Projects live. If you have too many native projects for things like wearables, or the different TV OS’s, then it might make sense to break this section into smaller sections for things like “Apple”, “Google”, “Windows”, or something similar. But for the sake of this demo, we are only working with one project for each platform, so they live together.
This layer should only reference the Client layer and Binding Layer
This is the Xamarin.Android project. If any native services need to be called from one of the shared layers, the IoC set up can be extended into this project and one of the interfaces could be implemented here and registered.
This is the Xamarin.iOS project. As stated above, native services can be set up and injected from here as well.
This is the UWP project. As stated above, native services can be set up and injected from here as well.
This is where Xamarin Binding projects should be if there is a need for binding to any native libraries. As with the Platforms layer, if there are many different binding projects, this layer could be split into different sections for each OS. This layer should be exclusive and not reference any other layer.
This is where UI and Unit tests are. This layer can reference any other level in order to test them. This layer can wire up a completely different IoC container, contain mock projects for simulating any other layer, or any other external reference.