Orchardcore addresses

From CrestApps/CrestApps.AgentSkills · View interactive version

Orchardcore addresses is an AI agent skill from the GitHub repository CrestApps/CrestApps.AgentSkills. Skill for the CrestApps Orchard Core Addresses module (CrestApps.OrchardCore.Addresses, Addresses.Countries, Addresses.GooglePlaces). Covers the immutable Address value shared by customers, checkout, orders, shipping and taxation, per-country address formats from Google address data with AddAddresse

How to use this skill

Skill file

CrestApps Addresses

Addresses (new in CrestApps.OrchardCore 3.0) gives the platform one address structure for every country. Each country's rules decide which parts it has, which are required, and how it is checked and printed. The data is a snapshot of Google's address data (CC BY 4.0) inside the package; a site never calls Google for formats.

Where it fits in the commerce suite

| Module | Relationship |

| --- | --- |

| Shipping (orchardcore-shipping) | Requires Addresses; zones and rates use Address |

| Customers (orchardcore-customers) | Address book edited with the address editor |

| Checkout and Orders (orchardcore-checkout, orchardcore-orders) | Contact and shipping steps, immutable order copies, printed with IAddressFormatter |

| Taxation (orchardcore-taxation) | Jurisdictions match region codes; country lists come from ICountryService |

| CrestApps Content Fields (orchardcore-crestapps-content-fields) | Address Field (CrestApps.OrchardCore.ContentFields.Address) on any content type |

Addresses depends on nothing and can be used outside commerce.

Features

| Feature ID | Name | Depends on |

| --- | --- | --- |

| CrestApps.OrchardCore.Addresses | Addresses | none |

| CrestApps.OrchardCore.Addresses.Countries | Address Countries | Addresses, OrchardCore.Contents, OrchardCore.ContentFields, OrchardCore.Title |

| CrestApps.OrchardCore.Addresses.GooglePlaces | Address Suggestions (Google Places) | Addresses |

Guidelines

{
  "steps": [
    {
      "name": "Feature",
      "enable": [
        "CrestApps.OrchardCore.Addresses",
        "CrestApps.OrchardCore.Addresses.Countries",
        "CrestApps.OrchardCore.Addresses.GooglePlaces",
        "OrchardCore.RateLimits"
      ],
      "disable": []
    }
  ]
}

The Address value

