CommonDomain/EventStore Interface Fetching

730 views Asked by At

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 already 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.

1

There are 1 answers

2
Dennis Traub On BEST ANSWER

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]