If you are a C# developer and need to build a line of business cross platform mobile application, Xamarin and Xamarin Forms should be your tools of choice. But if this is your first time, how do you know if you have made solid choices in your application architecture? In this session, you will receive guidance on using Xamarin and Xamarin Forms (open source platform for building native mobile applications) to create an application architecture that will support not only the delivery of your application but future application maintenance and feature additions. Topics discussed: MVVM (model - view - viewmodel), Dependency Injection, Component Communications, Validation, and Navigation.
Architectural Guidance for Building Mobile Apps Using Xamarin.Forms
1. Architectural Guidance for
Building Mobile Apps Using
Xamarin Forms
Richard Taylor
Vice President of Engineering
SentryOne
@rightincode – http://www.rightincode.com
2.
3. Who is Richard Taylor?
Full Stack Software Engineer
Lives in Huntersville, NC
Organizer of CLT Xamarin Developers
Lead Organizer of Modern Devs CLT
http://www.rightincode.com
@rightincode
Vice President of Engineering
Huntersville, NC
http://www.sentryone.com
@sentryone
@rightincode - http://www.rightincode.com
4. What are elements of a “good” software
architecture?
Separation of Concerns
Single Responsibility Principle
Loose Coupling
Don’t Repeat Yourself (DRY)
@rightincode - http://www.rightincode.com
5. How can we achieve a “good” software
architecture?
Use proven architectural styles
Onion Architecture
Use proven architectural/design patterns
MVVM (Model-View-ViewModel)
Inversion of Control
Dependency Injection
@rightincode - http://www.rightincode.com
6. Xamarin.Forms – Folder Structure
@rightincode - http://www.rightincode.com
Visual Studio Solution Explorer
Reference Project:
Tip Calculator
7. Xamarin.Forms Architectural Guidance –
MVVM
Design pattern that help facilitate the separation
of the business and presentation logic from an
applications user interface.
The why..
Separation of Concerns
Support unit testing of the view model/model
independently of the user interface
Easier maintenance
@rightincode - http://www.rightincode.com
8. Xamarin.Forms Architectural Guidance –
MVVM – cont.
View
Responsible for the user interface
View Model
Responsible for implementing of the properties and
commands that the view binds to
Notifies the view when state changes
Coordinates all required interactions between the
view and model
@rightincode - http://www.rightincode.com
11. Xamarin.Forms Architectural Guidance –
Dependency Injection
Enables the decoupling of concrete types from
the code that depends on the types
With DI, dependencies are provided by
external facilities at runtime
DI is a form of Inversion of Control
@rightincode - http://www.rightincode.com
12. Xamarin.Forms Architectural Guidance –
Dependency Injection – cont.
DI helps improve testability
DI does not require a container that houses
the mapping between interfaces and their
concrete types but DI containers are common
@rightincode - http://www.rightincode.com
13. Xamarin.Forms Architectural Guidance -
Validation
Applications implementing the mvvm design pattern
should perform validation in the view model and/or
model
ValidatableObject<T> approach…
Properties of the view model/model that require
validation should be of type ValidatableObject<T> that
has validation rules defined in its Validations property
Validation is done by calling the Validate method of
the ValidatableObject<T> which executes the
validation rules on its Value property
@rightincode - http://www.rightincode.com
14. Xamarin.Forms Architectural Guidance –
Validation – cont.
ValidatableObject<T> approach cont.
Errors are placed in the Error property of the
ValidatableObject<T>
The IsValid property of the ValidatableObject<T> is
updated to indicate if the validation succeeded or
failed
For example, the entry view in Xamarin.Forms can be
bound to the IsValid property and be notified if
entered data is valid after validation and update the
user interface accordingly
@rightincode - http://www.rightincode.com
15. Xamarin.Forms Architectural Guidance -
Navigation
Support for navigation is built in and is considered
view-first navigation
View-first navigation has some weaknesses
Tight coupling between views
Difficult to coordinate navigation across views and
view models (i.e. initiating navigation from a view
model)
Difficult to enforce business rules that impact
navigation
Unit testing navigation is very difficult
@rightincode - http://www.rightincode.com
16. Xamarin.Forms Architectural Guidance -
Navigation
View model-first navigation alternative
Can be achieved by implementing a custom
navigation service or using one of the MVVM
frameworks
Placing the navigation logic in the view model
Decouples views
Improves testability by allowing easier unit testing of
navigation
Allows business rules to control navigation
@rightincode - http://www.rightincode.com
17. Xamarin.Forms Architectural Guidance –
Component Communications
Typical communications between components
are done using events which is a form of
publish-subscribe communications
Works but…
Requires tight coupling between components
Results in the extended lifespan of components
until all references are removed
@rightincode - http://www.rightincode.com
18. Xamarin.Forms Architectural Guidance –
Component Communications
MessagingCenter Class
Implements the publish-subscribe pattern
Allows components to communicate without having a
reference to each other
Allows components to be independently developed
and tested
Publishers send messages and subscribers listen for
messages they are interested in
Internally, the MessagingCenter Class uses weak
references – it does not keep objects alive and allows
them to be garbage collected
@rightincode - http://www.rightincode.com
20. Resources
Repo: (code)
https://github.com/rightincode/TipCalcPro
Onion Architecture
http://blog.thedigitalgroup.com/chetanv/2015/07/06/und
erstanding-onion-architecture/
Inversion of Control
https://martinfowler.com/articles/injection.html
Unit Testing
https://www.toptal.com/qa/how-to-write-testable-code-
and-why-it-matters
Pluralsight
Building a Pragmatic Unit Test Suite
Writing Highly Maintainable Unit Tests
Advance Unit Testing
@rightincode - http://www.rightincode.com