Example #1
0
        /// <summary>
        /// Traverses the specified layer.
        /// </summary>
        /// <param name="layer">The layer.</param>
        /// <param name="Scope">The scope.</param>
        /// <param name="mode">The mode.</param>
        public void Traverse(SoftwareLayer layer, ReferenceScope Scope, ConfigurationMode mode)
        {
            // Stocke les éléments traités pour éviter les doublons
            List <Guid> guids = new List <Guid>();

            if (layer is InterfaceLayer)
            {
                foreach (ServiceContract contract in ((InterfaceLayer)layer).ServiceContracts)
                {
                    foreach (Implementation impl in Implementation.GetLinksToImplementations(contract))
                    {
                        if (mode.CheckConfigurationMode(impl.ConfigurationMode) &&
                            !guids.Contains(impl.ClassImplementation.Id))
                        {
                            _visitor.Accept(impl, impl.ClassImplementation);
                            guids.Add(impl.ClassImplementation.Id);
                        }
                    }
                }
            }
            else if (layer is Layer)
            {
                foreach (ClassImplementation clazz in ((Layer)layer).Classes)
                {
                    foreach (NamedElement service in clazz.ServicesUsed)
                    {
                        IList <ClassUsesOperations> externalServiceLinks =
                            ClassUsesOperations.GetLinksToServicesUsed(clazz);
                        foreach (ClassUsesOperations link in externalServiceLinks)
                        {
                            if (mode.CheckConfigurationMode(link.ConfigurationMode) && ((link.Scope & Scope) == Scope))
                            {
                                if (service is ExternalServiceContract)
                                {
                                    _visitor.Accept(link, (ExternalServiceContract)service);
                                }
                                else if (service is ServiceContract)
                                {
                                    _visitor.Accept(link, (ServiceContract)service);
                                }
                                else if (service is ClassImplementation)
                                {
                                    _visitor.Accept(link, (ClassImplementation)service);
                                }
                                else
                                {
                                    throw new Exception("Type not implemented");
                                }
                                guids.Add(service.Id);
                            }
                        }
                    }
                }
            }
        }
Example #2
0
        public void Execute()
        {
            if (!(CurrentElement is Layer))
            {
                return;
            }

            // Vérification si la génération de la factory est pertinente (cad qu'il existe des références à partir de cette couche)
            int   nb    = 0;
            Layer layer = CurrentElement as Layer;

            foreach (ClassImplementation clazz in layer.Classes)
            {
                foreach (ClassUsesOperations service in ClassUsesOperations.GetLinksToServicesUsed(clazz))
                {
                    if (!Context.Mode.CheckConfigurationMode(service.ConfigurationMode))
                    {
                        continue;
                    }

                    if (service.TargetService is ExternalServiceContract)
                    {
                        nb++;
                    }
                    else
                    {
                        foreach (Implementation impl in Implementation.GetLinksToImplementations((ServiceContract)service.TargetService))
                        {
                            if (Context.Mode.CheckConfigurationMode(impl.ConfigurationMode))
                            {
                                nb++;
                            }
                        }
                    }
                }
            }

            if (nb > 0)
            {
                CallT4Template(Context.Project, T4Template, (CandleElement)CurrentElement, OutputFileName);
            }
        }
Example #3
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, "*");
                }
            }
        }
