Exemplo n.º 1
0
        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);
            }
        }
Exemplo n.º 2
0
        public void Discover(IEnumerable <Type> types)
        {
            ProjectionSchemaRegister helper = new ProjectionSchemaRegister();

            helper.Discover(types);

            foreach (var t in types.Where(x => typeof(IEvent).IsAssignableFrom(x) && !x.IsAbstract))
            {
                var eventSchema = new EventSchema(t, ServiceConventions.GetCategoryFromNamespace(t.Namespace));
                var findByEvent = helper.FindByEvent(t);
                eventSchema.AppendProjections(findByEvent);
                _events.Add(eventSchema);
            }
        }
Exemplo n.º 3
0
        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}");
                }
            }
        }
Exemplo n.º 4
0
        public void Discover(IEnumerable <Type> types)
        {
            var projectionTypes = types
                                  .Where(t => typeof(IProjection).IsAssignableFrom(t) && !t.IsAbstract)
                                  .ToArray();

            foreach (var type in projectionTypes)
            {
                var projectionPartitionerType = typeof(IProjectionStreamPartitioner <>).MakeGenericType(type);
                var partitionerTypes          = types.Where(x => projectionPartitionerType.IsAssignableFrom(x) && !x.IsAbstract)
                                                .ToArray();
                ProjectionSchema m = new ProjectionSchema(type, ServiceConventions.GetCategoryFromNamespace(type.Namespace), partitionerTypes);

                var specificProjectionType = type.GetInterface(nameof(IProjection) + "`1");
                if (specificProjectionType != null)
                {
                    m.ModelType = specificProjectionType.GetGenericArguments()[0];
                    _modelIndex.Add(m.ModelType, m);
                }

                _metadata.Add(m);

                foreach (var givenMethods in GetGivenMethods(type))
                {
                    var eventType = givenMethods.GetParameters()[2].ParameterType;

                    var list = _event2ProjectionType.GetOrAdd(eventType, (t) => new List <ProjectionSchema>());
                    list.Add(m);

                    m.AddEventType(eventType);
                }

                m.MakeReadonly();
            }

            Events = _event2ProjectionType.Keys.ToArray();
        }
Exemplo n.º 5
0
 public string GetCategory(Type type)
 {
     return(ServiceConventions.GetCategoryFromNamespace(type.Namespace));
 }