public INamedReference GenerateFactoryMethod(
            IServiceSetupRecord setup,
            Dictionary <string, INamedReference> ctorParamValueFactoryFields,
            INamedReference valueFactoryField,
            IClassBuilder locatorBuilder,
            INamedReference previousPluginMethod)
        {
            var implementingType = setup.ImplementingType ?? setup.InterfaceType;

            var methodName = $"Factory{TypeNameHelper.GetTypeMark(setup.InterfaceType)}_{Guid.NewGuid():N}";

            var method = locatorBuilder.HasMethod(methodName).WithModifier("private").Returns(setup.InterfaceType);

            method.Body.Write("return ").InvokeConstructor(implementingType,
                                                           invocation =>
            {
                foreach (var constructorParameter in setup.PreferredConstructor.GetParameters())
                {
                    INamedReference factoryField;
                    if (ctorParamValueFactoryFields.TryGetValue(constructorParameter.Name, out factoryField))
                    {
                        invocation.WithParam(b => b.Write("(").Write(constructorParameter.ParameterType).Write(")").Write(factoryField).Write("(this)"));
                    }
                    else
                    {
                        invocation.WithParam(b => b.Write("(").Write(constructorParameter.ParameterType).Write(")").Write("Get(typeof(").Write(constructorParameter.ParameterType).Write("))"));
                    }
                }
            }).EndStatement();

            return(method);
        }
Beispiel #2
0
            public INamedReference GenerateFactoryMethod(IServiceSetupRecord setup, Dictionary <string, INamedReference> ctorParamValueFactoryFields, INamedReference valueFactoryField, IClassBuilder locatorBuilder, INamedReference previousPluginMethod)
            {
                var fm = locatorBuilder.HasMethod($"ControllerIndexFactory_{Guid.NewGuid():N}").Returns <ControllerIndex>().WithModifier("private");

                fm.Body.Write("return new ").Write(typeof(ControllerIndex)).Write("(this.GetRoboapiControllerIndexFactory())").EndStatement();

                return(fm);
            }
Beispiel #3
0
        public INamedReference GenerateFactoryMethod(
            IServiceSetupRecord setup,
            Dictionary <string, INamedReference> ctorParamValueFactoryFields,
            INamedReference valueFactoryField,
            IClassBuilder locatorBuilder,
            INamedReference previousPluginMethod)
        {
            if (!IsApplicable(setup))
            {
                return(previousPluginMethod);
            }

            var behavior = setup.Behaviors.OfType <EntityBehavior>().Last();

            var entityClass =
                locatorBuilder.HasNestedClass($"{TypeNameHelper.GetTypeMark(setup.InterfaceType)}_Impl_{Guid.NewGuid():N}")
                .WithModifier("private")
                .WithModifier("sealed")
                .Implements <IEntity>()
                .Implements(setup.InterfaceType);

            var locatorField = entityClass.HasField <IServiceLocator>();

            var ctor         = entityClass.WithConstructor().WithModifier("public");
            var locatorParam = ctor.WithParam <IServiceLocator>("locator");

            ctor.Body.Assign(locatorField, a => a.Write(locatorParam)).EndStatement();
            ctor.Body.Write("IsDirty = true").EndStatement();

            var entityProperties = ImplementProperties(entityClass, setup.InterfaceType, behavior).ToList();

            ImplementIsDirtyProperty(entityClass);
            var pkProperty = ImplementReadPrimaryKeyAndGetPkProperty(entityClass, behavior, setup.InterfaceType);

            ImplementGetSaveMethodTypeMethod(entityClass, pkProperty);
            ImplementGetValuesMethod(entityClass, entityProperties, pkProperty);
            ImplementReadSelfMethod(entityClass, locatorField, entityProperties);
            ImplementReferenceEntityProperties(entityClass, setup.InterfaceType, m_setup.EntityNamingConvention);
            ImplementDbEntityNameProperty(entityClass, setup.InterfaceType);
            ImplementPkTypeProperty(entityClass, pkProperty);
            ImplementIsPkAuto(entityClass, pkProperty);

            var collectionMethod = locatorBuilder.HasMethod("GetEntitiesCollection").Returns(typeof(IEnumerable <Type>));

            collectionMethod.Body.Write("yield return typeof(")
            .Write(setup.InterfaceType)
            .Write(")")
            .EndStatement()
            .NewLine();

            var factoryMethod = locatorBuilder.HasMethod($"{TypeNameHelper.GetTypeMark(setup.InterfaceType)}_{Guid.NewGuid():N}").Returns(setup.InterfaceType);

            factoryMethod.Body.Write("return ")
            .InvokeConstructor(entityClass, inv => inv.WithParam("this"))
            .EndStatement();

            return(factoryMethod);
        }
Beispiel #4
0
        //protected abstract void
        public bool IsApplicable(IServiceSetupRecord setup)
        {
            if (setup.HasValueFactory || !setup.InterfaceType.IsInterface)
            {
                return(false);
            }

            return(IsApplicable(setup.InterfaceType, setup.ImplementingType));
        }
