1

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.

Ruben Bartelink
  • 59,778
  • 26
  • 187
  • 249
SonOfNun
  • 957
  • 7
  • 11
  • Please explain to me what the pratical use of such a construct is? Are you thinking in terms of "roles"? – Yves Reynhout Sep 27 '11 at 11:03
  • Just saw this - The practical use is coding to interface on my Aggregate Roots without requiring client knowledge of the actual implementation it is executing against. – SonOfNun Nov 11 '11 at 05:56
  • I acknowledge that decoupling might be a good thing, but can you give an example of two aggregate root types that would implement the same interface but not share the same implementation? – Yves Reynhout Nov 13 '11 at 18:05

1 Answers1

2

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]

Dennis Traub
  • 50,557
  • 7
  • 93
  • 108
  • Thank you but still doesn't answer my question. I have edited my post to provide more details – SonOfNun Sep 22 '11 at 13:36
  • That helps, thank you. It looks like I'll need to extend the EventStoreRepository to fetch the stream and read the first event to get that metadata. I have put an issue on the repo to provide this in the IConstructAggregates. – SonOfNun Sep 22 '11 at 14:19