Esempio n. 1
0
        void IStrategyCodeInjector.OnMetaModelUpdate(CodeInjectionContext context)
        {
            // Reference pour le ServiceLocator
            if (context.CurrentElement is Layer)
            {
                Layer layer = context.CurrentElement as Layer;
                foreach (ClassImplementation clazz in layer.Classes)
                {
                    foreach (ClassUsesOperations service in ClassUsesOperations.GetLinksToServicesUsed(clazz))
                    {
                        if (!context.GenerationContext.Mode.CheckConfigurationMode(service.ConfigurationMode) || service.TargetService is ExternalServiceContract)
                        {
                            continue;
                        }

                        foreach (Implementation impl in Implementation.GetLinksToImplementations((ServiceContract)service.TargetService))
                        {
                            if (context.GenerationContext.Mode.CheckConfigurationMode(impl.ConfigurationMode))
                            {
                                if (CheckAllHandler(impl.ClassImplementation, impl.ClassImplementation.Layer))
                                {
                                    layer.AddReferenceToService(entLibId, "EnterpriseLibrary", new VersionInfo(3, 1, 0, 0), "Microsoft.Practices.EnterpriseLibrary.PolicyInjection");
                                    return;
                                }
                            }
                        }
                    }
                }
            }

            if (context.CurrentElement is ClassImplementation)
            {
                ClassImplementation clazz = context.CurrentElement as ClassImplementation;
                if (clazz.Contract == null)
                {
                    return;
                }

                bool flag = false;
                foreach (Operation op in clazz.Contract.Operations)
                {
                    IPIABHandler handler = CheckHandler <LogCallHandler>(LogCallHandlerProperty, op, clazz);
                    if (handler != null && handler.Enabled)
                    {
                        ((ClassImplementation)context.CurrentElement).Layer.AddReferenceToService(entLibId, "EnterpriseLibrary", new VersionInfo(3, 1, 0, 0), "Microsoft.Practices.EnterpriseLibrary.Logging", ReferenceScope.Runtime, "*");
                        flag = true;
                    }

                    handler = CheckHandler <CacheCallHandler>(CacheCallHandlerProperty, op, clazz);
                    if (handler != null && handler.Enabled)
                    {
                        ((ClassImplementation)context.CurrentElement).Layer.AddReferenceToService(entLibId, "EnterpriseLibrary", new VersionInfo(3, 1, 0, 0), "Microsoft.Practices.EnterpriseLibrary.Caching", ReferenceScope.Runtime, "*");
                        flag = true;
                    }

                    // PerformanceCounterCallHandler
                    handler = CheckHandler <PerformanceCounterCallHandler>(PerformanceCounterCallHandlerProperty, op, clazz);
                    if (handler != null && handler.Enabled)
                    {
                        ((ClassImplementation)context.CurrentElement).Layer.AddReferenceToService(entLibId, "EnterpriseLibrary", new VersionInfo(3, 1, 0, 0), "Microsoft.Practices.EnterpriseLibrary.Common.Instrumentation", ReferenceScope.Runtime, "*");
                        flag = true;
                    }
                }

                if (flag)
                {
                    ((ClassImplementation)context.CurrentElement).Layer.AddReferenceToService(entLibId, "EnterpriseLibrary", new VersionInfo(3, 1, 0, 0), "Microsoft.Practices.EnterpriseLibrary.PolicyInjection");
                    ((ClassImplementation)context.CurrentElement).Layer.AddReferenceToService(entLibId, "EnterpriseLibrary", new VersionInfo(3, 1, 0, 0), "Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers", ReferenceScope.Runtime | ReferenceScope.Compilation, "*");
                }
            }
        }
Esempio n. 2
0
        void IStrategyCodeInjector.OnGenerateFunction(CodeInjectionContext context, DSLFactory.Candle.SystemModel.CodeGeneration.CodeModel.CandleCodeFunction function)
        {
            // Service factory
            if (context.CurrentElement is Layer)
            {
                // On part du principe que le template du servicelocator à généré une classe nommée ServiceLocator et
                // une méthode par création d'instance dont le nom commence par CreateInstanceOfxxxxxx
                if (function.Parent.Name == "ServiceLocatorBase" && function.Name.StartsWith("CreateInstanceOf"))
                {
                    string currentName = function.Name.Substring("CreateInstanceOf".Length);
                    foreach (ClassImplementation clazz in ((Layer)context.CurrentElement).Classes)
                    {
                        foreach (ClassUsesOperations service in ClassUsesOperations.GetLinksToServicesUsed(clazz))
                        {
                            if (!context.GenerationContext.Mode.CheckConfigurationMode(service.ConfigurationMode) || service.TargetService is ExternalServiceContract)
                            {
                                continue;
                            }

                            ServiceContract contract = service.TargetService as ServiceContract;
                            if (contract == null)
                            {
                                continue;
                            }

                            foreach (Implementation impl in Implementation.GetLinksToImplementations(contract))
                            {
                                if (context.GenerationContext.Mode.CheckConfigurationMode(impl.ConfigurationMode) && impl.ClassImplementation.Name == currentName)
                                {
                                    if (impl.ClassImplementation.Contract != null)
                                    {
                                        foreach (Operation op in impl.ClassImplementation.Contract.Operations)
                                        {
                                            if (CheckAllHandler(op, impl.ClassImplementation))
                                            {
                                                function.ReplaceBody(
                                                    @"           string path = ConfigurationManager.AppSettings[key];
            Type t = Assembly.Load(path).GetType(""" + impl.ClassImplementation.FullName + @""");
            PropertyInfo p = typeof(Microsoft.Practices.EnterpriseLibrary.PolicyInjection.PolicyInjection).GetProperty(""DefaultPolicyInjector"", BindingFlags.Static | BindingFlags.NonPublic);
            Microsoft.Practices.EnterpriseLibrary.PolicyInjection.PolicyInjector pi = (Microsoft.Practices.EnterpriseLibrary.PolicyInjection.PolicyInjector)p.GetValue(null, null);
            return (" + contract.FullName + ")pi.Create(t, typeof(" + contract.FullName + @"));
");
                                                return;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            // Attribut
            if (context.CurrentElement is ClassImplementation)
            {
                ClassImplementation clazz = context.CurrentElement as ClassImplementation;
                if (clazz.Name + "Base" == function.Parent.Name || clazz.Name == function.Parent.Name)
                {
                    Operation op = function.FindOperationFromContract(clazz.Contract);
                    if (op != null)
                    {
                        // LogCallHandler
                        IPIABHandler handler = CheckHandler <LogCallHandler>(LogCallHandlerProperty, op, clazz);
                        if (handler != null && handler.Enabled)
                        {
                            handler.SetAttribute(context, function);
                        }


                        // CacheCallHandler
                        handler = CheckHandler <CacheCallHandler>(CacheCallHandlerProperty, op, clazz);
                        if (handler != null && handler.Enabled)
                        {
                            handler.SetAttribute(context, function);
                        }

                        // PerformanceCounterCallHandler
                        handler = CheckHandler <PerformanceCounterCallHandler>(PerformanceCounterCallHandlerProperty, op, clazz);
                        if (handler != null && handler.Enabled)
                        {
                            handler.SetAttribute(context, function);
                        }
                    }
                }
            }
        }