Example #4
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);
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Execution du wizard
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="DSLFactory.Candle.SystemModel.Strategies.StrategyElementElementAddedEventArgs"/> instance containing the event data.</param>
        public void RunWizard(ModelElement sender, StrategyElementElementAddedEventArgs e)
        {
            CandleElement elem = e.ModelElement as CandleElement;

            txtRootName.Text = elem.RootName;

            if (elem is ServiceContract) // Ce cas est désactivé (voir selection du wizard)
            {
                _layer       = ((ServiceContract)e.ModelElement).Layer;
                txtName.Text =
                    StrategyManager.GetInstance(_layer.Store).NamingStrategy.CreateElementName(_layer, elem.RootName);
            }
            else
            {
                _layer       = ((ClassImplementation)e.ModelElement).Layer;
                _iLayer      = ((ClassImplementation)e.ModelElement).Layer.LayerPackage.InterfaceLayer;
                txtName.Text =
                    StrategyManager.GetInstance(_layer.Store).NamingStrategy.CreateElementName(_layer, elem.RootName);
                if (_iLayer == null)
                {
                    txtContractName.Visible = false;
                }
                else
                {
                    txtContractName.Text =
                        StrategyManager.GetInstance(_layer.Store).NamingStrategy.CreateElementName(_iLayer, txtName.Text);
                }
            }

            if (elem is ServiceContract || ((ClassImplementation)elem).Layer.LayerPackage.InterfaceLayer == null)
            {
                lblContractName.Visible = false;
                txtContractName.Visible = false;
                txtContractName.Text    = null;
            }

            lblHeader.Text = String.Format(lblHeader.Text, _layer.Name);
            groupBox1.Text = _layer.Namespace;

            if (!s_dontShow)
            {
                e.UserCancel = (ShowDialog() == DialogResult.Cancel);
                if (e.UserCancel)
                {
                    return;
                }
                s_dontShow = ckDontShow.Checked;
            }

            // Ici on force les noms des classes donc on ne veut pas que la régle basée sur la modification
            // du RootName s'execute. On l'indique dans le contexte de la transaction
            if (
                !elem.Store.TransactionManager.CurrentTransaction.TopLevelTransaction.Context.ContextInfo.ContainsKey(
                    "CustomizableElementChangeRule_Enabled"))
            {
                elem.Store.TransactionManager.CurrentTransaction.TopLevelTransaction.Context.ContextInfo.Add(
                    "CustomizableElementChangeRule_Enabled", false);
            }
            elem.Name = txtName.Text;

            using (Transaction transaction = elem.Store.TransactionManager.BeginTransaction("Set root name"))
            {
                // Force la transaction pour que la règle s'execute tout de suite et qu'on puisse
                // forcer le nom ensuite
                elem.RootName = txtRootName.Text;
                transaction.Commit();
            }

            // Si c'est une classe, on essaye de créer son interface
            ClassImplementation clazz = elem as ClassImplementation;

            if (clazz != null && _iLayer != null && !String.IsNullOrEmpty(txtContractName.Text))
            {
                if (clazz.Contract == null)
                {
                    // On regarde si l'interface n'existe pas
                    clazz.Contract =
                        _iLayer.ServiceContracts.Find(
                            delegate(ServiceContract c) { return(c.Name == txtContractName.Text); });

                    if (clazz.Contract == null)
                    {
                        clazz.Contract          = new ServiceContract(clazz.Store);
                        clazz.Contract.RootName = txtRootName.Text;
                        clazz.Layer.LayerPackage.InterfaceLayer.ServiceContracts.Add(clazz.Contract);
                        UnplacedModelHelper.RegisterNewModel(clazz.Store, clazz.Contract);

                        // Si la classe courante utilise un seul contract, on le recopie
                        IList <ClassUsesOperations> links = ClassUsesOperations.GetLinksToServicesUsed(clazz);
                        if (links.Count == 1)
                        {
                            ServiceContract contract = links[0].TargetService as ServiceContract;
                            if (contract != null)
                            {
                                TypeWithOperations.CopyOperations(contract, clazz.Contract);
                            }
                            else
                            {
                                ExternalServiceContract externalContract =
                                    links[0].TargetService as ExternalServiceContract;
                                if (externalContract != null)
                                {
                                    TypeWithOperations.CopyOperations(externalContract.ReferencedServiceContract,
                                                                      clazz.Contract);
                                }
                            }
                        }
                    }
                }

                using (Transaction transaction = elem.Store.TransactionManager.BeginTransaction("Set root name"))
                {
                    // Force la transaction pour que la règle s'execute tout de suite et qu'on puisse
                    // forcer le nom ensuite
                    clazz.Contract.RootName = elem.RootName;
                    transaction.Commit();
                }

                if (clazz.Contract.Name != txtContractName.Text)
                {
                    clazz.Contract.Name = txtContractName.Text;
                }
            }

            e.CancelBubble = true;
        }