Skip to content

[IMPROVE] Let us select private types #60

@Antoshidza

Description

@Antoshidza

It is powerful thing to have implementations private while still be able to assign instances through unity inspector. The whole idea of this is to let code be flexible and not know what particular implementations we use which is polymorphic thing. Sometimes we don't want to expose types for public use in code.

For example I have next implementation (code below):
Here I have some IStartup implementation which have injected fields which is why I don't want to serialize it. So I have IStartupConfig which just creates IStartup being just dumb serializable data container with one method. This way I perfectly can assign composition of IStartup modules through ScriptableObject with IStartupConfig[] array.
But RestoreCardEveryTurn.Config should be public and there is no case I want to use it from code, because again this class specifically for serialization in editor.

public class RestoreCardEveryTurn : IStartup
{
    [AddTypeMenu("GameRules/Restore Card Every Turn")]
    [Serializable]
    public class Config : IStartupConfig
    {
        [SerializeField] private int _restoreCount;
        
        public IStartup Construct() => new RestoreCardEveryTurn(_restoreCount);
    }
    
    [Inject] private readonly ITurn _turn;
    [Inject] private readonly ISelectAndRestoreCardService _selectAndRestoreCardService;
    
    private readonly int _restoreCount;
    private readonly CompositeDisposable _subs = new();

    bool IStartup.Persistent => true;

    public RestoreCardEveryTurn(in int restoreCount)
        => _restoreCount = restoreCount;

    UniTask IInitializable.Initialize()
    {
        _turn.NextTurn
            .SubscribeAsync(async _ =>
            {
                for (int i = 0; i < _restoreCount; i++)
                    await _selectAndRestoreCardService.TrySelectAndRestoreCard();
            })
            .AddTo(_subs);
        return UniTask.CompletedTask;
    }

    void IDisposable.Dispose() => _subs.Dispose();
}

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions