Making code more expressive

JasinCole

Well-known member
Joined
Feb 16, 2023
Messages
66
Programming Experience
1-3
This is another ones of those problems that I just don't know the best way to handle/best practice.

I'm posting this here because it has to do with xaml code in avalonia, but I do not think this is specific to that project.

Let's say I have the following xaml, and in the VM I have a dictionary named Accounts. The Dict<Key (is the account number),Value (is the balance)>.
The reason I am doing the below is a Dictionaries order is not guaranteed and I want to display the information in a similar fashion everytime. But instead of making hundreds of calls to the DB I am making just one that is returning all accounts I ask for with just the account number and balance and assigning that to a dictionary.

With the above I can call Accounts[acctnum] as below. But this seems not very expressive and what happens if I want to change the account numbers and import them at runtime?
C#:
<Grid ColumnDefinitions="*, Auto">
            <TextBlock Grid.Column="0" Text="General Checking" />
            <TextBlock
              Grid.Column="1"
              Text="{CompiledBinding Accounts[1000], FallbackValue=0}" />
          </Grid>
 
Have you looked at the tutorials on using the DataTemplate.
 
Have you looked at the tutorials on using the DataTemplate.

I have used the DataTemplate before when writing xaml for an ItemsControl. I understand what it's used for, but how does this help me? That I am not sure of. Can you give me a few more hints and nudges in a specific direction?

I've read this Data Templates - Avalonia UI
All that seems to indicate(to me) is that I should probably be using models in my code more.
 
A dictionary is a collection. As you noticed when you were using the data template with your items control, the template is applied to each item of the collection without you having to explicitly give indexes into the collection.

I maybe making that the mistaken assumption that the fragment of XAML you showed in post #1 is repeated for each of the accounts in your dictionary, and you are concerned about changing the account number in the binding expression. Let the data template do the repeating pattern with varying account numbers.

As an aside, for good or bad, the dictionary implementation in C# will actually keep things in the order that they are added in. The no guarantees about order comes into play when you start removing and then adding items. I know this flies in the face of the way we are taught about hash tables, and how a "dictionary" implementation is just a hashtable where hashtables do not guarantee order.
 
I maybe making that the mistaken assumption that the fragment of XAML you showed in post #1 is repeated for each of the accounts in your dictionary
Well that's part of the problem. Assuming I take the full list of accounts I want to work with on my view, that dictionary contains items that I do not want to show in a repetitive pattern. But I do use those other accounts in calculations in the same view.

That means at some point I am going to be more explicite either by displaying the accounts in xaml one by one or in the code behind by breaking out the accounts into other collections. To be fair they both feel a little dirty, like there should be a more elegant solution.

That is the crux of the issue for me, I have 3 choices, 2 are obvious and just don't feel right and the third is the elusive one that may or may not exist(probably over my head). I never know which way to go and while it all works it just seems clunky. This is problably more of an exercise in "How do I make this explicit non-expressive repetitive code look and feel more streamlined?"
 
But I do use those other accounts in calculations in the same view.

Keep those other accounts in their own data structure, or have your view model only expose the accounts that should be in the view.
 

Latest posts

Back
Top Bottom