private void DiscoverWhens(AggregateSchema type) { foreach (var whenMethod in GetWhensMethods(type.Type)) { var cmdType = whenMethod.GetParameters()[1].ParameterType; var category = ServiceConventions.GetCategoryFromNamespace(cmdType.Namespace); if (category != type.Category) { throw new CategoryMismatchByNamespaceException(cmdType, type.Type); } Type[] events = Array.Empty <Type>(); if (typeof(IEvent).IsAssignableFrom(whenMethod.ReturnType) && whenMethod.ReturnType.IsClass) { events = new Type[] { whenMethod.ReturnType } } ; else { var attribute = whenMethod.GetCustomAttribute <EmittingEventsAttribute>(); if (attribute != null) { events = attribute.EventTypes; } } var commandSchema = new CommandSchema(cmdType, category, cmdType.IsPublic, false, null, events); _commands.Add(commandSchema); type.Commands.Add(commandSchema); _commandIndex.TryAdd(cmdType, type); } }
public void Discover(IEnumerable <Type> types) { var aggregateTypes = types .Where(t => typeof(IAggregate).IsAssignableFrom(t) && !t.IsAbstract) .ToArray(); foreach (var type in aggregateTypes) { var metadata = new AggregateSchema(type); _aggregates.Add(type, metadata); DiscoverWhens(metadata); DiscoverGivens(metadata); } var events = types.Where(x => typeof(IEvent).IsAssignableFrom(x) && !x.IsAbstract) .ToArray(); foreach (var eventType in events) { if (!_eventIndex.ContainsKey(eventType)) { string category = ServiceConventions.GetCategoryFromNamespace(eventType.Namespace); var aggregate = _aggregates.Values.FirstOrDefault(x => x.Category == category); if (aggregate != null) { var isOwnedByAggregate = aggregate.Type.Namespace == eventType.Namespace; AggregateEventSchema es = new AggregateEventSchema(eventType, false, isOwnedByAggregate); aggregate.Events.Add(es); } } } var commands = types.Where(x => typeof(ICommand).IsAssignableFrom(x) && !x.IsAbstract).ToArray(); foreach (var command in commands.Where(x => !_commandIndex.ContainsKey(x))) { var customHandler = typeof(ICommandHandler <>).MakeGenericType(command); var handler = types.FirstOrDefault(x => customHandler.IsAssignableFrom(x)); if (handler != null) { _commands.Add(new CommandSchema(command, ServiceConventions.GetCategoryFromNamespace(command.Namespace), command.IsPublic, true, handler)); } else { Debug.WriteLine($"Found a dangling command: {command.FullName}"); } } }
private void DiscoverGivens(AggregateSchema type) { foreach (var givenMethod in GetGivensMethods(type.Type)) { var eventType = givenMethod.GetParameters()[1].ParameterType; if (givenMethod.ReturnType != type.StateType) { throw new InvalidAggregateSchemaException(type.Type, eventType); } var isOwnedByAggregate = eventType.Namespace == type.Type.Namespace; AggregateEventSchema es = new AggregateEventSchema(eventType, true, isOwnedByAggregate); type.Events.Add(es); _events.Add(es); _eventIndex.TryAdd(eventType, type); } }