public AttributeBasedEntityHandlerFactory( Type initialClass, AnySupport anySupport, ServiceDescriptor descriptor, Func <IEventSourcedContext, object> factory = null ) : base(initialClass, anySupport, factory, anySupport.ResolveServiceDescriptor(descriptor)) { BehaviorResolver = new ReflectionCacheBehaviorResolver(() => ResolvedMethods); InitCache(); }
protected internal AttributeBasedCrdtHandlerFactory(Type initialClass, AnySupport anySupport, ServiceDescriptor descriptor, Func <ICrdtEntityCreationContext, object> factory = null) : base(initialClass, anySupport, factory, anySupport.ResolveServiceDescriptor(descriptor)) { var allMethods = ReflectionHelper.GetAllDeclaredMethods(initialClass); var handlers = allMethods.Where(x => x.GetCustomAttribute <CommandHandlerAttribute>() != null) .Select(method => { var attribute = method.GetCustomAttribute <CommandHandlerAttribute>(); var name = string.IsNullOrEmpty(attribute.Name) ? ReflectionHelper.GetCapitalizedName(method) : attribute.Name; if (!ResolvedMethods.TryGetValue(name, out var serviceMethod)) { throw new CloudStateException( $"Command handler method [{method.Name}] for command [{name}] found, but the service has no command by that name."); } return(method, serviceMethod); }).ToArray(); CommandHandlers = GetHandlers <ICommandContext>(handlers, false); StreamedCommandHandlers = GetHandlers <IStreamedCommandContext <ICommandContext> >(handlers, true); }
public void ResolveServiceDescriptor() { var methods = AnySupport.ResolveServiceDescriptor( ShoppingcartReflection.Descriptor.FindTypeByName <ServiceDescriptor>("ShoppingCart") ); Assert.Equal(3, methods.Count); var method = methods["AddItem"].Method; Assert.Equal(AddLineItem.Descriptor.FullName, method.InputType.FullName); Assert.Equal(typeof(AddLineItem), method.InputType.ClrType); var bytes = AddLineItem.ToByteString(); Assert.Equal(AddLineItem, method.InputType.Parser.ParseFrom(bytes)); Assert.Equal(Empty.Descriptor.FullName, method.OutputType.FullName); Assert.Equal(typeof(Empty), method.OutputType.ClrType); var defaultEmpty = new Empty(); bytes = defaultEmpty.ToByteString(); Assert.Equal(defaultEmpty, method.OutputType.Parser.ParseFrom(bytes)); }