3

私はこれで少し困っています。

Andreas Öhlund はここで質問に答えましたが、彼のアドバイスを使用して動作させることができませんでした。

これが私のセットアップです:

public abstract class CommandHandler<T> : IHandleMessages<T>, IDomainReadRepository where T : Command
{
    public IDomainRepository DomainRepository { get; set; }

    protected abstract void OnProcess(T command);

    public TAggregate GetById<TAggregate>(Guid id) where TAggregate : IEventProvider, new()
    {
        return DomainRepository.GetById<TAggregate>(id);
    }

    public void Handle(T message)
    {
        OnProcess(message);
        // Domain repository will save.
    }
}

アイデアは、特定のコマンド ハンドラーが OnProcess メソッドをオーバーライドして処理を実行すると、DomainRepository がすべてを保存するというものです。

コンポーネントを登録した方法は次のとおりです。

public class EndpointConfig : IConfigureThisEndpoint, AsA_Server, IWantCustomInitialization
{
    public void Init()
    {
        Configure.With().DefiningCommandsAs(c => c.Namespace != null && c.Namespace.EndsWith("Commands"));
        Configure.Instance.DefaultBuilder().Configurer.ConfigureComponent<DomainRepository>(DependencyLifecycle.InstancePerCall);
        Configure.Instance.DefaultBuilder().Configurer.ConfigureComponent<EventStore.Sql.EventStore>(DependencyLifecycle.InstancePerCall);
        Configure.Instance.DefaultBuilder().Configurer.ConfigureComponent<MongoDbObjectSecurityDescriptorRepository>(DependencyLifecycle.InstancePerCall);
        Configure.Instance.DefaultBuilder().Configurer.ConfigureComponent<LocalTenantConfig>(DependencyLifecycle.InstancePerCall);
    }
}

これらは、DomainRepository によって使用されるチェーンのすべてのオブジェクトです。ただし、コマンドを受信すると、DomainRepository は null です。DomainRepository が必要とするオブジェクトを登録する行をコメント アウトすると、実際には作成に失敗したというエラーが表示されます (Autofac DependencyResolutionException)。

他のすべてのオブジェクトはコンストラクター注入を使用することに注意してください (それらは以前の既存のプロジェクトから取得されます)。パブリック プロパティ インジェクションを使用するように変更しようとしましたが、違いはありませんでした。

ここで私が間違っていることを誰かが指摘できれば幸いです。

4

1 に答える 1