Mapping identifying fields to classes

Posts   
 
    
g.smeets
User
Posts: 9
Joined: 06-Jan-2020
# Posted on: 28-Jul-2020 23:28:23   

I'm using classes for Entity identifying fields in our new codebase, which has some advantages that I do not want to go into full detail on right now. I was wondering if anyone has any experience and/or pointers on how to get this integrated with the data layer that LLBLGen generates.

i.e.:


public class FooIdentity : EntityIdentity
{
    protected int Id { get; set;}
    public static implicit operator int(FooIdentity i) { return i.Id; }
    public static implicit operator FooIdentity(int i) { return new FooIdentity(i); }
    
}

public class Foo
{
    public FooIdentity FooId { get; set; }
    public string Bar { get; set; }
}

This allows for simple type casting from ints to Id's, but gives strong typing benefits from there onwards. I hope this gives the gist of what I'm attempting.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 29-Jul-2020 08:09:17   

Hi there!

What LLBLGen version are you using?

I don't fully follow on your motivation of doing this, however this seems like a good candidate to TypeConverter. This converters are special classes that translate from/to your type from/to the system db mapped type, so you can specify your custom type in the Designer and the code is generated using this type in the fields you specify. There are a lot of built-in TypeConverters (bit ->bool, string->bool, etc) but you can implement your own. Please take a look at this guide to know more about it.

David Elizondo | LLBLGen Support Team
g.smeets
User
Posts: 9
Joined: 06-Jan-2020
# Posted on: 30-Jul-2020 01:28:04   

Thanks for the pointer, I'll have a look!

I'm using LLBLGen 5.7 btw.

The rationale is a bit complicated to explain in a few short lines. But what I ultimately want to achieve is to have an embedded DSL with type safe predicates over my entities' identities.

It would be great to get this working end-to-end, so that my identity is of the correct type directly after the O/R mapping without any manual conversion.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39749
Joined: 17-Aug-2003
# Posted on: 30-Jul-2020 09:33:16   

In general one would do this using valuetypes (the DDD variant, like the designer supports), but our runtime doesn't support valuetypes, so they're inlined at generation time. The alternative is indeed a typeconverter, although if you have many of these types you might run into a lot of type converters.

Frans Bouma | Lead developer LLBLGen Pro