Avoiding circurlar references / fetches

Posts   
 
    
Posts: 37
Joined: 09-Nov-2016
# Posted on: 26-Jan-2017 15:50:59   

Hi,

I have generated a model from my database and two tables have a navigator to each other.

Table 1 -> Table 2 Table 2 -> Table 1

When I get data like this (using Prefetch):


using (DataAccessAdapter adapter = new DataAccessAdapter("Somestring"))
{
    var query = new LinqMetaData(adapter);

    var itemPath = new PrefetchPath2(EntityType.ItemEntity);
    
    itemPath.Add(ItemEntity.PrefetchPathTable1).SubPath.Add(Table1Entity.PrefetchPathTable2);
    
    var allItems = query.TableItem.WithPath(itemPath).ToList();
}

The resulting JSON looks something like this:


{  
   "table1":[  
      {  
         "table2":[  
            {  
               "table1":[  
                  {  
                     "table2":[  
                        {  

                        }
                     ]
                  }
               ]
            }
         ]
      }
   ]
}

This results in a large amount of data which is not needed. The serialization is being configured like this:


 services.AddMvc()
            .AddJsonOptions(options => options.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects)
            .AddJsonOptions(options => options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver() { IgnoreSerializableAttribute = true, IgnoreSerializableInterface = true });

The question is, is there any way to either tell the designer, that the navigator should be ignored when serializing or some way to add this to the above Prefetch call when using the generated code?

I have tried searching the forums, and also tried using the ExcludeIncludeFieldsList but without any luck.

Best regards Andreas

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 26-Jan-2017 17:58:35   

Is the problem about fetching the data or serializing it? Do you need to fetch more data/levels than you need to serialize?

For fetching data, you can choose the level to stop at using PrefetchPath, you will only fetch what needs to be fetched.

Posts: 37
Joined: 09-Nov-2016
# Posted on: 26-Jan-2017 22:36:17   

Hi Walaa,

Thank your for your reply.

The problem is serializing it, meaning that I would like to keep both navigators (a simple solution would be to delete one navigator), but control the serialization. We are migrating from an EF project, and we used the [JsonIgnore] Data Annotation on the Data Model classes to stop the serialization from creating a circular reference.

So I would like to exclude certain properties when serializing the object. Something like this:


itemPath.Add(ItemEntity.PrefetchPathTable1)
   .SubPath.Add(Table1Entity.PrefetchPathTable2)
          .Exclude(SomePropertiesRelatedToTable1Entity)
  .SubPath.Add(Table2Entity.PrefetchPathTable3);

Or set a flag in designer that this property should not be serialized.

Not sure I understand the level you mention.

We are using LLBLGen Pro 5.1 and the LLBLGen Runtime (sorry for not specifying this earlier).

Best regards, Andreas

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 27-Jan-2017 10:07:58   

You could add a Browseable(false) attribute on one of the navigators (the one you don't need). You can do so in the entity editor-> code generation info tab, additional attributes. Select the navigator, then add the attribute. (using Browseable($false))

If I'm not mistaken, the value is then ignored when serializing.

Frans Bouma | Lead developer LLBLGen Pro
Posts: 37
Joined: 09-Nov-2016
# Posted on: 27-Jan-2017 11:19:34   

I have tried added the Browsable($false) attribute, but it is still being serialized. The tooltip states that the attribute "Specifies whether a property or event should be displayed in a Properties window.".

I have attached a screenshot of my editor and my generated code looks like this:


...
    [Browsable(false)]
    [DataMember]
    {
...

Am I adding the right attribute?

Attachments
Filename File size Added on Approval
Browsable.JPG 87,487 27-Jan-2017 11:19.42 Approved
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 28-Jan-2017 09:19:22   

The attribute is applied correctly, but it's not a given it will work, sadly. It's up to the serializer whether it will work, I hoped it would pan out ok. However 'DataMember' is applied too. If you uncheck the checkbox in the inherited attributes pane in your screenshot, in front of DataMember, (and remove browsable(false), it's of no use here it seems), it won't generate that attribute and the property shouldn't be seen as part of the contract. Could you try that please?

Frans Bouma | Lead developer LLBLGen Pro
Posts: 37
Joined: 09-Nov-2016
# Posted on: 31-Jan-2017 11:37:11   

Yes that worked simple_smile

Thank you for your help!