Using other than standard Persistence Mappers per attribute

In MDriven it is possible to create your own persistence mappers per attribute. If you do not create your own it is still possible to pick and choose from  the ones supplied.

Note that this level of specialization will stop the generic tools like MDriven Prototyping and MDriven Server from functioning properly (they use our standard).

One specific case that raise question for this functionality are Enums.

Our standard is to store the enum value as its text representation in the database.

However legacy databases typically treat application level enums as integers.

This question was posted on the forum:

When using enums that are persisted with the GenericEnumAsInteger mapper, you can’t exceute a query like this:
MyEnumType eTest = MyEnumType.theFirstValue
IList<Class1> result = (from x in ecoSpace.PSQuery<Class1>() where (x.MyEnumAttribute == eTest) select x).ToList();

In this case you get the following error:

An exception of type ‘System.Data.SqlClient.SqlException’ occurred in Eco.Persistence.dll but was not handled in user code. Additional information: Error converting a nvarchar value to int.
Using the GenericEnumAsVar or executing a MemQuery instead works as expected.

What the questioner (Alois) has done in this case is to override the suggested persistence mapper on attribute level:

image

The name GenericEnumAsInteger was found in this collection:

image

image

What the questioner was unaware of – since this was not easily understood – hence this article – is exactly how MDriven finds what enum persistence mapper to use. For the attribute we use the override value set on the attribute  – but for the parameter the attribute is not available for us.

Instead we use this strategy:

1. Try to find a Persistence Mapper with the same name as the Type of the parameter. In this case “EcoProject1.MyEnumType”

2. If none was found and if the parameter is of type Enum – we try to find the persistence mapper with the name “Enum”

This is what happen in this particular case. In the error reported I believe that “Enum” persistence mapper is still set to “Eco.Persistence.Default.GenericEnumAsVarChar”

image

So what must be done to make the linq query below work is either to change persistence mapper Enum to Eco.Persistence.Default.GenericEnumAsInteger or to add a new row to the collection of persistance mappers named EcoProject1.MyEnumType with MapperTypeName Eco.Persistence.Default.GenericEnumAsInteger .

MyEnumType eTest = MyEnumType.theFirstValue
IList<Class1> result = (from x in ecoSpace.PSQuery<Class1>() where (x.MyEnumAttribute == eTest) select x).ToList();

This entry was posted in Linq, MDrivenDesigner and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*