question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Proposal: Instantiate Resources from Dependency Injection

See original GitHub issue

Proposal: Instantiate Resources from Dependency Injection

Summary

Use Microsoft.Extensions.DependencyInjection or other framework that implements IServiceProvider to instantiate resources to instantiate Resources from XAML…

Rationale

  • Dependency Injection allows for creating loosely coupled resources such as View Models that are defined in separate or distant (not in same solution) projects.
  • Current methods require requiring a “dummy instance” on a default constructor that can be instantiated by Xaml, and then removing and replacing it at runtime from the Resources.
  • Will greatly improve maintainability and quality of code.

Scope

Capability Priority
This proposal will allow developers to use design time bindings on View Models and other objects created via Dependency Injection Must
This proposal will allow developers to use any dependency injection framework that implements IServiceProvider. Must
This proposal will allow developers to define an Interface as a Resource since the instance will be obtained from the IServiceProvider. Must
This proposal will not require dependency injection for all projects. Must

Important Notes

Example

<local:IMyResource x:Key="MyResource" ServiceProvider="{x:Bind Provider}" />

Here, ServiceProvider is any instance of IServiceProvider that can be resolved via x:Bind.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:14
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
sharpninjacommented, Nov 15, 2021

Nice, do you have a simple skeleton to share by any chance?

https://github.com/sharpninja/WindowsAppSdkHost

3reactions
jesbiscommented, Dec 10, 2019

Thanks for the full example! That’s much clearer.

This seems worth considering. It’s unlikely we’ll be able to get to this for the first WinUI 3.0 release, so we’ll keep it in the backlog for a future iteration.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why should I use dependency injection?
Dependency inversion or injection is a technique that allows your code to decide on what implementation of a method gets called at run...
Read more >
Java Dependency Injection - DI Design Pattern Example ...
We have used constructors to inject the dependencies in the application classes, another way is to use a setter method to inject dependencies...
Read more >
Use dependency injection in .NET Azure Functions
Constructor injection is used to make your dependencies available in a function. The use of constructor injection requires that you do not use ......
Read more >
Dependency injection in Android
Learn to build for your use case by following Google's prescriptive and opinionated guidance. ... Stay in touch with the latest releases ...
Read more >
Manual dependency injection
Dependency injection is a good technique for creating scalable and testable Android apps. Use containers as a way to share instances of classes ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found