Open idan-h opened 2 years ago
Hi @idan-h, thanks for this, I think people will find it very useful!
Unfortunately, I don't think this is a great fit for the library itself - the StronglyTypedId library is a source generator only, so by default you don't want the dll copied to the build output, which means these methods aren't available 🤷
I think this is maybe better placed as a blog post/documentation in the README?
Yea, this is why I added it in a seperate project, so it could be shipped in a different nuget package.
If you want to add it to documentation thats fine by me, but I think in the long term there would be many more helper methods like this, so the real life integration would be feasible.
You might want to consider adding another repo, or combine that in the current repo and ship different nuget package
Yea, this is why I added it in a seperate project, so it could be shipped in a different nuget package.
🤦♂️ My bad, I'm clearly tired, I read that as being src/StronglyTypedIds/Efcore. That makes much sense.
I think you're probably right, as a separate package of "helpers" does make sense 🙂 My concern here is the heavy use of reflection to do this, which feels quite fragile. I'll have a little think about it and see
Yea, that's true, but I think there is no way out of reflection on this one.. Its either you write a lot of boilerplate code or use reflection Tests are sure needed
This seems very interesting. Could this be done as it's own nuget if it won't be merged in? Would also be useful to update the README.md when new functionality like this is introduced.
Would it be possible have a source generator output a method with signature void ConfigureConventions( ModelConfigurationBuilder configurationBuilder )
that accumulates all of the known StronglyTypedId
objects with EfCoreValueConverters
and registers all of them? I could see it being tricky to opt into/out of that, but the heuristic of just having a ef core converter at all seems like it should suffice for most cases.
It looks like this method adds a configuration for ALL entities, ONLY on the Id
property.
For example, I have a class like
public class SomeClass
{
public Guid Id { get; set; }
// this is a FK
public MyRelatedEntityClassId MyRelatedEntityClassId { get; set; }
public virtual MyRelatedEntityClass MyRelatedEntityClass { get; set; }
}
public class MyRelatedEntityClass
{
// this has 'many' of the 'someCLass'
public virtual ICollection<SomeClass> SomeClasses { get; set; }
}
if i register your class like:
builder.RegisterStrongTypedIdDynamically(entityType, this, "MyStrongId ");
I will get the error:
Unhandled exception. System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.InvalidOperationException: The property 'MyStrongId ' cannot be added to the type 'MyRelatedEntityClass' because no property type was specified and there is no corresponding CLR property or field. To add a shadow state property, the property type must be specified.
I'll make some modifications in the morning and share if i get it working. Or, i'll just manually register in the configs...
59
Extension method which dynamically registers the id conversion, and if needed automatically uses Identity
Use inside the DbContext
OnModelCreating
:Tested only with postgresql, tests with different dbs might be needed
It uses
And
If needed