예제 #1
0
        public void BuildOperations()
        {
            var orchestrationPrefix       = GetGenericTypeNamePrefix(typeof(IOrchestration <>));
            var readOrchestrationPrefix   = GetGenericTypeNamePrefix(typeof(IReadOrchestration <>));
            var atLeastOnceActivityPrefix = GetGenericTypeNamePrefix(typeof(IAtLeastOnceActivity <>));
            var atMostOnceActivityPrefix  = GetGenericTypeNamePrefix(typeof(IAtMostOnceActivity <>));
            var updatePrefix = GetGenericTypeNamePrefix(typeof(IUpdate <,>));
            var readPrefix   = GetGenericTypeNamePrefix(typeof(IRead <,>));

            var startups = new List <IStartupOrchestration>();

            foreach (var o in operations)
            {
                Type spec = null;
                foreach (var i in o.GetInterfaces())
                {
                    var name = GetGenericTypeNamePrefix(i);
                    if (name == orchestrationPrefix || name == readOrchestrationPrefix ||
                        name == atLeastOnceActivityPrefix || name == atMostOnceActivityPrefix ||
                        name == updatePrefix || name == readPrefix)
                    {
                        if (spec != null)
                        {
                            throw new BuilderException($"invalid operation {o.Name} : multiple specifiers : {spec.FullName}, {i.FullName}");
                        }
                        spec = i;
                    }
                }

                if (spec == null)
                {
                    throw new BuilderException($"invalid operation {o.Name} : missing interface IActivity, IOrchestration, or ITransaction");
                }

                var specname = GetGenericTypeNamePrefix(spec);

                if (specname == orchestrationPrefix)
                {
                    var returntype = spec.GenericTypeArguments[0];
                    var m          = typeof(IServiceBuilder).GetMethod(nameof(IServiceBuilder.DefineOrchestration));
                    var mg         = m.MakeGenericMethod(o, returntype);
                    mg.Invoke(serviceBuilder, new object[0]);

                    if (typeof(IStartupOrchestration).IsAssignableFrom(o))
                    {
                        serviceBuilder.OnFirstStart((IOrchestration)Activator.CreateInstance(o));
                    }
                }
                else if (specname == readOrchestrationPrefix)
                {
                    var returntype = spec.GenericTypeArguments[0];
                    var m          = typeof(IServiceBuilder).GetMethod(nameof(IServiceBuilder.DefineReadOrchestration));
                    var mg         = m.MakeGenericMethod(o, returntype);
                    mg.Invoke(serviceBuilder, new object[0]);
                }
                else if (specname == atLeastOnceActivityPrefix)
                {
                    var returntype = spec.GenericTypeArguments[0];
                    var m          = typeof(IServiceBuilder).GetMethod(nameof(IServiceBuilder.DefineAtLeastOnceActivity));
                    var mg         = m.MakeGenericMethod(o, returntype);
                    mg.Invoke(serviceBuilder, new object[0]);
                }
                else if (specname == atMostOnceActivityPrefix)
                {
                    var returntype = spec.GenericTypeArguments[0];
                    var m          = typeof(IServiceBuilder).GetMethod(nameof(IServiceBuilder.DefineAtMostOnceActivity));
                    var mg         = m.MakeGenericMethod(o, returntype);
                    mg.Invoke(serviceBuilder, new object[0]);
                }
                else if (specname == updatePrefix)
                {
                    var statetype  = spec.GenericTypeArguments[0];
                    var returntype = spec.GenericTypeArguments[1];
                    var m          = typeof(IServiceBuilder).GetMethod(nameof(IServiceBuilder.DefineUpdate));
                    var mg         = m.MakeGenericMethod(statetype, o, returntype);
                    mg.Invoke(serviceBuilder, new object[0]);
                }
                else if (specname == readPrefix)
                {
                    var statetype  = spec.GenericTypeArguments[0];
                    var returntype = spec.GenericTypeArguments[1];
                    var m          = typeof(IServiceBuilder).GetMethod(nameof(IServiceBuilder.DefineRead));
                    var mg         = m.MakeGenericMethod(statetype, o, returntype);
                    mg.Invoke(serviceBuilder, new object[0]);
                }
            }
        }