Beispiel #5
0
            public INamedReference GenerateFactoryMethod(
                IServiceSetupRecord setup,
                Dictionary <string, INamedReference> ctorParamValueFactoryFields,
                INamedReference valueFactoryField,
                IClassBuilder locatorBuilder,
                INamedReference previousPluginMethod)
            {
                var method = locatorBuilder.HasMethod("error");

                method.Body.Write("cannot compile this");

                return(method);
            }
Beispiel #6
0
        public INamedReference GenerateFactoryMethod(
            IServiceSetupRecord setup,
            Dictionary <string, INamedReference> ctorParamValueFactoryFields,
            INamedReference valueFactoryField,
            IClassBuilder locatorBuilder,
            INamedReference previousPluginMethod)
        {
            var implClass =
                locatorBuilder.HasNestedClass(
                    $"{TypeNameHelper.GetTypeMark(this.GetType())}_implements_{TypeNameHelper.GetTypeMark(setup.InterfaceType)}_{Guid.NewGuid():N}")
                .Implements(setup.InterfaceType)
                .WithModifier("private")
                .WithModifier("sealed");

            var locatorField = implClass.HasField <IServiceLocator>();
            var locatorParam = implClass.WithConstructor().WithModifier("public").WithParam("locator", typeof(IServiceLocator));

            implClass.WithConstructor().Body.Assign(locatorField, a => a.Write(locatorParam));

            foreach (var property in setup.InterfaceType.GetProperties())
            {
                if (!ImplementProperty(implClass, property, locatorField))
                {
                    DefaultImplementProperty(implClass, property);
                }
            }

            foreach (var method in setup.InterfaceType.GetMethods())
            {
                if (method.IsSpecialName)
                {
                    continue;
                }

                if (!ImplementMethod(implClass, method, locatorField))
                {
                    DefaultImplementMethod(implClass, method);
                }
            }

            var factoryMethod =
                locatorBuilder.HasMethod($"factory_{TypeNameHelper.GetTypeMark(setup.InterfaceType)}_{Guid.NewGuid():N}").Returns(setup.InterfaceType).WithModifier("private");

            factoryMethod.Body.Write(" return ")
            .InvokeConstructor(implClass, i => i.WithParam(p => p.Write("this")))
            .EndStatement();

            return(factoryMethod);
        }
Beispiel #7
0
        public INamedReference GenerateFactoryMethod(
            IServiceSetupRecord setup,
            Dictionary <string, INamedReference> ctorParamValueFactoryFields,
            INamedReference valueFactoryField,
            IClassBuilder locatorBuilder,
            INamedReference previousPluginMethod)
        {
            var methodName = $"Factory{TypeNameHelper.GetTypeMark(setup.InterfaceType)}_{Guid.NewGuid():N}";

            var method = locatorBuilder.HasMethod(methodName).WithModifier("private").Returns(setup.InterfaceType);

            method.Body.Write("return ").Write("(").Write(setup.InterfaceType).Write(")").Write(valueFactoryField).Write("(this)").EndStatement();

            return(method);
        }
Beispiel #8
0
        public INamedReference GenerateFactoryMethod(
            IServiceSetupRecord setup,
            Dictionary <string, INamedReference> ctorParamValueFactoryFields,
            INamedReference valueFactoryField,
            IClassBuilder locatorBuilder,
            INamedReference previousPluginMethod)
        {
            var collectionMethod = locatorBuilder.HasMethod("GetCollectionItems");

            collectionMethod.Body.If(
                condition => condition.Write("collectionType == typeof(").Write(CollectionType).Write(")"),
                then => then.Write("yield return Get(typeof(").Write(setup.InterfaceType).Write("))").EndStatement()).NewLine();

            return(previousPluginMethod);
        }
Beispiel #9
0
        public INamedReference GenerateFactoryMethod(
            IServiceSetupRecord setup,
            Dictionary <string, INamedReference> ctorParamValueFactoryFields,
            INamedReference valueFactoryField,
            IClassBuilder locatorBuilder,
            INamedReference previousPluginMethod)
        {
            var factory = locatorBuilder.HasMethod("GetEntityCollectorInstance").Returns(typeof(IEntityCollector));

            factory.WithModifier("private");

            factory.Body.Write("return new ")
            .Write(typeof(EntityCollector))
            .Write("(System.Linq.Enumerable.ToList(this.GetEntitiesCollection()))")
            .EndStatement();

            return(factory);
        }
Beispiel #10
0
        public static T GetBehavior <T>(this IServiceSetupRecord record, T defaultBehavior = null) where T : class, IBehavior
        {
            T lastBehavior = null;

            var behaviors = record.Behaviors.OfType <T>();

            foreach (var currentBehavior in behaviors)
            {
                if (lastBehavior != null)
                {
                    currentBehavior.InheritPreviousBehavior(lastBehavior);
                }

                lastBehavior = currentBehavior;
            }

            return(lastBehavior ?? defaultBehavior);
        }
