Get Catalog Names

Posts   
 
    
LeoB
User
Posts: 2
Joined: 29-Jun-2020
# Posted on: 29-Jun-2020 18:04:41   

C# LLBLGEN 5.7.0 Adapter pattern

I'm using a custom DataAccessAdapter in a solution with multiple catalogs and queries with relationships across multiple catalogs. My issue comes when progamatically updating all catalogs per query. I can use the CatalogNameOverwriteHashtable and get the new catalog names without issue. I just need a clean way to get the original catalog names.

I realise I could iterate through all my entities and use this: var catalogName = PersistenceInfoProviderSingleton.GetInstance().GetAllFieldPersistenceInfos(new MyEntity()).First().SourceCatalogName;

This seems excessive with the number of entities we have. Or I could just put the original catalog names in a config file, but this creates other maintenance issues.

Is there another way to programmatically get all the old catalog names?

Walaa avatar
Walaa
Support Team
Posts: 14983
Joined: 21-Aug-2005
# Posted on: 29-Jun-2020 23:48:27   

Did you try: adapter.CatalogNameOverwrites.Keys ?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39749
Joined: 17-Aug-2003
# Posted on: 30-Jun-2020 09:54:55   

Other than the .Keys way, iterating over a few thousand elements won't take more than 1ms, if not less. The lookup is through a dictionary so it won't take much time. You can also use the EntityType enum and get all values, use tostring to get the names and use that with GetElementMappingInfo instead. So unless you're doing this in every query, I wouldn't worry about enumerating things once.

Frans Bouma | Lead developer LLBLGen Pro
LeoB
User
Posts: 2
Joined: 29-Jun-2020
# Posted on: 30-Jun-2020 10:29:23   

Presumably the adapter will only have the .Keys populated after I've populated it the first time?

new DataAccessAdapter().CatalogNameOverwrites.Keys

This is an empty dictionary for me when trying to get the original catalogs.

I'm happy to use the GetElementMappingInfo iteration, but alternatively is there a recommended way to make changes to the persistenceInfoProvider.lpt file? I can't see a TemplateID in the documentation to add to my project config. In the meantime I've added a HashSet<string> to the PersistenceInfoProviderCore object in the template file:

myCatalogStrings.Add(@"<%=mapping.MappedTarget.ContainingSchema.ParentCatalog.CatalogName%>");

This does the job but I'm not sure it's a long-term solution.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39749
Joined: 17-Aug-2003
# Posted on: 30-Jun-2020 13:24:19   

LeoB wrote:

Presumably the adapter will only have the .Keys populated after I've populated it the first time? new DataAccessAdapter().CatalogNameOverwrites.Keys

This is an empty dictionary for me when trying to get the original catalogs.

Yes, I think Walaa assumed you defined the overwrites somewhere e.g. a config file (as you know the new names too, so the old names should be available there too).

I'm happy to use the GetElementMappingInfo iteration, but alternatively is there a recommended way to make changes to the persistenceInfoProvider.lpt file? I can't see a TemplateID in the documentation to add to my project config. In the meantime I've added a HashSet<string> to the PersistenceInfoProviderCore object in the template file:

myCatalogStrings.Add(@"<%=mapping.MappedTarget.ContainingSchema.ParentCatalog.CatalogName%>");

This does the job but I'm not sure it's a long-term solution.

Altering the shipped template isn't really recommended. If you want the catalog names in the mappings, I'd go for an adhoc template (https://www.llblgen.com/Documentation/5.7/Designer/Functionality%20Reference/AdHocTemplates.htm) which is a separate simple template that's run every time you generate code and which generates e.g. a static class with C# code that produces the catalog names in a list or dictionary.

Enumerating the mapped catalogs in the project elements, you can either enumerate all mappings in a foreach loop and emit the catalog names there (just borrow code from the persistence info provider template) or simply ask the project what catalogs have elements in them using this method: https://www.llblgen.com/Documentation/5.7/ReferenceManuals/Designer/html/CB8F8344.htm There might be catalogs without mapped elements on them in that list, which might not be what you need.

In all honesty, I'd go for the meta-data retrieval as you had it, you need to it just once (as the data is static) and I doubt you'll notice the enumeration, even if your project has 1000s of entities. It frees you from the template adjustments and will work always.

Frans Bouma | Lead developer LLBLGen Pro