Skip to content

Instantly share code, notes, and snippets.

@Haosvit
Created March 14, 2017 07:05
Show Gist options
  • Select an option

  • Save Haosvit/67c78e9dcd55dd09360424e0a7efcf60 to your computer and use it in GitHub Desktop.

Select an option

Save Haosvit/67c78e9dcd55dd09360424e0a7efcf60 to your computer and use it in GitHub Desktop.

Có 1 số chỗ trong source code cần viết theo kiểu tối ưu hơn, anh em có thể tham khảo:

  1. Chỗ register singleton. Thay vì viết: Mvx.RegisterSingleton(new Factory()); thì nên viết thành  Mvx.RegisterSingleton(() => new Factory()); Vì kiểu này () => là lazy tạo object, sẽ giảm thời gian tạo đối tượng lúc ban đầu. Có thể rearch thêm class: Lazy để viết tối ưu hơn nữa.

  2. Lazy command init: Thay vì code: public ICommand ShowRouteCommand => new MvxCommand(OnShowRouteCommandHandler); Nên: private IMvxCommand _showRouteCommand; public IMvxCommand ShowRouteCommand => _ showRouteCommand?? (_showRouteCommand = new MvxCommand(OnShowRoute));

  3. Set property với phong cách bờ rào: Phong cách nông dân: private RouteBusStopLine _routeLines; public RouteBusStopLine RouteLines { get { return _routeLines; } set { _routeLines = value; RaisePropertyChanged(() => RouteLines); } } Phong cách bờ rào: private RouteBusStopLine _routeLines; public RouteBusStopLine RouteLines { get { return _routeLines; } set { SetProperty(ref _ routeLines, value); } } (Get code mới nhất sẽ hỗ trợ hàm này)

  4. Các thứ tinh tinh khác nhằm tăng perfomance, đẹp code

  • Sử dụng using cho hình ảnh, text, streamreader này nọ
  • Dùng booltovisibleconverter hoặc valueovisibleconverter thay vì dùng 1 biến bool dạng IsAbcViewVisible rồi đi set lung tung khắp nơi chỉ để ẩn hiện cái view
  • StreamReader thì try catch và dispose và close nó đi ở finally
  • Trước khi close viewmodel thì phải unregister events.
  • Không nên where hay select ở trên dữ liệu của data model (ví dụ như BusStops, BusRoutes) mà viết hàm trong data model để support cho clean code hơn (ví dụ như tạo hàm BusById())
  • View nên implement Idispose và dispose các biến private khi close cái view
  • Các hằng số nên được đặt tên, ví dụ: task.MakePhoneCall(“danang”,”02361022”);  Tạo hằng số ở file ConstVariables.cs rồi bỏ vào đây
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment