private void saveCodeActivity_Save_ExecuteCode(object sender, EventArgs e)
        {
            IInlineFunction function           = this.GetBinding <IInlineFunction>("Function");
            string          code               = this.GetBinding <string>("FunctionCode");
            List <string>   selectedAssemblies = this.GetBinding <List <string> >("SelectedAssemblies");

            using (TransactionScope transactionScope = TransactionsFacade.CreateNewScope())
            {
                IEnumerable <IInlineFunctionAssemblyReference> assemblyReferences =
                    DataFacade.GetData <IInlineFunctionAssemblyReference>(f => f.Function == function.Id).Evaluate();

                foreach (string selectedAssembly in selectedAssemblies)
                {
                    string name     = System.IO.Path.GetFileName(selectedAssembly).ToLowerInvariant();
                    string location = InlineFunctionHelper.GetAssemblyLocation(selectedAssembly).ToLowerInvariant();

                    if (assemblyReferences
                        .Any(f => (string.Compare(f.Name, name, StringComparison.InvariantCultureIgnoreCase) == 0) &&
                             (string.Compare(f.Location, location, StringComparison.InvariantCultureIgnoreCase) == 0)) == false)
                    {
                        IInlineFunctionAssemblyReference assemblyReference = DataFacade.BuildNew <IInlineFunctionAssemblyReference>();
                        assemblyReference.Id       = Guid.NewGuid();
                        assemblyReference.Function = function.Id;
                        assemblyReference.Name     = name;
                        assemblyReference.Location = location;

                        DataFacade.AddNew(assemblyReference);
                    }
                }


                foreach (IInlineFunctionAssemblyReference assemblyReference in assemblyReferences)
                {
                    string fullPath = InlineFunctionHelper.GetAssemblyFullPath(assemblyReference.Name, assemblyReference.Location);

                    if (selectedAssemblies.Any(f => string.Compare(f, fullPath, StringComparison.InvariantCultureIgnoreCase) == 0) == false)
                    {
                        DataFacade.Delete(assemblyReference);
                    }
                }


                IInlineFunction oldFunction = DataFacade.GetData <IInlineFunction>(f => f.Id == function.Id).Single();
                if ((oldFunction.Name != function.Name) || (oldFunction.Namespace != function.Namespace))
                {
                    InlineFunctionHelper.FunctionRenamed(function, oldFunction);
                }


                List <ManagedParameterDefinition> parameters = this.GetBinding <List <ManagedParameterDefinition> >("Parameters");
                ManagedParameterManager.Save(function.Id, parameters);

                DataFacade.Update(function);
                function.SetFunctionCode(code);

                transactionScope.Complete();
            }

            SetSaveStatus(true);

            UpdateTreeRefresher updateTreeRefresher = this.CreateUpdateTreeRefresher(this.EntityToken);

            updateTreeRefresher.PostRefreshMesseges(function.GetDataEntityToken());
        }
        private void finalizeCodeActivity_Finalize_ExecuteCode(object sender, EventArgs e)
        {
            IInlineFunction function = this.GetBinding <IInlineFunction>("NewFunction");

            function.UpdateCodePath();

            string selectedTemplate = this.GetBinding <string>("SelectedTemplate");

            string codeTemplate;

            switch (selectedTemplate)
            {
            case "clean":
                codeTemplate = _cleanTemplate;
                break;

            case "parameter":
                codeTemplate = _parameterTemplate;

                List <ManagedParameterDefinition> parameters = new List <ManagedParameterDefinition>();

                ManagedParameterDefinition parameter1 = new ManagedParameterDefinition();
                parameter1.Id       = Guid.NewGuid();
                parameter1.Name     = "myIntValue";
                parameter1.Label    = "myIntValue";
                parameter1.HelpText = "myIntValue";
                parameter1.Position = 0;
                parameter1.Type     = typeof(int);
                parameter1.TestValueFunctionMarkup = "<f:function xmlns:f=\"http://www.composite.net/ns/function/1.0\" name=\"Composite.Constant.Integer\"><f:param name=\"Constant\" value=\"0\" /></f:function>";
                parameter1.WidgetFunctionMarkup    = "<f:widgetfunction xmlns:f=\"http://www.composite.net/ns/function/1.0\" name=\"Composite.Widgets.Integer.TextBox\" label=\"myIntValue\" bindingsourcename=\"\"><f:helpdefinition xmlns:f=\"http://www.composite.net/ns/function/1.0\" helptext=\"myIntValue\" /></f:widgetfunction>";
                parameters.Add(parameter1);


                ManagedParameterDefinition parameter2 = new ManagedParameterDefinition();
                parameter2.Id       = Guid.NewGuid();
                parameter2.Name     = "myStringValue";
                parameter2.Label    = "myStringValue";
                parameter2.HelpText = "myStringValue";
                parameter2.Position = 1;
                parameter2.Type     = typeof(string);
                parameter2.TestValueFunctionMarkup = "<f:function xmlns:f=\"http://www.composite.net/ns/function/1.0\" name=\"Composite.Constant.String\"><f:param name=\"Constant\" value=\"Hello world!\" /></f:function>";
                parameter2.WidgetFunctionMarkup    = "<f:widgetfunction xmlns:f=\"http://www.composite.net/ns/function/1.0\" name=\"Composite.Widgets.String.TextBox\" label=\"myStringValue\" bindingsourcename=\"\"><f:helpdefinition xmlns:f=\"http://www.composite.net/ns/function/1.0\" helptext=\"myStringValue\" /></f:widgetfunction>";
                parameters.Add(parameter2);

                ManagedParameterManager.Save(function.Id, parameters);
                break;

            case "dataconnection":
                codeTemplate = _dataConnectionTemplate;
                break;

            default:
                throw new NotImplementedException();
            }

            string code = string.Format(codeTemplate, function.Namespace, InlineFunctionHelper.MethodClassContainerName, function.Name);

            code = code.Replace('и', '\t');

            using (TransactionScope transactionScope = TransactionsFacade.CreateNewScope())
            {
                foreach (string assemblyPath in InlineFunctionHelper.DefaultAssemblies)
                {
                    IInlineFunctionAssemblyReference reference = DataFacade.BuildNew <IInlineFunctionAssemblyReference>();
                    reference.Id       = Guid.NewGuid();
                    reference.Function = function.Id;
                    reference.Name     = System.IO.Path.GetFileName(assemblyPath);
                    reference.Location = InlineFunctionHelper.GetAssemblyLocation(assemblyPath);

                    DataFacade.AddNew(reference);
                }

                function.SetFunctionCode(code);

                function = DataFacade.AddNew(function);

                UserSettings.LastSpecifiedNamespace = function.Namespace;

                transactionScope.Complete();
            }

            this.CloseCurrentView();
            this.CreateAddNewTreeRefresher(this.EntityToken).PostRefreshMesseges(function.GetDataEntityToken());
            this.ExecuteWorklow(function.GetDataEntityToken(), WorkflowFacade.GetWorkflowType("Composite.Workflows.Plugins.Elements.ElementProviders.MethodBasedFunctionProviderElementProvider.EditInlineFunctionWorkflow"));
        }