Dev GuideAPI Reference
Dev GuideAPI ReferenceUser GuideGitHubNuGetDev CommunitySubmit a ticketLog In
GitHubNuGetDev CommunitySubmit a ticket

MessageProvider

Describes MessageProvider in Optimizely Configured Commerce.

Code Sample: MessageProvider

[DependencyName(nameof(ValidateContext))]
public sealed class ValidateContext : HandlerBase<AddSessionParameter, AddSessionResult>
{
    private readonly Lazy<IAuthenticationService> authenticationService;
 
    private readonly Lazy<IUserProfileUtilities> userProfileUtilities;
 
    public ValidateContext(Lazy<IAuthenticationService> authenticationService, Lazy<IUserProfileUtilities> userProfileUtilities)
    {
        this.authenticationService = authenticationService;
        this.userProfileUtilities = userProfileUtilities;
    }
 
    public override int Order => 500;
 
    public override AddSessionResult Execute(IUnitOfWork unitOfWork, AddSessionParameter parameter, AddSessionResult result)
    {
        var userProfile = unitOfWork.GetTypedRepository<IUserProfileRepository>().GetByUserName(parameter.UserName);
 
        if (userProfile == null)
        {
            return this.CreateErrorServiceResult(result, SubCode.AccountServiceUserProfileNotFound, MessageProvider.Current.SignInInfo_UserNamePassword_Combination);
        }
 
        if (userProfile.IsDeactivated)
        {
            return this.CreateErrorServiceResult(result, SubCode.Deactivated, MessageProvider.Current.SignInInfo_User_IsDeactivated);
        }
 
        if (userProfile.IsPasswordChangeRequired)
        {
            return this.CreateErrorServiceResult(result, SubCode.PasswordExpired, MessageProvider.Current.SignInInfo_UserNamePassword_ChangeRequired);
        }
 
        if (this.authenticationService.Value.IsLockedOut(userProfile.UserName))
        {
            return this.CreateErrorServiceResult(result, SubCode.LockedOut, MessageProvider.Current.SignInInfo_UserLockedOut);
        }
 
        if (!this.userProfileUtilities.Value.IsAllowedForWebsite(userProfile, SiteContext.Current.WebsiteDto))
        {
            return this.CreateErrorServiceResult(result, SubCode.AccountServiceUserNotAllowedForWebsite, MessageProvider.Current.SignInInfo_UserNotAllowedForWebsite);
        }
 
        result.UserProfile = userProfile;
 
        return this.NextHandler.Execute(unitOfWork, parameter, result);
    }
}

The example below uses a number of site messages from the MessageProvider to indicate if adding a new session is currently valid. The messages are accessed using the current message provider.

Example

The site message text. If the site message is not found, the default value is returned.

Returns

  • name – The name of the site message to return.
  • defaultValue – The text to use for the site message if it cannot be found.
  • description – The description to use for the site message if it cannot be found.

Parameters

virtual string GetMessage(string name, string defaultValue, string description = "")

Finds and returns a site message by name. If the site message is not found, the default value and description are used to create the missing site message.

GetMessage(string, string, string)

  • messageProvider – The message provider to set as the current message provider. This message provider will be used by the rest of the application.

Parameters

static void SetCurrent(MessageProvider messageProvider)

Sets the current message provider. This allows the default message provider can be changed.

SetCurrent(MessageProvider)

Methods

In addition, the MessageProvider includes a property for each of a large number of site messages. For example, the MessageProvider.SignInInfo_UserNamePassword_Combination property returns a message stating that the credentials used to log in are incorrect. All of the property names attempt to describe the message content.

static MessageProvider Current { get; } = new MessageProvider

Holds the current message provider. This is the property you should use to access the current message provider and any of the site messages. The default provider is MessageProvider.

Current

Properties

The provider for accessing Site Messages configured in the Admin Console. Using this provider also gives the additional benefit of making unit testing much easier. Before your test harness runs, you can change the current MessageProvider to a provider that does not access the database to return site messages. This makes testing easier because it removes a dependency on an external resource.