ReactiveUI, View/ViewModel injection and DI in general

前端 未结 2 1738
南旧
南旧 2020-12-23 23:29

Lately I\'ve trying to get myself into the new age of UI development and discovered ReactiveUI. I love its declarative nature.

I wanted to make a complete switch, so

2条回答
  •  我在风中等你
    2020-12-24 00:05

    ServiceLocator is an anti-pattern that should be avoided.

    I generally think a lot of the advice around IoC/DI is pretty bad in the domain of 'cross-platform mobile applications', because you have to remember that a lot of their ideas were written for web apps, not mobile or desktop apps.

    For example, the vast majority of popular IoC containers concern themselves solely with resolution speed on a warm cache, while basically completely disregarding memory usage or startup time - this is 100% fine for server applications, because these things don't matter; but for a mobile app? Startup time is huge.

    Splat's Service Location solves a number of issues for RxUI:

    1. Service Location is fast, and has almost no overhead to set up.
    2. It encapsulates several different common object lifetime models (i.e. 'create new every time', 'singleton', 'lazy'), just by writing the Func differently
    3. It's Mono Linker friendly (generally)
    4. Service Location allows us to register types in platform-specific code, but use them in PCL code.

    The best way to use Service Locator

    In fact, I do generally agree with Mark Seemann, in that constructor injection is the preferred way to go - this is the pattern I really like:

        public SuspensionHost(ISuspensionDriver driver = null)
        {
            driver = driver ?? Locator.Current.GetService();
        }
    

    This uses a Service Located interface for the default interface, but only if the caller didn't give an explicit one in the constructor. Far more straightforward to test in a unit test runner than trying to construct a sham IoC container, but still falls back to a default implementation at runtime.

    View-First or ViewModel-First?

    Whether you can use VM-based routing (i.e. RoutedViewHost, IScreen, RoutingState, and friends) in ReactiveUI depends on the platform you're on:

    • WPF, Xamarin Forms: Absolutely
    • WP8, WinRT: You can make it work, you lose some transitions and niceties
    • Android, iOS Native: Very difficult to make work

提交回复
热议问题