Skip to content

Architecture

Poh Zhi-Ee edited this page Nov 3, 2018 · 15 revisions

This document serve to explain some stuff

Table of Contents

  1. IoC Container
  2. ViewModelLocator
  3. Logging
  4. Navigation Service
  5. DTO

IoC Container

The IoC container used in this project is AutoFac, selected due to ease of use, as well as good documentation and multiple examples of people using it on their Xamarin project.

To use an IoC container:

  1. Register types
  2. Specify lifetime
  3. Resolve

ViewModelLocator

The ViewModelLocator is to allow design time evaluation of the ViewModel while still allowing constructor injection of the ViewModel.

Sample usage

XAML:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="NottCS.Views.MenuPage"
             BindingContext="{Binding Source={StaticResource Locator}, Path=Menu}"
             Title="Menu">

ViewModelLocator class:

  1. Register the type with the container and specify the lifetime
builder.RegisterType<MenuViewModel>().InstancePerDependency();
  1. Expose the property to be binded
public MenuViewModel Menu => _container.Resolve<MenuViewModel>();

The constructor injection is done through the use of the IoC Container.


Logging

The logging is done through the use of NLog.

Main advantages:

  1. Can specify logging levels
  2. Easily include timestamp of log
  3. Easily format log string to include more information like callsite
  4. Easily change logging medium, i.e. Console, Text file, etc.

NLog setup found under App.xaml.cs under SetupNLog() function. (Updated as of 3 Nov 2018)

Sample usage:

private readonly ILogger<MainViewModel> _logger;
public MainViewModel(ILogger<MainViewModel> logger)
{
    _logger = logger;
    logger.LogInformation("MainViewModel created");
}

Navigation Service

Since this application has been updated to use a drawer style, there are 2 ways for navigation to happen. Page terminologies illustrated here

1. Replace the Detail page. This is needed when an item on the Drawer/Menu Page is selected.

Usage:

await _navigationService.SetDetailPageAsync(viewModelType, navigationParam);

2. Navigate from the current Detail page. This is needed when the Detail page itself needs to navigate further.

Usage:

await _navigationService.NavigateToAsync<ViewModelType>(navigationParam);

DTO

A DTO is basically a Data Transfer Object. Not to be confused with Model. These are basically dedicated for data transfer with the backend only, not to be used in anything else. Can refer to this,

Clone this wiki locally