<commit_before>As mentioned previously the link:../api/org/grails/datastore/mapping/multitenancy/TenantResolver.html[TenantResolver] interface is how you define the value of the link:../api/grails/gorm/multitenancy/Tenants.html#currentId()[current tenant].

This section will cover a few more details about implementing `TenantResolver` instances.

==== Specifying the Tenant Resolver

As already mentioned you can specify the `TenantResolver` via configuration using the `grails.gorm.tenantResolverClass` setting:

[source,yaml]
----
grails:
    gorm:
        multiTenancy:
            mode: DATABASE
            tenantResolverClass: org.grails.datastore.mapping.multitenancy.web.SubDomainTenantResolver
----

However, if you are using Grails or Spring Boot then the `TenantResolver` can also be specified as a Spring bean and it will automatically be injected, this allows you to use dependency injection to configure the dependencies of the resolver.

==== Built-in Tenant Resolvers

The following table contains all of the `TenantResolver` implementations that ship with GORM and are usable out of the box. The package name has been shorted from `org.grails.datastore.mapping` to `o.g.d.m` for brevity:

[format="csv", options="header"]
|===
name,description
`o.g.d.m.multitenancy.resolvers.FixedTenantResolver`, Resolves against a fixed tenant id
`o.g.d.m.multitenancy.resolvers.SystemPropertyTenantResolver`, Resolves the tenant id from a system property called `gorm.tenantId`
`o.g.d.m.multitenancy.web.SubDomainTenantResolver`, Resolves the tenant id from the subdomain via DNS
`o.g.d.m.multitenancy.web.CookieTenantResolver`, Resolves the current tenant from an HTTP cookie named `gorm.tenantId` by default
`o.g.d.m.multitenancy.web.SessionTenantResolver`, Resolves the current tenant from the HTTP session using the attribute `gorm.tenantId` by default
|===

The tenant resolvers in the `org.grails.datastore.mapping.multitenancy.web` package require the `grails-datastore-web` dependency:

[source,groovy,subs="attributes"]
.build.gradle
----
compile "org.grails:grails-datastore-web:{version}"
----

==== The AllTenantsResolver interface

If you are using discriminator-based multi-tenancy then you may need to implement the link:../api/org/grails/datastore/mapping/multitenancy/AllTenantsResolver.html[AllTenantsResolver] interface in your `TenantResolver` implentation if you want to at any point iterate over all available tenants.

Typically with discriminator-based multi-tenancy the tenants are identified by some other domain class property. So for example an implementation would look like:

[source,groovy]
----
Iterable<Serializable> resolveTenantIds() {
    new DetachedCriteria(Company)<commit_msg>Document HttpHeaderTenantResolver<commit_after>As mentioned previously the link:../api/org/grails/datastore/mapping/multitenancy/TenantResolver.html[TenantResolver] interface is how you define the value of the link:../api/grails/gorm/multitenancy/Tenants.html#currentId()[current tenant].

This section will cover a few more details about implementing `TenantResolver` instances.

==== Specifying the Tenant Resolver

As already mentioned you can specify the `TenantResolver` via configuration using the `grails.gorm.tenantResolverClass` setting:

[source,yaml]
----
grails:
    gorm:
        multiTenancy:
            mode: DATABASE
            tenantResolverClass: org.grails.datastore.mapping.multitenancy.web.SubDomainTenantResolver
----

However, if you are using Grails or Spring Boot then the `TenantResolver` can also be specified as a Spring bean and it will automatically be injected, this allows you to use dependency injection to configure the dependencies of the resolver.

==== Built-in Tenant Resolvers

The following table contains all of the `TenantResolver` implementations that ship with GORM and are usable out of the box. The package name has been shorted from `org.grails.datastore.mapping` to `o.g.d.m` for brevity:

[format="csv", options="header"]
|===
name,description
`o.g.d.m.multitenancy.resolvers.FixedTenantResolver`, Resolves against a fixed tenant id
`o.g.d.m.multitenancy.resolvers.SystemPropertyTenantResolver`, Resolves the tenant id from a system property called `gorm.tenantId`
`o.g.d.m.multitenancy.web.SubDomainTenantResolver`, Resolves the tenant id from the subdomain via DNS
`o.g.d.m.multitenancy.web.CookieTenantResolver`, Resolves the current tenant from an HTTP cookie named `gorm.tenantId` by default
`o.g.d.m.multitenancy.web.SessionTenantResolver`, Resolves the current tenant from the HTTP session using the attribute `gorm.tenantId` by default
`o.g.d.m.multitenancy.web.HttpHeaderTenantResolver`, Resolves the current tenant from the request HTTP Header using the header name `gorm.tenantId` by default
|===

The tenant resolvers in the `org.grails.datastore.mapping.multitenancy.web` package require the `grails-datastore-web` dependency:

[source,groovy,subs="attributes"]
.build.gradle
----
compile "org.grails:grails-datastore-web:{version}"
----

==== The AllTenantsResolver interface

If you are using discriminator-based multi-tenancy then you may need to implement the link:../api/org/grails/datastore/mapping/multitenancy/AllTenantsResolver.html[AllTenantsResolver] interface in your `TenantResolver` implentation if you want to at any point iterate over all available tenants.

Typically with discriminator-based multi-tenancy the tenants are identified by some other domain class property. So for example an implementation would look like:

[source,groovy]
----
Iterable<Serializable> resolveTenantIds() {
    new DetachedCriteria(Company)
{'fraction_non_alphanumeric': 0.08785345900764717, 'fraction_numerical': 0.0, 'mean_word_length': 5.126361655773421, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7388316', 'n_tokens_mistral': 1601, 'n_tokens_neox': 1584, 'n_words': 545}