Happy new year folks!
Q1: How should I register .SingleInstance() dependencies so that they get resolved fast?
Q2: Is there any speed difference between type (reflection) based registration and lambda based registration when dealing with .SingleInstance() dependencies?
Q3: Please look at the working code at the bottom: Can I improve on it? Is the current type-based registration of the ViewModel classes considered speed-optimal?
I am using Autofac in a low power Xamarin.Forms (mobile device) environment to resolve the ViewModel objects corresponding to the View from where the VM gets resolved. I want to make the resolution fast as the user can swap very fast between views.
All of my Service-layer-dependencies are configured as SingleInstance() and have a lifetime equal to the application. This makes sense as this is a single user, client-application, so that services dont need to be scoped between clients.
The lifetime of the ViewModels on the other hand are tied to their View, so each resolved ViewModel needs to be a new one. ViewModels are NOT .SingleInstance().
The very and of this post shows a sample resolution call to the ViewModel. Note that I am providing extrenal dependencies for the Resolve(...) call.
Now how could I improve on the Autofac configuration so that I get the maximum possible speed without unnecessary reflection for trivial tasks?
Bellow you can see snipets on my DI configuration:
builder.RegisterType<XamarinLocalSettingsPersister>().As<IPersistLocalSettings>().SingleInstance();
builder.RegisterInstance<IDictionary<string, object>>(settingsKvps).ExternallyOwned();
builder.RegisterType<Settings>().As<ISettings>().SingleInstance();
builder.RegisterType<TmdbApiService>().As<ITmdbApiService>().SingleInstance();
builder.RegisterType<MovieGenreSettingsService>().As<IMovieGenreSettingsService>().SingleInstance();
// And more like this...
builder.RegisterType<MainPage3ViewModel>().AsSelf();
builder.RegisterType<TrendingPage3ViewModel>().AsSelf();
builder.RegisterType<MainSettingsPage2ViewModel>().AsSelf();
builder.RegisterType <MovieDetailPageViewModel>().AsSelf();
// And more like this...
Sample resolution call is done by providing additional external dependencies:
scope.Resolve<MovieDetailPageViewModel>(
new TypedParameter(typeof(MovieDetailModel), movie),
new TypedParameter(typeof(IPageService), new PageService(this))
Aucun commentaire:
Enregistrer un commentaire