Skip to content

Value Objects ​

A blind index can sit inside a value object: an SSN modelled as an Ssn record, a postcode inside an Address. Tayra indexes it, and you query it through the value object, as long as the entity reaches it through [DeepPersonalData].

Model ​

Put [BlindIndex] on the member inside the value object, and its companion next to it. Mark the entity's navigation [DeepPersonalData]:

cs
public sealed record Ssn
{
    [PersonalData, BlindIndex(Transforms = ["digits"])]
    public string Value { get; init; } = "";

    // The companion lives on the value object, next to the member it indexes.
    public string? ValueIndex { get; init; }
}

public class Person
{
    public int Id { get; set; }

    [DataSubjectId]
    public string SubjectId { get; set; } = "";

    // Marks the path Tayra follows to encrypt and to index the value object.
    [DeepPersonalData]
    public Ssn Ssn { get; set; } = new();
}
anchor

On save, Tayra walks the same [DeepPersonalData] graph it encrypts: it hashes Ssn.Value into Ssn.ValueIndex, then encrypts Ssn.Value. This covers nested value objects at any depth, and every element of a [DeepPersonalData] collection.

Why the companion lives on the value object

The value object is indexed the same way wherever it appears. A Person with a Ssn and a SpouseSsn gets two companion columns without declaring either on Person, and the same Ssn type works in every entity that holds one.

Map It in EF Core ​

Owned types, complex types and owned JSON columns all work. The companion maps like any other member of the value object:

cs
modelBuilder.Entity<Person>(e =>
{
    // An owned type: Ssn_Value and Ssn_ValueIndex columns in the People table.
    e.OwnsOne(p => p.Ssn, ssn => ssn.HasIndex(s => s.ValueIndex));

    // Or a complex type, or an owned JSON column:
    //   e.ComplexProperty(p => p.Ssn);
    //   e.OwnsOne(p => p.Ssn, ssn => ssn.ToJson());
});
anchor

Query Through the Value Object ​

Pass BuildPredicateAsync a selector from the entity you query, through the value object:

cs
// The selector goes through the value object, from the entity you query.
var match = await blindIndexer.BuildPredicateAsync((Person p) => p.Ssn.Value, "123-45-6789");

// Translates to: WHERE "Ssn_ValueIndex" = @hash
var people = await db.People.Where(match).ToListAsync();
anchor

The predicate compares the value object's companion (p => p.Ssn.ValueIndex == hash), so it runs in the database and composes with the rest of your query. The value's transforms apply to the search term too: with digits, "123-45-6789" and "123456789" find the same row.

BuildPredicateAsync throws if a step in the selector is not [DeepPersonalData]. Tayra never computes an index there, so the query could only match nothing.

To compute a hash yourself, name the value object's type, which declares the index:

cs
// The index belongs to the value object's type.
var hash = await blindIndexer.ComputeHashAsync("123456789", "ValueIndex", typeof(Ssn));
anchor

Existing Rows ​

Rows saved before an index existed have an empty companion. Fill it with the recompute service, which also covers indexes inside value objects.

Multi-Tenancy ​

When blind index keys are partitioned by tenant, a value object is keyed by the tenant of the record that holds it, so the same SSN in two tenants does not link. Query with BuildPredicateForTenantAsync. A value object with its own [DataSubjectId] or [BlindIndexTenant] is keyed by its own tenant, as it is encrypted with its own key.

Limitations ​

  • Collections are indexed but not queried in one call. Each element of a [DeepPersonalData] collection gets its index, but BuildPredicateAsync takes a member path, not Any(...). Compute the hash and compare the element's companion in your own Any predicate.
  • Classes and records, not structs. Tayra writes the companion into the value object, which a struct copy would lose.