Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom ValueConverter and dependency injection #29421

Closed
MithrilMan opened this issue Oct 25, 2022 · 2 comments
Closed

Custom ValueConverter and dependency injection #29421

MithrilMan opened this issue Oct 25, 2022 · 2 comments

Comments

@MithrilMan
Copy link

MithrilMan commented Oct 25, 2022

Is it possible to use dependency injection on a custom value converter?

In my scenario I have dynamic connection string that changes the db based on an api controller route path.
In a segment of that path we store the db alias and we have services in places that adjust the connection string according to that value.
Now the problem is that one of the model has a property that transform a local table identification (autonumeric id) into a string that's composed of both the "database alias" and the local resource id.

E.g. we have a table TableX with it's property ID = 5 on a database named "MyDatabase", the ending result should be
a string property with value "MyDatabase-5"

To do this I tried to implement a ValueConverter using our service that collect the db alias and composing the final value in this ValueConverter.
I thought value converter were instantiated by a serviceProvider using DI but it seems it's not the case, infact I get an error

Cannot create an instance of value converter type 'DomainEntityIdToLocalIdConverter'. Ensure that the type can be instantiated and has a parameterless constructor, or use the overload of 'HasConversion' that accepts a delegate

The value converter I did is this

public class DomainEntityIdToLocalIdConverter : ValueConverter<DomainEntityId?, long?>
{
   public DomainEntityIdToLocalIdConverter(IWayDomainDataContext wayDomainDataContext)
       : base(
           v => v == null ? null : v.RemoteId,
           v => v == null ? null : wayDomainDataContext.GetDomainEntityId(v.Value)
           )
   { }
}

Does ValueConverter supports any kind of dependency injection mechanism?
If not (sigh) what's the suggested approach to achieve what we need?

P.S.
The scope of our service is "Scoped" so matches the EFCore DBContext scope

Thanks

EF Core version: 6.0.8
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 6.0

@ajcvickers
Copy link
Member

@MithrilMan Value converters are part of the model, so this would require building a new model each time the converter changes. This could be solved by #12205, which we are considering for EF8. As it stands, I can't think of a way to do it short of rebuilding the model, which is not very efficient, but could be an option if the number of databases is not too large. See https://learn.microsoft.com/en-us/ef/core/modeling/dynamic-model

@MithrilMan
Copy link
Author

ok I see, hit a limitation, indeed passing a context to the ValueConverter would be nice, I was looking for it too

@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants