Skip to content

Lesson2B

Poh Zhi-Ee edited this page Nov 15, 2018 · 1 revision

Basic Object Oriented Programming

Some keywords to understand

  • Constructor
  • Encapsulation
  • Property, Automatic property
  • Static
  • Access Modifier (public, protected, private)
  • Inheritance
  • Polymorphism
  • Virtual & Override
  • Interface

This one can be learnt through reading book or any common online resource. This knowledge applicable throughout many programming fields, good to know even if not for this project.

XAML

XML-like UI styling, makes creating UI easy.

Most commonly used:

  • StackLayout
  • Grid
  • Label
  • Entry
  • Button

This one pretty much learn by experience. A lot of domain specific knowledge, only 30-40% of things learnt here are applicable elsewhere.

MVVM

Stands for Model-View-ViewModel

It is a software architectural pattern, to separate UI logic from business logic. Good to understand the basic principles of software architecture, applicable to all big software projects. Sounds easy but it is extremely difficult to resolve edge cases and still remain clean on architecture.

Summary

  • Model - pretty much plain data store (like the Item class)
  • View - pretty much all XAML
  • ViewModel - the data source of the xaml
  • Service - this is where most of the business logic reside, i.e. getting data from server and filtering, navigation, login, notifications, etc.

Avoid any code in the .xaml.cs file as far as possible. Even if there is any, make sure it only handles UI, no logic

Unit testing

Isolating a piece of code and test it.

Very similar to programming challenge:

  1. give some input
  2. run the function
  3. determine if output is expected

Main challenges:

1) Isolating a piece of code

Usually most code will rely on something else too e.g. your data processing also involves writing to a log file for error tracking, how can you test only the data processing part?

2) Too specific testing

Overtesting. Start to test for implementation, such as verifying that Service1.Function1 has been called once, Service1.Function2 has been called once, and Service1.Function3 is never called.

This will result in very fragile tests, minor changes in code will result in changing many tests to pass.

Tasks for learning

Basic XAML + ViewModel

  1. Create a basic Xamarin.Forms app
  2. Edit the MainPage.xaml as you see fit
  3. Create a MainViewModel.cs and bind the data of MainPage to MainViewModel
  4. Add buttons in MainPage, bind to Commands in MainViewModel, which wraps around some function inside MainViewModel.

Example command:

public ICommand Button1Command => new Command(Function1);
private void Function1(){
    Debug.WriteLine("Function1 called");
    Content += " A"; //assumes you have already binded Content, also note that if you did not implement INotifyPropertyChanged correctly your View will not update even as Content changes
}
  1. Test if binding works

Unit Testing & OOP

  1. Create a Console application
  2. Create some class