Dependency Injection in an IValueConverter #12708
-
In the old Xamarin.Forms, you could do DI using static functions and put them anywhere, including in converters. I much prefer the approach that leverages Microsoft.Extensions.DependencyInjection, but what is the best pattern for converters now? Here's what I know:
Prism.Maui is only in beta. Since I like XAML, do not use Prism, want to keep using converters, and want to avoid anti-patterns... it makes me wonder what the right approach is. The answer that seems most obvious given the nature of converters is that they are meant to be simple immutable stateless functions, and I should only use them when I need to do trivial conversions that don't rely on services. And thus, building some kind of wrapper or factory is the right approach when services are required. Am I missing anything? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 8 replies
-
This is sorely needed! |
Beta Was this translation helpful? Give feedback.
-
It is some sort of anti pattern, but I´m using a static service helper class.
And then use it like this:
But I would prefer some sort of constructor or property injection. |
Beta Was this translation helpful? Give feedback.
-
Recommend not using a static IServiceProvider. If you run into this issue, I think it is a potential sign that you should consider a bit of refactoring. Here are a number of better options, in my opinion:
|
Beta Was this translation helpful? Give feedback.
Recommend not using a static IServiceProvider. If you run into this issue, I think it is a potential sign that you should consider a bit of refactoring.
Here are a number of better options, in my opinion: