Repository: microsoft/PowerToys
File: src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/TopLevelViewModel.cs (C#)

Code before:
        // UpdateHotkey();
        // UpdateTags();
    }

    private void Item_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)
    {
        if (!string.IsNullOrEmpty(e.PropertyName))
        {
            PropChanged?.Invoke(this, new PropChangedEventArgs(e.PropertyName));

            if (e.PropertyName == "IsInitialized")
            {
                GenerateId();

                UpdateAlias();
                UpdateHotkey();
                UpdateTags();
            }
        }
    }

    private void Save() => SettingsModel.SaveSettings(_settings);

    private void HandleChangeAlias()
    {
        SetAlias(Alias);
        Save();
    }

    public void SetAlias(CommandAlias? newAlias)
    {
        _serviceProvider.GetService<AliasManager>()!.UpdateAlias(Id, newAlias);
        UpdateAlias();
        UpdateTags();
    }

    private void UpdateAlias()
    {
        // Add tags for the alias, if we have one.
        var aliases = _serviceProvider.GetService<AliasManager>();
        if (aliases != null)
        {
            Alias = aliases.AliasFromId(Id);
        }
    }

    private void UpdateHotkey()
    {
        var hotkey = _settings.CommandHotkeys.Where(hk => hk.CommandId == Id).FirstOrDefault();
        if (hotkey != null)
        {

Review comment (bug):
Given all operation can be failed (Especially Save(), which I assume it will be saving to setting file), and I will suggest we need to rollback to previous Alias still being use?

The current alias handling pattern is not perfect, given it will directly change "current alias object".
Moreover, we can try to "Save" first, since for failure situation (no matter it failed at save, or failed at serviceManager), there will be less code to handle rollback to previous alias.

Code after:
        // UpdateHotkey();
        // UpdateTags();
    }

    private void Item_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)
    {
        if (!string.IsNullOrEmpty(e.PropertyName))
        {
            PropChanged?.Invoke(this, new PropChangedEventArgs(e.PropertyName));

            if (e.PropertyName == "IsInitialized")
            {
                GenerateId();

                FetchAliasFromAliasManager();
                UpdateHotkey();
                UpdateTags();
            }
        }
    }

    private void Save() => SettingsModel.SaveSettings(_settings);

    private void HandleChangeAlias()
    {
        SetAlias();
        Save();
    }

    public void SetAlias()
    {
        var commandAlias = Alias is null
                ? null
                : new CommandAlias(Alias.Alias, Alias.CommandId, Alias.IsDirect);

        _serviceProvider.GetService<AliasManager>()!.UpdateAlias(Id, commandAlias);
        UpdateTags();
    }

    private void FetchAliasFromAliasManager()
    {
        var am = _serviceProvider.GetService<AliasManager>();
        if (am != null)
        {
            var commandAlias = am.AliasFromId(Id);
            if (commandAlias is not null)
            {
                // Decouple from the alias manager alias object
                Alias = new CommandAlias(commandAlias.Alias, commandAlias.CommandId, commandAlias.IsDirect);
            }
        }