开发者

CommonDomain/EventStore Interface Fetching

开发者 https://www.devze.com 2023-04-06 15:47 出处:网络
I\'d like to access my aggregate root by an interface it implements: repository.GetById[[IMyInterface]](id);

I'd like to access my aggregate root by an interface it implements:

 repository.GetById[[IMyInterface]](id);    

What do I need to tell CommonDomain or EventStore to accomplish this? I believe my IConstructAggregates receives the Implementation Type of the aggregate that stored the events. Do i need to just keep my own map of ids.

For example, say I have these agg roots :

class AggRoot1 : IInterface1, IInterface2 {}
class AggRoot2 : IInterface1, IInterface2 {}

I alr开发者_JAVA百科eady saved an aggregate1 instance having 'idFromAggRoot1'. Now I want to fetch like so:

repository.GetById<IInterface1>(idFromAggRoot1);

How can I know what I should create later since there are two implementors of IInterface1? AggRoot1? AggRoot2? IInterface1? Activator would bomb here so I know I need to implement IConstructAggregates but wonder if there is some other descriptor to tell me what the original commit agg root type was.


The IConstructAggregates.Build() method can be implemented to return whatever type you need.

In Common.AggregateFactory the default implementation creates an instance of the Aggregate via Activator.CreateInstance(type) as IAggregate.

An own implementation might look like this:

public class MyAggregateFactory : IConstructAggregates
{
    public IAggregate Build(Type type, Guid id, IMemento snapshot)
    {
        if (type == typeof(IMyInterface))
            return new MyAggregate();
        else
            return Activator.CreateInstance(type) as IAggregate;
    }
}

Edit: The aggregate type is being stored in the event message's header as EventMessage.Header[EventStoreRepository.AggregateTypeHeader]

0

精彩评论

暂无评论...
验证码 换一张
取 消