Translating to Enums

Posts   
 
    
BubbaBill
User
Posts: 6
Joined: 18-Jul-2005
# Posted on: 04-Aug-2005 16:12:38   

In the database I often store enum values as smallint and then perform an Enum.Parse when I pull them out to cast the int values into the enumeration structures I have already created. I see in the designer that the .NET type is set to System.Int16 automatically and I am wondering if there is any way I can force the type to actually bind to my enumeration and not the System.Int16 type.

As an example I have the idea of statuses that are defined in C# as follows:

public enum PackageStatus { Active = 0, Inactive = 1, Pending = 2, ScheduledForInitialization = 3 }

In the database this is stored in the Package entity as status smallint NOT NULL.

The designer is defining the Package entity in C# as System.Int16 Status. I would like the definition to actually be PackageStatus Status.

Thanks...

Skeeterbug
User
Posts: 165
Joined: 21-May-2004
# Posted on: 04-Aug-2005 18:50:08   

BubbaBill wrote:

In the database I often store enum values as smallint and then perform an Enum.Parse when I pull them out to cast the int values into the enumeration structures I have already created. I see in the designer that the .NET type is set to System.Int16 automatically and I am wondering if there is any way I can force the type to actually bind to my enumeration and not the System.Int16 type.

As an example I have the idea of statuses that are defined in C# as follows:

public enum PackageStatus { Active = 0, Inactive = 1, Pending = 2, ScheduledForInitialization = 3 }

In the database this is stored in the Package entity as status smallint NOT NULL.

The designer is defining the Package entity in C# as System.Int16 Status. I would like the definition to actually be PackageStatus Status.

Thanks...

No, there isn't a PackageStatus type to bind too. See this for more info on enums.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemenumclasstopic.asp

The enumeration converts the string(active,inactive, etc) into an integral type. If none is given (like above) the default is Int32.

BubbaBill
User
Posts: 6
Joined: 18-Jul-2005
# Posted on: 04-Aug-2005 21:15:14   

What I am wondering is if LLBLGen Pro can translate from the db type of int to an enumeration that I have already defined. I have already created and am using my enumeration in the business layer. When I store that in the db it is stored as an int. So now I am trying to use LLBLGen but it wants to make the field a System.Int and not the intended enumeration.

Skeeterbug
User
Posts: 165
Joined: 21-May-2004
# Posted on: 04-Aug-2005 21:42:31   

BubbaBill wrote:

What I am wondering is if LLBLGen Pro can translate from the db type of int to an enumeration that I have already defined. I have already created and am using my enumeration in the business layer. When I store that in the db it is stored as an int. So now I am trying to use LLBLGen but it wants to make the field a System.Int and not the intended enumeration.

There is an enumeration generator that is in the 3rd party tools section. I used this a few projects back.

quick sample: if(sometable.ForeignKey == (int)someEnum.Value) {}

real sample:

if(UserPermissions.PermissionTypeId == (int)permissionType.Admin) { //admin code } else { }

Is this what you were looking for?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39588
Joined: 17-Aug-2003
# Posted on: 05-Aug-2005 10:52:17   

The designer currently can't set the type to something else than the predefined .NET type for the db type. In 1.0.2005.1 a feature will be added to define a different .NET type for a given DB type, with specific aspects, like NUMBER(1,0) to be boolean.

I'll see if I can also add enum types to this mix on a per-field basis, though it will be difficult, especially because the enum type has to be compiled and referenced by the generated code.

Frans Bouma | Lead developer LLBLGen Pro
Posts: 254
Joined: 16-Nov-2006
# Posted on: 10-Mar-2007 22:39:55   

Just picking up on this old thread I'm also trying to find a way to map a database int field to an enumeration type.

I see you have a ConstantsEnums.cs file generated in the code though which has a section

// __LLBLGENPRO_USER_CODE_REGION_START CustomUserConstants
// __LLBLGENPRO_USER_CODE_REGION_END

So could you read enumerated values from the LLBLGen assembly and allow them to be selected from a drop down in the designer along with all other .NET native types.

This would be really useful to avoid having to do a lot of unnecessary casts.

Thoughts?

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 11-Mar-2007 04:35:21   

I see you have a ConstantsEnums.cs file generated in the code though which has a section

I don't think LLBLGen Designer should worry about the UserCustomCode.. that's tricky disappointed . But you can use TypeConverters smile .

Here is a good example of that you are looking for wink : http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=7355

David Elizondo | LLBLGen Support Team