expressappframework-405451-multitenancy-shared-data.md
In multi-tenant applications, the host database can maintain shared business objects accessible to tenants. Shared data is helpful when storing master tables, such as currency, tax, and other global application settings. Tenant users can read these shared data tables, but cannot modify table data. Host UI administrators have complete CRUD control over host and tenant data through code.
Register host object types as shared to indicate that all tenants have read-only access to corresponding data. Data types associated with shared types must also be registered as shared types. Otherwise, access to associated objects is denied.
Use the WithSharedBusinessObjects (EF Core) / WithSharedBusinessObjects (XPO) method to register shared host object types in the application builder.
The list below contains methods that return the Object Space for shared data. When calling these methods, set the generic type parameter to the shared object type and the tenantName parameter to null. Once you obtain the Object Space, access required data in the same manner as you would do with regular business objects.
Tip
In tenant accounts, you can use CreateObjectSpace methods without the tenantName parameter to get the same result.
The following code sample registers the TaxRate type as a shared type and accesses TaxRate objects in code:
builder.AddMultiTenancy()
.WithSharedBusinessObjects(typeof(TaxRate))
// ...
decimal CalculateTax() {
using (var objectSpace = Application.CreateObjectSpace<TaxRate>(null)) {
var taxRate = objectSpace.GetObjectsQuery<TaxRate>().FirstOrDefault(t => t.State == Customer.BillingAddressState);
if (taxRate != null) {
return TotalAmount * taxRate.Rate;
}
}
return 0;
}
Tip
You can find examples on how to access business objects shared by the host from a tenant in our Outlook Inspired Demo installed with XAF. Files to review:
Important
Host user interface mode does not support the following standard modules: Modules in a Multi-Tenant Application
You can use shared business objects in calculated properties as operands in Free Joins. Specify a PersistentAliasAttribute.
You can assign a shared type to a non-persistent reference property to display the shared business objects in the lookup property editor.
Note
If you use shared types with business object types in calculated or lookup properties, you must populate additional object spaces as described in the following method description: PopulateAdditionalObjectSpaces(XafApplication).
The host has full access to tenant data. The host can use the following methods to obtain an Object Space for a specific tenant.
ObjectSpaceFactoryExtensions.CreateObjectSpace<T>(objectSpaceFactory,tenantName)
IDataService.GetObjectSpace<T>(tenantName)
var objectSpace = Application.CreateObjectSpace<Type>('tenantName'));
Note
To access tenant data, the tenant database must exist and be updated; you cannot access databases that are non-existent or uninitialized.
IServiceProvider instance from the ObjectSpace or Session instance used to access shared business objects, as services retrieved in this manner may function incorrectly.See Also
Access Object Space, Security System, and Caption Helper in the ASP.NET Core Environment
Ways to Access an Object Space (the Database Context for CRUD Operations)