Repodb: Bug PropertyHandler for Nullable Enum

Created on 22 Mar 2020  路  17Comments  路  Source: mikependon/RepoDB

public enum Hands
{
        Left,
        Right
 }

public class HandPropertyHandler : IPropertyHandler<string, Hands?>
{
        public Hands? Get(string input, ClassProperty property)
        {
            // Transformation back to class
            return Hands.Left;
        }

        public string Set(Hands? input, ClassProperty property)
        {
            // Transformation towards the database
            return Hands.Left.ToString();
        }
 }

public class Person
 {
        public int Id { get; set; }

        [PropertyHandler(typeof(HandPropertyHandler))]
        public Hands? Hand { get; set; }
 }

 Person newPerson= new Person();
int id = connection.Insert<Person, int>(newPerson);

result in Exception
'No coercion operator is defined between types 'System.String' and 'System.Nullable`1[RepodbTest.Hands]'.'

bug deployed review

All 17 comments

Thank you for this bug report. Do you have a timeline (needs) for the fix with regards to this one?

Thank you Michael
The sooner will be the best :-)
I"m using your library on a new project and for now, this blocking me.
I think my last three tickets are somewhere linked.

Best,
Gilles

It seems to be on the compiler of the library for Nullable. I will definitely prioritize this. However, TBH, it may take a little while before I can dig and deliver the fix (maybe a week). But, I will definitely look at this one first over the other. Would that be okay?

Yes, it will be OK.
Thank you, if the first is resolved, it will be a workaround for the others.

Sounds like a plan :) - Cheers!

Can you share the package and version you are using for this? Thanks

Latest
RepoDb 1.10.10
RepoDb.SqlServer 1.0.4

Hello Michael,

Hopes all is fine for you.
Any news on this issue and the #400 #401 ?

Gilles

Hi Gilles, I have not yet dived into this. But please give the time today and tomorrow, I will issue you a beta 2 release for v1.10.11. Is that feasible on your end?

Yes no problem.
Thank you very much Michael.

I am busy with http://repodb.net and is not yet completed. That is why :) - but I will do my assignment for you til tomorrow :)

The fix has been pushed to beta2 of v1.10.11.

> Install-Package RepoDb -version 1.10.11-beta2

The version updates to RepoDb.SqlServer and RepoDb will soon to be released. For now, please install this beta2. Cheers!

I guess it is not resolved, using the project RepoDb.SqlServer.IntegrationTests, in Models, I create

    public enum Hands
    {
        Left,
        Right
    }

```csharp
public class HandPropertyHandler : IPropertyHandler
{
public Hands? Get(string input, ClassProperty property)
{
// Transformation back to class
if (string.IsNullOrEmpty(input))
{
return null;
}
return Hands.Left;
}

    public string Set(Hands? input, ClassProperty property)
    {
        // Transformation towards the database
        if (input.HasValue)
        {
            return Hands.Left.ToString();
        }
        return null;
    }
}
then add column on CompleteTable table
```sql
[Hand] [nvarchar](max) NULL,

and property Hand on CompleteTable class

[PropertyHandler(typeof(HandPropertyHandler))]
public Hands? Hand { get; set; }

this raise a lot of error running the integration test. Issue, should be re-open.

Using the last code in repository

Have you tried dropping the table from your RepoDbTest DB and re-executing the tests?

@sellig - I am doing the test now. It is important to take note that RepoDb supports the Enum automatically without introducing the IPropertyHandler(s). RepoDb compiler auto converts your field values into enum internally (if your field is NVARCHAR/INT and the class property as ENUM). In short, no need to create a property handler for this.

Can you test your solution without the property handler?

When it comes to the exceptions of the property handler for enumeration. I find it not an ideal use-case actually as you are doubling the transformation (unless I will remove the support to Enum) and let the developers handle that via PropertyHandler. Technically, I can't convert the the already converted value for the target enum.

Please revert if this answer your questions or you need more clarrity. Thanks!

I have have tried by droping the table but it's same result.
I know that I don't need the handler but It was a workaround for #401, it's the same without the handler.

Was this page helpful?
0 / 5 - 0 ratings