Beispiel #11
0
        public INamedReference GenerateFactoryMethod(
            IServiceSetupRecord setup,
            Dictionary <string, INamedReference> ctorParamValueFactoryFields,
            INamedReference valueFactoryField,
            IClassBuilder locatorBuilder,
            INamedReference previousPluginMethod)
        {
            if (setup.GetBehavior(s_defaultBehavior).AlwaysNewInstance)
            {
                return(previousPluginMethod);
            }

            var field = locatorBuilder.HasField(setup.InterfaceType).WithModifier("private");

            var fMethod = locatorBuilder.HasMethod($"Singleton_{TypeNameHelper.GetTypeMark(setup.InterfaceType)}_{Guid.NewGuid():N}").Returns(setup.InterfaceType);

            fMethod.Body.Write(" return ")
            .LazyReadOrAssign(field, a => a.Invoke(previousPluginMethod, inv => { }))
            .EndStatement();

            return(fMethod);
        }
Beispiel #12
0
        public INamedReference GenerateFactoryMethod(
            IServiceSetupRecord setup,
            Dictionary <string, INamedReference> ctorParamValueFactoryFields,
            INamedReference valueFactoryField,
            IClassBuilder locatorBuilder,
            INamedReference previousPluginMethod)
        {
            var behavior = setup.Behaviors.OfType <ControllerBehavior>().FirstOrDefault();

            if (behavior == null)
            {
                return(previousPluginMethod);
            }

            var indexFactory =
                locatorBuilder.HasMethod("GetRoboapiControllerIndexFactory")
                .Returns <IEnumerable <KeyValuePair <string, Type> > >()
                .WithModifier("private");

            indexFactory.Body.Write("yield return new ")
            .Write(typeof(KeyValuePair <string, Type>))
            .Write("(")
            .String(behavior.ControllerName)
            .Write(", ")
            .Typeof(setup.ImplementingType)
            .Write(")")
            .EndStatement();

            var builder = Activator.CreateInstance(behavior.ProxyBuilderType) as ControllerProxyBuilder;

            var proxy = builder.BuildProxyClass(locatorBuilder, setup.ImplementingType, behavior.CallBuilderType);

            var factoryMethod =
                locatorBuilder.HasMethod($"factory{setup.ImplementingType.Name}").Returns(setup.ImplementingType);

            factoryMethod.Body.Write("return new ").Write(proxy).Write("(this)").EndStatement();

            return(factoryMethod);
        }
Beispiel #13
0
        public INamedReference GenerateFactoryMethod(
            IServiceSetupRecord setup,
            Dictionary <string, INamedReference> ctorParamValueFactoryFields,
            INamedReference valueFactoryField,
            IClassBuilder locatorBuilder,
            INamedReference previousPluginMethod)
        {
            if (!setup.GetBehavior(s_defaultBehavior).Dispose)
            {
                return(previousPluginMethod);
            }

            var method = locatorBuilder.HasMethod($"Dispose{TypeNameHelper.GetTypeMark(setup.InterfaceType)}_{Guid.NewGuid():N}")
                         .Returns(setup.InterfaceType);

            method.Body.Write("return ")
            .Invoke("TryRegisterDisposable", invocation => invocation.WithParam(p => p.Invoke(previousPluginMethod,
                                                                                              inv => { })))
            .EndStatement();

            return(method);
        }
Beispiel #14
0
 public bool IsApplicable(IServiceSetupRecord setup)
 {
     return(setup.InterfaceType == typeof(IEntityCollector));
 }
Beispiel #15
0
 public bool IsApplicable(IServiceSetupRecord setup)
 {
     return(setup.HasValueFactory);
 }
Beispiel #16
0
 public bool IsApplicable(IServiceSetupRecord setup)
 {
     return(((setup.InterfaceType != null) && CollectionType.IsAssignableFrom(setup.InterfaceType)) ||
            ((setup.ImplementingType != null) && CollectionType.IsAssignableFrom(setup.ImplementingType)));
 }
Beispiel #17
0
        public bool IsApplicable(IServiceSetupRecord setup)
        {
            Func <Type, bool> canBeDisposable = t => (t == null) || typeof(IDisposable).IsAssignableFrom(t);

            return(setup.HasValueFactory || canBeDisposable(setup.ImplementingType) || canBeDisposable(setup.InterfaceType));
        }
Beispiel #18
0
 public bool IsApplicable(IServiceSetupRecord setup)
 {
     return(true);
 }
 public void BindTo(IServiceSetupRecord setup)
 {
 }
Beispiel #20
0
 public bool IsApplicable(IServiceSetupRecord setup)
 {
     return(setup.InterfaceType == typeof(ControllerIndex));
 }
 public bool IsApplicable(IServiceSetupRecord setup)
 {
     return
         (!(setup.HasValueFactory || (setup.ImplementingType == null) || !setup.ImplementingType.IsClass ||
            setup.ImplementingType.IsAbstract));
 }
Beispiel #22
0
 public bool IsApplicable(IServiceSetupRecord setup)
 {
     return(setup.GetBehavior <EntityBehavior>() != null);
 }
Beispiel #23
0
 public bool IsApplicable(IServiceSetupRecord setup)
 {
     return(setup.Behaviors.OfType <ControllerBehavior>().Any());
 }