Skip to content

Indexers Package

kobi2294 edited this page Apr 17, 2019 · 1 revision

The Indexers Package

Overview

This package contains classes that help to create indexed properties. You can use ReadonlyIndexer for index properties that only support the get clause, or WriteableIndexer for read-write index properties. We can build indexers using dictionaries, or using getter and setter methods.

Example

The following class represents a league of soccer teams. It exposes a property called Scores that can be used to query the score of each team.

    public class League
    {
        private Dictionary<string, int> _scores = new Dictionary<string, int>();

        public IReadOnlyIndexer<string, int> Scores { get; }

        public League()
        {
            Scores = MvvmKit.Indexers.ReadOnly(_scores);
        }
    }

Now you can use it as follows:

    public static class IndexerDemo
    {
        public static void Run()
        {
            var league = new League();
            var liverpoolScore = league.Scores["Liverpool"];
        }
    }

What if we wanted to allow access to the score both by team name, or by ordinal number? We can build a multi-key indexer, as follows:

    public class League
    {
        private Dictionary<string, int> _scores = new Dictionary<string, int>();

        public IReadOnlyIndexer<string, int, int> Scores { get; }

        public League()
        {
            Scores = MvvmKit.Indexers.ReadOnly(_scores)
                                     .And((int i) => _scores.ElementAt(i).Value);
        }
    }

Now we can access the score using team name, or ordinal index:

    public static class IndexerDemo
    {
        public static void Run()
        {
            var league = new League();
            var liverpoolScore = league.Scores["Liverpool"];
            var firstTeamScore = league.Scores[0];
        }
    }

Interfaces

The package includes the following interfaces

Name Description
IReadOnlyIndexer<K, T> Exposes a collection of values of type T using keys of type K
IReadOnlyIndexer<K1, K2, T> Exposes a collection of values of type T using 2 sets of keys of types K1, K2
IReadOnlyIndexer<K1, K2, K3, T> The same with 3 sets of keys
IReadOnlyIndexer<K1, K2, K3, K4, T> The same with 4 sets of keys
IReadOnlyIndexer<K, T> Exposes a collection of values of type T using keys of type K
IWriteableIndexer<K, T> The same as it's readonly counterpart, but the values may also be modified using the key
IWriteableIndexer<K1, K2, T> The same as it's readonly counterpart, but the values may also be modified using the key
IWriteableIndexer<K1, K2, K3, T> The same as it's readonly counterpart, but the values may also be modified using the key
IWriteableIndexer<K1, K2, K3, K4, T> The same as it's readonly counterpart, but the values may also be modified using the key

Shortcuts

The Indexers class contains a list of static methods and extension methods that can be used to create indexers. They are meant to be used as fluent api's so you can chain them to create the indexer that you need.

  • You may use dictionaries to create readonly or read-write indexers.
  • Use the ReadOnly Method to create a single key read only indexer.
  • You can supply either a getter function or a dictionary as parameter.
  • Use the Writeable method to create a single key read write indexer.
  • You can supply either a pair of getter and setter methods, or a dictionary.
  • You can convert a read only indexer to read-write using the Writeable extension method.
  • Vice versa, you can convert a read-write indexer to read-only using the ReadOnly method.
  • You can add sets of keys to the same indexer using the And Method. It also accepts either access methods or dictionaries.