Address (CrestApps.OrchardCore.Addresses.Models, in CrestApps.OrchardCore.Addresses.Abstractions) is immutable with init-only parts: Name, Company, AddressLine1, AddressLine2, DependentLocality, City, Region, PostalCode, SortingCode, Country (ISO alpha-2), County, District (for tax) and Phone. It also carries optional coordinates, Latitude, Longitude, GeocodedUtc and GeocodeSource (a geocoding provider's name, or Manual when pinned on a map), which Clone() copies. AddressComparison.IsSameAddress(left, right) compares the street lines, city, region, postal code and country, trimmed and case-insensitively.

var address = new Address
{
    Name = "Avery Brooks",
    AddressLine1 = "1 Main Street",
    City = "Springfield",
    Region = "IL",
    PostalCode = "62701",
    Country = "US",
};

Contact info, named addresses and address books

These shared building blocks live in CrestApps.OrchardCore.Addresses.Abstractions so modules stop declaring their own copies:

Normalize a phone typed into a form with PhoneNumberNormalization.ToE164OrTrimmed(text, phoneNumberService, country) (PhoneNumbers.Abstractions); see orchardcore-phone-numbers.

Formats, validation and printing

Registered with services.AddAddressesCore() (the Addresses feature does this):

| Service | Use |

| --- | --- |

| IAddressFormatProvider.GetFormatAsync(countryCode) | AddressFormat: Layout (and LatinLayout), Required, Uppercase, PostalCodePattern, PostalCodeExamples, labels (RegionLabel, PostalCodeLabel), Regions; FindRegion by code, key or name. Unknown countries get a generic format. |

| IAddressValidator.ValidateAsync(address) | AddressValidationResult with Problems (AddressProblem(Component, Kind), kinds Missing, Invalid, Unknown, NotInRegion), IsValid, and the tidied Address |

| IAddressFormatter.GetLinesAsync(address, options) | Lines in the country's order; AddressFormatOptions.Envelope capitalizes as the post wants, IncludeCountry adds the country name |

public sealed class WarehouseAddressService
{
    private readonly IAddressValidator _validator;
    private readonly IAddressFormatter _formatter;

    public WarehouseAddressService(IAddressValidator validator, IAddressFormatter formatter)
    {
        _validator = validator;
        _formatter = formatter;
    }

    public async Task<IReadOnlyList<string>> GetLabelLinesAsync(Address address)
    {
        var result = await _validator.ValidateAsync(address);

        if (!result.IsValid)
        {
            return [];
        }

        return await _formatter.GetLinesAsync(result.Address, new AddressFormatOptions { Envelope = true });
    }
}

Validation checks the form of an address, not that the place exists.

The address editor

Every address form (address book, admin, checkout, Address Field) uses the AddressEditor shape: country first, then fields in the country's order with its labels, required marks and region list. IAddressEditorService (CrestApps.OrchardCore.Addresses.Core.Editing):

| Method | Use |

| --- | --- |

| BuildAsync(address, options) | Editor model for an address or a new one |

| PopulateAsync(model, options) | Lay a posted model out again for its country |

| ValidateAsync(model, modelState, prefix, options) | Add problems to ModelState under prefix, in the visitor's language; returns the tidied address |

| GetLayoutAsync(countryCode) | The AddressEditorLayout |

AddressEditorOptions: ShowName, RequireName, ShowCompany, ShowPhone, Countries (all the site serves when null), DefaultCountry.

// GET
var editor = await _addressEditor.BuildAsync(customerAddress, options);
model.AddressEditor = await _shapeFactory.CreateAddressEditorAsync(editor, prefix: "Address");

// POST
var posted = new AddressEditorViewModel();
await TryUpdateModelAsync(posted, "Address");
var result = await _addressEditor.ValidateAsync(posted, ModelState, "Address", options);

A display driver can return Initialize<AddressEditorViewModel>("AddressEditor", ...) with its own prefix. CreateAddressDisplayAsync(address) gives the AddressDisplay shape. When the country changes, the editor script fetches ~/addresses/formats/{countryCode} (anonymous, cached an hour); without script the server re-lays the form on post. AddressEditorViewModel.HasAnyValue() tells a blank optional address from one started; AddressEditorViewModel.From(address) copies a stored Address into an editor model (null for none) and ToAddress() turns a posted model back into an Address, for editors that bind the view model directly.

Country lists

ICountryService.GetCountriesAsync() returns the ISO list from the .NET runtime, or with Address Countries on, the published Country content items (seeded from the ISO list, unique codes in CountryPart's Code field, CountryIndex: one row per Country content item, declared .ExclusiveToDocumentType().SingleRecordPerDocument()), falling back to ISO while there are none. Read and write the code with GetCountryCode() and SetCountryCode(); CountryCodeUtility normalizes codes. Managed countries decide which countries are offered and their names, not how addresses are written. Taxation stores codes, so toggling either feature never orphans tax data.

Address suggestions

public interface IAddressAutocompleteProvider
{
    Task<bool> IsAvailableAsync(CancellationToken cancellationToken = default);

    Task<IReadOnlyList<AddressSuggestion>> SuggestAsync(AddressSuggestionRequest request, CancellationToken cancellationToken = default);

    Task<Address> ResolveAsync(string suggestionId, string sessionToken, CancellationToken cancellationToken = default);
}

Register your provider as a scoped IAddressAutocompleteProvider in your own feature; the first that IsAvailableAsync answers. It runs only on the server, so keys never reach the browser. AddressSuggestionRequest has Text, Country, SessionToken and Language.

| Endpoint | Answers |

| --- | --- |

| GET addresses/suggest?q=&country=&session= | [{ "id", "text" }]; q under 3 or over 120 characters answers [] |

| GET addresses/resolve?id=&session= | The address parts, or 404 |

Both answer 404 with no provider and 429 over the limit. They are in the address-autocomplete rate-limit group (AddressConstants.AutocompleteRateLimitGroup); with OrchardCore.RateLimits on, a built-in limit allows 60 calls a minute per visitor IP, and a Group policy can add stricter limits (see orchardcore-rate-limits). Behind a proxy, enable Orchard Core's Reverse Proxy Configuration. The editor shows suggestions only when data-suggest-url is set, which happens only when a provider is available.

Google Places

GooglePlacesAddressAutocompleteProvider calls the Places API (New) with the named HttpClient GooglePlaces (5-second timeout). The key is set under Settings > Address suggestions (group address-suggestions, permission ManageAddressSuggestions) and kept protected in GooglePlacesSettings.ApiKey (purpose CrestApps.OrchardCore.Addresses.GooglePlaces.ApiKey); it is never shown again, a blank field keeps it, and Remove the key clears it. Restrict the key to the Places API (New). Errors and timeouts are logged and give no suggestions.

Permissions

| Permission | Allows | Default roles |

| --- | --- | --- |

| ManageAddressSuggestions | Settings > Address suggestions | Administrator |

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.