Exemplo n.º 1
0
        public static ParameterTemplate AddParameter(this MethodTemplate methodTemplate, TypeTemplate type, string name, ICodeFragment defaultValue = null)
        {
            var parameter = new ParameterTemplate(type, name, defaultValue);

            methodTemplate.Parameters.Add(parameter);
            return(parameter);
        }
Exemplo n.º 2
0
 public ParameterTemplate AddParameter(object value, Column column)
 {
     string name = _schemaProvider.NameParameter("p" + Interlocked.Increment(ref _number) + _parameterSuffix);
     var parameterTemplate = new ParameterTemplate(name, column);
     _parameters.Add(parameterTemplate, value);
     return parameterTemplate;
 }
Exemplo n.º 3
0
 public CommandTemplate(Func<IDbCommand, IDbParameterFactory> createGetParameterFactoryFunc, string commandText, ParameterTemplate[] parameterNames, Dictionary<string, int> index)
 {
     _createGetParameterFactoryFunc = createGetParameterFactoryFunc;
     _commandText = commandText;
     _parameters = parameterNames;
     _index = index;
 }
Exemplo n.º 4
0
 public ParameterTemplate AddParameter(string name, DbType dbType, object value)
 {
     name = _schemaProvider.NameParameter(name + _parameterSuffix);
     var parameterTemplate = new ParameterTemplate(name, dbType, 0);
     _parameters.Add(parameterTemplate, value);
     return parameterTemplate;
 }
Exemplo n.º 5
0
        /// <summary>
        /// Определяет, является ли для данного параметра родительская секция табличной (если она не существует, то не является)
        /// </summary>
        /// <param name="parameterTemplate"></param>
        /// <returns></returns>
        public static bool IsParentSectionTable(this ParameterTemplate parameterTemplate)
        {
            var result = false;

            if (parameterTemplate.ParentSection != null)
            {
                result = parameterTemplate.ParentSection.blnGrid;
            }

            return(result);
        }
Exemplo n.º 6
0
        public ParamGenerator()
        {
            var assembly     = System.Reflection.Assembly.GetExecutingAssembly();
            var resourceName = "LogicAppTemplate.Templates.paramTemplate.json";

            using (Stream stream = assembly.GetManifestResourceStream(resourceName))
                using (StreamReader reader = new StreamReader(stream))
                {
                    paramTemplate = JsonConvert.DeserializeObject <ParameterTemplate>(reader.ReadToEnd());
                }
        }
Exemplo n.º 7
0
        public virtual void Write(ICodeFragment fragment, IOutputCache output)
        {
            ParameterTemplate template = (ParameterTemplate)fragment;

            output.Add(template.Name)
            .Add(": ")
            .Add(template.Type);
            if (template.DefaultValue != null)
            {
                output.Add(" = ")
                .Add(template.DefaultValue);
            }
        }
Exemplo n.º 8
0
        public override void Write(ICodeFragment fragment, IOutputCache output)
        {
            ParameterTemplate template = (ParameterTemplate)fragment;

            output.Add(template.Name)
            .If(template.IsOptional).Add("?").EndIf()
            .Add(": ")
            .Add(template.Type);
            if (template.DefaultValue != null)
            {
                output.Add(" = ")
                .Add(template.DefaultValue);
            }
        }
Exemplo n.º 9
0
        public virtual void Write(ICodeFragment fragment, IOutputCache output)
        {
            ParameterTemplate template = (ParameterTemplate)fragment;

            template.Attributes.ForEach(x => x.IsInline = true);
            output.Add(template.Attributes)
            .Add(template.Type)
            .Add(" ")
            .Add(template.Name);
            if (template.DefaultValue != null)
            {
                output.Add(" = ")
                .Add(template.DefaultValue);
            }
        }
Exemplo n.º 10
0
 private static void RewriteSqlEqualityToInClause(IDbCommand command, ParameterTemplate parameterTemplate, StringBuilder builder)
 {
     if (command.CommandText.Contains("!= " + parameterTemplate.Name))
     {
         command.CommandText = command.CommandText.Replace("!= " + parameterTemplate.Name,
                                                           "NOT IN (" +
                                                           builder.ToString().Substring(1) +
                                                           ")");
     }
     else
     {
         command.CommandText = command.CommandText.Replace("= " + parameterTemplate.Name,
                                                           "IN (" +
                                                           builder.ToString().Substring(1) +
                                                           ")");
     }
 }
Exemplo n.º 11
0
        internal FlexLabelItem(ParameterTemplate row, bool isParameterInSection)
            : base(row)
        {
            Height = row.intHeight;
            Width  = isParameterInSection
                        ? row.intWidth
                        : row.intLabelSize;
            if (Width == 0)
            {
                Width = row.intWidth;
            }
            Top   = row.intTop;
            Left  = row.intLeft;
            Order = row.intOrder;

            FontSize = ReportSettings.DefaultFontSize;
            Text     = string.IsNullOrEmpty(row.NationalName) ? row.DefaultName : row.NationalName;
        }
Exemplo n.º 12
0
        public void Add
            (ParameterTemplate parameterTemplateRow,
            ActivityParameter activityParametersRow)
        {
            Add(parameterTemplateRow, false);

            var parametersItem = new FlexLabelItem(parameterTemplateRow, activityParametersRow)
            {
                IsParameterValue = true
            };
            var nodeAp = new FlexNodeReport(this, parametersItem);

            //нод, который отвечает за контрол, выводится с фиксированной высотой
            nodeAp.DataItem.Height = DefaultControlHeight;
            if (nodeAp.DataItem.Height > parametersItem.Height)
            {
                nodeAp.DataItem.Height = parametersItem.Height;
            }
            ChildList.Add(nodeAp);
        }
Exemplo n.º 13
0
 private static IEnumerable<IDbDataParameter> CreateParameters(IDbCommand command, ParameterTemplate parameterTemplate, object value)
 {
     var range = value as IRange;
     if (range != null)
     {
         yield return CreateParameter(command, parameterTemplate, range.Start, "_start");
         yield return CreateParameter(command, parameterTemplate, range.End, "_end");
         CommandBuilder.SetBetweenInCommandText(command, parameterTemplate.Name);
     }
     else
     {
         var list = value as IEnumerable;
         if (list != null)
         {
             var builder = new StringBuilder();
             var array = list.Cast<object>().ToArray();
             for (int i = 0; i < array.Length; i++)
             {
                 builder.AppendFormat(",{0}_{1}", parameterTemplate.Name, i);
                 yield return CreateParameter(command, parameterTemplate, array[i], "_" + i);
             }
             if (command.CommandText.Contains("!= " + parameterTemplate.Name))
             {
                 command.CommandText = command.CommandText.Replace("!= " + parameterTemplate.Name,
                                                                   "NOT IN (" + builder.ToString().Substring(1) +
                                                                   ")");
             }
             else
             {
                 command.CommandText = command.CommandText.Replace("= " + parameterTemplate.Name,
                                                                   "IN (" + builder.ToString().Substring(1) + ")");
             }
         }
         else
         {
             yield return CreateParameter(command, parameterTemplate, value);
         }
     }
     
 }
Exemplo n.º 14
0
        public static bool IsParameterInSectionTable(ParameterTemplate parameterTemplate, Template template)
        {
            //TODO предусмотреть ситуацию, когда имеет место динамический или ressurected параметр
            var result = false;

            if (parameterTemplate != null)
            {
                if (parameterTemplate.idfsSection.HasValue)
                {
                    var section = template.SectionTemplatesLookup.FirstOrDefault(c => c.idfsSection == parameterTemplate.idfsSection);
                    if (section != null)
                    {
                        result = section.blnGrid;
                    }
                }
            }
            else
            {
                result = true;
            }
            return(result);
        }
Exemplo n.º 15
0
 private IEnumerable<IDbDataParameter> CreateParameters(IDbCommand command, ParameterTemplate parameterTemplate, object value)
 {
     if (value == null || TypeHelper.IsKnownType(value.GetType()) || parameterTemplate.DbType == DbType.Binary)
     {
         yield return CreateParameter(command, parameterTemplate, value);
     }
     else
     {
         var range = value as IRange;
         if (range != null)
         {
             yield return CreateParameter(command, parameterTemplate, range.Start, "_start");
             yield return CreateParameter(command, parameterTemplate, range.End, "_end");
             CommandBuilder.SetBetweenInCommandText(command, parameterTemplate.Name);
         }
         else
         {
             var list = value as IEnumerable;
             if (list != null)
             {
                 var builder = new StringBuilder();
                 var array = list.Cast<object>().ToArray();
                 for (int i = 0; i < array.Length; i++)
                 {
                     builder.AppendFormat(",{0}_{1}", parameterTemplate.Name, i);
                     yield return CreateParameter(command, parameterTemplate, array[i], "_" + i);
                 }
                 RewriteSqlEqualityToInClause(command, parameterTemplate, builder);
             }
             else
             {
                 yield return CreateParameter(command, parameterTemplate, value);
             }
         }
     }
 }
Exemplo n.º 16
0
        public virtual void Write(AngularWriteConfiguration configuration, List <ITransferObject> transferObjects, List <FileTemplate> files)
        {
            Logger.Trace("Generate angular service for ASP.net controller...");
            if (!configuration.Language.IsTypeScript())
            {
                throw new InvalidOperationException($"Can not generate service for ASP.net Controller for language {configuration.Language?.Name ?? "Empty"}");
            }
            string httpClient       = configuration.Service.HttpClient?.Name ?? "HttpClient";
            string httpClientImport = configuration.Service.HttpClient?.Import ?? "@angular/common/http";

            foreach (HttpServiceTransferObject controller in transferObjects.OfType <HttpServiceTransferObject>())
            {
                Dictionary <HttpServiceActionParameterTransferObject, ParameterTemplate> mapping = new Dictionary <HttpServiceActionParameterTransferObject, ParameterTemplate>();
                string        controllerName = controller.Name.TrimEnd("Controller");
                ClassTemplate classTemplate  = files.AddFile(configuration.Service.RelativePath, configuration.AddHeader)
                                               .AddNamespace(string.Empty)
                                               .AddClass(configuration.Service.Name ?? controllerName + "Service")
                                               .FormatName(configuration)
                                               .WithUsing(httpClient, httpClientImport)
                                               .WithUsing("Injectable", "@angular/core")
                                               .WithUsing("Observable", "rxjs")
                                               .WithUsing("Subject", "rxjs")
                                               .WithAttribute("Injectable", Code.AnonymousObject().WithProperty("providedIn", Code.String("root")));
                FieldTemplate httpField       = classTemplate.AddField("http", Code.Type(httpClient)).Readonly().FormatName(configuration);
                FieldTemplate serviceUrlField = classTemplate.AddField("serviceUrl", Code.Type("string")).Public().FormatName(configuration).Default(Code.String(string.Empty));
                classTemplate.AddConstructor().WithParameter(Code.Type(httpClient), "http")
                .WithCode(Code.This().Field(httpField).Assign(Code.Local("http")).Close());
                string relativeModelPath = FileSystem.RelativeTo(configuration.Model?.RelativePath ?? ".", configuration.Service.RelativePath);
                foreach (HttpServiceActionTransferObject action in controller.Actions)
                {
                    ICodeFragment errorCode = Code.Lambda("error", Code.Local("subject").Method("error", Code.Local("error")));
                    this.MapType(controller.Language, configuration.Language, action.ReturnType);
                    TypeTemplate returnType = action.ReturnType.ToTemplate();
                    this.AddUsing(action.ReturnType, classTemplate, configuration, relativeModelPath);
                    MethodTemplate methodTemplate = classTemplate.AddMethod(action.Name, Code.Generic("Observable", returnType))
                                                    .FormatName(configuration);
                    foreach (HttpServiceActionParameterTransferObject parameter in action.Parameters)
                    {
                        this.MapType(controller.Language, configuration.Language, parameter.Type);
                        this.AddUsing(parameter.Type, classTemplate, configuration, relativeModelPath);
                        ParameterTemplate parameterTemplate = methodTemplate.AddParameter(parameter.Type.ToTemplate(), parameter.Name).FormatName(configuration);
                        mapping.Add(parameter, parameterTemplate);
                    }
                    methodTemplate.AddParameter(Code.Type("{}"), "httpOptions?");
                    TypeTemplate subjectType = Code.Generic("Subject", returnType);
                    methodTemplate.WithCode(Code.Declare(subjectType, "subject", Code.New(subjectType)));
                    string uri = ("/" + (controller.Route?.Replace("[controller]", controllerName.ToLower()).TrimEnd('/') ?? controllerName) + "/" + action.Route?.Replace("[action]", action.Name.ToLower())).TrimEnd('/');

                    List <HttpServiceActionParameterTransferObject> inlineParameters    = action.Parameters.Where(x => !x.FromBody && x.Inline).OrderBy(x => x.InlineIndex).ToList();
                    List <HttpServiceActionParameterTransferObject> urlParameters       = action.Parameters.Where(x => !x.FromBody && !x.Inline && x.AppendName).ToList();
                    List <HttpServiceActionParameterTransferObject> urlDirectParameters = action.Parameters.Where(x => !x.FromBody && !x.Inline && !x.AppendName).ToList();
                    uri = urlParameters.Count > 0 ? $"{uri}?{urlParameters.First().Name}=" : urlDirectParameters.Count > 0 ? $"{uri}?" : uri;
                    MultilineCodeFragment code            = Code.Multiline();
                    DeclareTemplate       declareTemplate = null;
                    bool hasReturnType = returnType.Name != "void";
                    bool isPrimitive   = this.IsPrimitive(returnType);
                    if (returnType.Name == "Array")
                    {
                        TypeTemplate type = ((GenericTypeTemplate)returnType).Types[0];
                        declareTemplate = Code.Declare(returnType, "list", Code.TypeScript("[]")).Constant();
                        code.AddLine(declareTemplate)
                        .AddLine(Code.TypeScript("for (const entry of <[]>result)").StartBlock())
                        .AddLine(Code.Local(declareTemplate).Method("push", isPrimitive ? (ICodeFragment)Code.Cast(type, Code.Local("entry")) : Code.New(type, Code.Local("entry"))).Close())
                        .AddLine(Code.TypeScript("").EndBlock());
                    }
                    else if (hasReturnType)
                    {
                        declareTemplate = Code.Declare(returnType, "model", isPrimitive ? (ICodeFragment)Code.Cast(returnType, Code.Local("result")) : Code.New(returnType, Code.Local("result"))).Constant();
                        code.AddLine(declareTemplate);
                    }
                    code.AddLine(Code.Local("subject").Method("next").WithParameter(declareTemplate.ToLocal()).Close())
                    .AddLine(Code.Local("subject").Method("complete").Close());
                    ChainedCodeFragment parameterUrl = Code.This().Field(serviceUrlField);
                    if (inlineParameters.Count == 0)
                    {
                        parameterUrl = parameterUrl.Append(Code.String(uri));
                    }
                    foreach (HttpServiceActionParameterTransferObject parameter in inlineParameters)
                    {
                        string[] chunks = uri.Split(new [] { $"{{{parameter.Name}}}" }, StringSplitOptions.RemoveEmptyEntries);
                        parameterUrl = parameterUrl.Append(Code.String(chunks[0])).Append(Code.Local(parameter.Name));
                        uri          = chunks.Length == 1 ? string.Empty : chunks[1];
                    }
                    bool isFirst = true;
                    foreach (HttpServiceActionParameterTransferObject parameter in urlDirectParameters)
                    {
                        if (isFirst)
                        {
                            isFirst      = false;
                            parameterUrl = parameterUrl.Append(Code.Local(parameter.Name));
                        }
                        else
                        {
                            parameterUrl = parameterUrl.Append(Code.String("&")).Append(Code.Local(parameter.Name));
                        }
                    }
                    foreach (HttpServiceActionParameterTransferObject parameter in urlParameters)
                    {
                        if (isFirst)
                        {
                            isFirst      = false;
                            parameterUrl = parameterUrl.Append(Code.Local(mapping[parameter]));
                        }
                        else
                        {
                            parameterUrl = parameterUrl.Append(Code.String($"&{parameter.Name}=")).Append(Code.Local(mapping[parameter]));
                        }
                    }

                    methodTemplate.WithCode(
                        Code.This()
                        .Field(httpField)
                        .Method(action.Type.ToString().ToLowerInvariant(),
                                parameterUrl,
                                action.RequireBodyParameter ? Code.Local(action.Parameters.Single(x => x.FromBody).Name) : null,
                                Code.Local("httpOptions")
                                )
                        .Method("subscribe", Code.Lambda(hasReturnType ? "result" : null, code), errorCode).Close()
                        );
                    methodTemplate.WithCode(Code.Return(Code.Local("subject")));
                }
            }
        }
Exemplo n.º 17
0
 public static LambdaTemplate Lambda(this Code _, ParameterTemplate parameter, ICodeFragment code)
 {
     return(new LambdaTemplate(parameter.Yield(), code));
 }
Exemplo n.º 18
0
        private static IDbDataParameter CreateSingleParameter(IDbParameterFactory parameterFactory, object value, ParameterTemplate template)    
        {
            if (template.Column != null) return CreateSingleParameter(parameterFactory, value, template.Name, template.Column);

            var parameter = parameterFactory.CreateParameter(template.Name, template.DbType, template.MaxLength);
            parameter.Value = CommandHelper.FixObjectType(value);
            return parameter;
        }
Exemplo n.º 19
0
 private static IEnumerable<IDbDataParameter> CreateParameterComplex(IDbParameterFactory parameterFactory, ParameterTemplate template, object value, IDbCommand command)
 {
     if (template.Column != null && template.Column.IsBinary)
     {
         yield return CreateSingleParameter(parameterFactory, value, template.Name, template.Column);
     }
     else
     {
         var str = value as string;
         if (str != null)
         {
             yield return CreateSingleParameter(parameterFactory, value, template.Name, template.Column);
         }
         else
         {
             var range = value as IRange;
             if (range != null)
             {
                 yield return
                     CreateSingleParameter(parameterFactory, range.Start, template.Name + "_start", template.Column);
                 yield return CreateSingleParameter(parameterFactory, range.End, template.Name + "_end", template.Column);
                 SetBetweenInCommandText(command, template.Name);
             }
             else
             {
                 var list = value as IEnumerable;
                 if (list != null)
                 {
                     var builder = new StringBuilder();
                     var array = list.Cast<object>().ToArray();
                     for (int i = 0; i < array.Length; i++)
                     {
                         builder.AppendFormat(",{0}_{1}", template.Name, i);
                         yield return
                             CreateSingleParameter(parameterFactory, array[i], template.Name + "_" + i, template.Column);
                     }
                     if (command.CommandText.Contains("!= " + template.Name))
                     {
                         command.CommandText = command.CommandText.Replace("!= " + template.Name,
                                                                           "NOT IN (" +
                                                                           builder.ToString().Substring(1) + ")");
                     }
                     else
                     {
                         command.CommandText = command.CommandText.Replace("= " + template.Name,
                                                                           "IN (" +
                                                                           builder.ToString().Substring(1) +
                                                                           ")");
                     }
                 }
                 else
                 {
                     yield return CreateSingleParameter(parameterFactory, value, template);
                 }
             }
         }
     }
 }
Exemplo n.º 20
0
 public static ParameterTemplate Optional(this ParameterTemplate template)
 {
     template.IsOptional = true;
     return(template);
 }
Exemplo n.º 21
0
 private static IDbDataParameter CreateParameter(IDbCommand command, ParameterTemplate parameterTemplate, object value, string suffix = "")
 {
     var parameter = command.CreateParameter();
     parameter.ParameterName = parameterTemplate.Name + suffix;
     parameter.DbType = parameterTemplate.DbType;
     parameter.Value = value;
     return parameter;
 }
Exemplo n.º 22
0
 public static LocalVariableTemplate Local(this Code _, ParameterTemplate type)
 {
     return(new LocalVariableTemplate(type.Name));
 }
        public virtual void Write(EntityFrameworkWriteConfiguration configuration, List <ITransferObject> transferObjects, List <FileTemplate> files)
        {
            foreach (EntityFrameworkWriteRepositoryConfiguration repositoryConfiguration in configuration.Repositories)
            {
                EntityTransferObject entity = transferObjects.OfType <EntityTransferObject>().FirstOrDefault(x => x.Name == repositoryConfiguration.Entity)
                                              .AssertIsNotNull(nameof(repositoryConfiguration.Entity), $"Entity {repositoryConfiguration.Entity} not found. Ensure it is read before.");

                ClassTemplate repository = files.AddFile(configuration.RelativePath, configuration.AddHeader)
                                           .AddNamespace(repositoryConfiguration.Namespace ?? configuration.Namespace)
                                           .AddClass(repositoryConfiguration.Name ?? entity.Name + "Repository")
                                           .FormatName(configuration)
                                           .WithUsing("System.Collections.Generic")
                                           .WithUsing("System.Linq");
                if (configuration.IsCore)
                {
                    repository.WithUsing("Microsoft.EntityFrameworkCore");
                }
                else
                {
                    repository.WithUsing("System.Data.Entity");
                }
                if (!string.IsNullOrEmpty(configuration.Namespace) && !string.IsNullOrEmpty(repositoryConfiguration.Namespace) && configuration.Namespace != repositoryConfiguration.Namespace)
                {
                    repository.AddUsing(configuration.Namespace);
                }

                configuration.Usings.ForEach(x => repository.AddUsing(x));
                repositoryConfiguration.Usings.ForEach(x => repository.AddUsing(x));

                TypeTemplate modelType = entity.Model.ToTemplate();

                FieldTemplate dataSetField     = repository.AddField("dataSet", Code.Generic("DbSet", modelType)).Readonly();
                FieldTemplate dataContextField = repository.AddField("dataContext", Code.Type("DataContext")).Readonly();

                TypeTemplate        dataContextType      = Code.Type("DataContext");
                ConstructorTemplate constructor          = repository.AddConstructor();
                ParameterTemplate   dataContextParameter = constructor.AddParameter(dataContextType, "dataContext", Code.Null());
                constructor.Code.AddLine(Code.This().Field(dataContextField).Assign(Code.NullCoalescing(Code.Local(dataContextParameter), Code.New(dataContextType))).Close())
                .AddLine(Code.This().Field(dataSetField).Assign(Code.This().Field(dataContextField).GenericMethod("Set", modelType)).Close());

                repository.AddMethod("Get", Code.Generic("IQueryable", modelType))
                .Code.AddLine(Code.Return(Code.This().Field(dataSetField)));

                repository.AddMethod("Get", modelType)
                .WithParameter(Code.Type("params object[]"), "keys")
                .Code.AddLine(Code.Return(Code.This().Field(dataSetField).Method("Find", Code.Local("keys"))));

                if (configuration.IsCore)
                {
                    repository.AddMethod("Add", modelType)
                    .WithParameter(modelType, "entity")
                    .Code.AddLine(Code.Declare(modelType, "result", Code.This().Field(dataSetField).Method("Add", Code.Local("entity")).Property("Entity")))
                    .AddLine(Code.This().Field(dataContextField).Method("SaveChanges").Close())
                    .AddLine(Code.Return(Code.Local("result")));

                    repository.AddMethod("Add", Code.Generic("IEnumerable", modelType))
                    .WithParameter(Code.Generic("IEnumerable", modelType), "entities")
                    .Code.AddLine(Code.Declare(Code.Generic("IEnumerable", modelType), "result", Code.Local("entities").Method("Select", Code.Lambda("x", Code.This().Field(dataSetField).Method("Add", Code.Local("x")).Property("Entity"))).Method("ToList")))
                    .AddLine(Code.This().Field(dataContextField).Method("SaveChanges").Close())
                    .AddLine(Code.Return(Code.Local("result")));

                    repository.AddMethod("Update", modelType)
                    .WithParameter(modelType, "entity")
                    .Code.AddLine(Code.Declare(modelType, "result", Code.This().Field(dataSetField).Method("Update", Code.Local("entity")).Property("Entity")))
                    .AddLine(Code.This().Field(dataContextField).Method("SaveChanges").Close())
                    .AddLine(Code.Return(Code.Local("result")));

                    repository.AddMethod("Update", Code.Generic("IEnumerable", modelType))
                    .WithParameter(Code.Generic("IEnumerable", modelType), "entities")
                    .Code.AddLine(Code.Declare(Code.Generic("IEnumerable", modelType), "result", Code.Local("entities").Method("Select", Code.Lambda("x", Code.This().Field(dataSetField).Method("Update", Code.Local("x")).Property("Entity"))).Method("ToList")))
                    .AddLine(Code.This().Field(dataContextField).Method("SaveChanges").Close())
                    .AddLine(Code.Return(Code.Local("result")));
                }
                else
                {
                    repository.AddMethod("Add", modelType)
                    .WithParameter(modelType, "entity")
                    .Code.AddLine(Code.Declare(modelType, "result", Code.This().Field(dataSetField).Method("Add", Code.Local("entity"))))
                    .AddLine(Code.This().Field(dataContextField).Method("SaveChanges").Close())
                    .AddLine(Code.Return(Code.Local("result")));

                    repository.AddMethod("Add", Code.Generic("IEnumerable", modelType))
                    .WithParameter(Code.Generic("IEnumerable", modelType), "entities")
                    .Code.AddLine(Code.Declare(Code.Generic("IEnumerable", modelType), "result", Code.Local("entities").Method("Select", Code.Lambda("x", Code.This().Field(dataSetField).Method("Add", Code.Local("x")))).Method("ToList")))
                    .AddLine(Code.This().Field(dataContextField).Method("SaveChanges").Close())
                    .AddLine(Code.Return(Code.Local("result")));

                    repository.WithUsing("System.Data.Entity.Migrations")
                    .AddMethod("Update", modelType)
                    .WithParameter(modelType, "entity")
                    .Code.AddLine(Code.This().Field(dataSetField).Method("AddOrUpdate", Code.Local("entity")).Close())
                    .AddLine(Code.This().Field(dataContextField).Method("SaveChanges").Close())
                    .AddLine(Code.Return(Code.Local("entity")));

                    repository.WithUsing("System.Data.Entity.Migrations")
                    .AddMethod("Update", Code.Generic("IEnumerable", modelType))
                    .WithParameter(Code.Generic("IEnumerable", modelType), "entities")
                    .Code.AddLine(Code.Declare(Code.Generic("List", modelType), "result", Code.Local("entities").Method("ToList")))
                    .AddLine(Code.Local("result").Method("ForEach", Code.Lambda("x", Code.This().Field(dataSetField).Method("AddOrUpdate", Code.Local("x")))).Close())
                    .AddLine(Code.This().Field(dataContextField).Method("SaveChanges").Close())
                    .AddLine(Code.Return(Code.Local("result")));
                }

                //repository.AddMethod("Update", Code.Void())
                //          .WithParameter(Code.Generic("Delta", modelType), "delta")
                //          .WithParameter(Code.Type("object[]"), "keys")
                //          .Code.AddLine(Code.Declare(modelType, "entity", Code.This().Field(dataSetField).Method("Find", Code.Local("keys"))))
                //          .AddLine(Code.If(Code.Local("entity").Equals().Null(), x => x.Code.AddLine(Code.Throw(Code.Type("InvalidOperationException"), Code.String("Can not find any element with this keys, Use Add(...) method instead")))))
                //          .AddLine(Code.Local("delta").Method("Patch", Code.Local("entity")).Close())
                //          .AddLine(Code.This().Method("Update", Code.Local("entity")).Close())
                //          .AddLine(Code.This().Field(dataContextField).Method("SaveChanges").Close());

                repository.AddMethod("Delete", Code.Void())
                .WithParameter(modelType, "entity")
                .Code.AddLine(Code.This().Field(dataSetField).Method("Remove", Code.Local("entity")).Close())
                .AddLine(Code.This().Field(dataContextField).Method("SaveChanges").Close());

                repository.AddMethod("Delete", Code.Void())
                .WithParameter(Code.Generic("IEnumerable", modelType), "entities")
                .Code.AddLine(Code.This().Field(dataSetField).Method("RemoveRange", Code.Local("entities")).Close())
                .AddLine(Code.This().Field(dataContextField).Method("SaveChanges").Close());

                if (configuration.IsCore)
                {
                    repository.AddMethod("Delete", Code.Void())
                    .WithParameter(Code.Type("params object[]"), "keys")
                    .Code.AddLine(Code.This().Field(dataSetField).Method("Remove", Code.This().Field(dataSetField).Method("Find", Code.Local("keys"))).Close())
                    .AddLine(Code.This().Field(dataContextField).Method("SaveChanges").Close());
                }
                else
                {
                    repository.AddMethod("Delete", Code.Void())
                    .WithParameter(Code.Type("params object[]"), "keys")
                    .Code.AddLine(Code.This().Field(dataSetField).Method("Remove", Code.This().Field(dataSetField).Method("Find", Code.Local("keys"))).Close())
                    .AddLine(Code.This().Field(dataContextField).Method("SaveChanges").Close());
                }

                //foreach (string key in entity.Keys)
                //{
                //    PropertyTransferObject property = entity.Model.Properties.First(x => x.Name.Equals(key, StringComparison.InvariantCultureIgnoreCase));
                //    delete.AddParameter(property.Type.ToTemplate(), property.Name)
                //          .FormatName(configuration.Language, configuration.FormatNames);
                //}
            }
        }
Exemplo n.º 24
0
        protected virtual ClassTemplate WriteClass(EntityFrameworkWriteConfiguration configuration, List <ITransferObject> transferObjects, List <FileTemplate> files)
        {
            ClassTemplate dataContext = files.AddFile(configuration.RelativePath, configuration.AddHeader)
                                        .AddNamespace(configuration.Namespace)
                                        .AddClass("DataContext", Code.Type("DbContext"));

            if (configuration.IsCore)
            {
                dataContext.WithUsing("Microsoft.EntityFrameworkCore");
            }
            else
            {
                dataContext.WithUsing("System.Data.Entity");
            }

            configuration.Usings.ForEach(x => dataContext.AddUsing(x));

            PropertyTemplate defaultConnectionProperty = dataContext.AddProperty("DefaultConnection", Code.Type("string")).Static().WithDefaultValue(Code.String("name=DataContext"));

            foreach (EntityTransferObject entity in transferObjects.OfType <EntityTransferObject>())
            {
                dataContext.AddProperty(entity.Name, Code.Generic("DbSet", entity.Model.ToTemplate()))
                .FormatName(configuration)
                .Virtual();
            }

            dataContext.AddConstructor()
            .WithThisConstructor(Code.Null());

            ConstructorTemplate constructor      = dataContext.AddConstructor();
            ParameterTemplate   connectionString = constructor.AddParameter(Code.Type("string"), "connectionString");

            if (configuration.IsCore)
            {
                constructor.WithBaseConstructor(Code.Static(Code.Type("SqlServerDbContextOptionsExtensions")).Method("UseSqlServer", Code.New(Code.Type("DbContextOptionsBuilder")), Code.NullCoalescing(Code.Local(connectionString), Code.Local(defaultConnectionProperty))).Property("Options"))
                .Code.AddLine(Code.This().Property("Database").Method("SetCommandTimeout", Code.Number(configuration.DataContext.CommandTimeout)).Close());
            }
            else
            {
                constructor.WithBaseConstructor(Code.NullCoalescing(Code.Local("connectionString"), Code.Local(defaultConnectionProperty)))
                .Code.AddLine(Code.This().Property("Database").Property("CommandTimeout").Assign(Code.Number(configuration.DataContext.CommandTimeout)).Close());
            }

            MethodTemplate    createMethod = dataContext.AddMethod("OnModelCreating", Code.Void()).Protected().Override();
            ParameterTemplate modelBuilder = createMethod.AddParameter(Code.Type(configuration.IsCore ? "ModelBuilder" : "DbModelBuilder"), "modelBuilder");

            if (!configuration.IsCore)
            {
                createMethod.Code.AddLine(Code.Local(modelBuilder).Property("Configurations").Method("AddFromAssembly", Code.This().Method("GetType").Property("Assembly")).Close());
            }

            foreach (EntityTransferObject entity in transferObjects.OfType <EntityTransferObject>())
            {
                createMethod.Code.AddLine(Code.Local(modelBuilder).GenericMethod("Entity", entity.Model.ToTemplate()).BreakLine()
                                          .Method("ToTable", Code.String(entity.Table), Code.String(entity.Schema)).BreakLine()
                                          .Method("HasKey", Code.Lambda("x", Code.Csharp("new { " + string.Join(", ", entity.Keys.Select(key => $"x.{key.Name}")) + " }"))).Close());
            }
            foreach (StoredProcedureTransferObject storedProcedure in transferObjects.OfType <StoredProcedureTransferObject>())
            {
                dataContext.AddMethod(storedProcedure.Name, storedProcedure.ReturnType.ToTemplate())
                .Code.AddLine(Code.This().Property("Database").Method("ExecuteSqlCommand", Code.String($"exec {storedProcedure.Schema}.{storedProcedure.Name}")).Close());
            }
            return(dataContext);
        }
Exemplo n.º 25
0
        public void Add(ParameterTemplate parameterTemplateRow, bool isParameterInSection)
        {
            var parameterTemplateItem = new FlexLabelItem(parameterTemplateRow, isParameterInSection);

            ChildList.Add(new FlexNodeReport(this, parameterTemplateItem));
        }
Exemplo n.º 26
0
 private IDbDataParameter CreateParameter(IDbCommand command, ParameterTemplate parameterTemplate, object value, string suffix = "")
 {
     var factory = _createGetParameterFactoryFunc(command);
     var parameter = default(IDbDataParameter);
     if(parameterTemplate.Column != null)
     {
         parameter = factory.CreateParameter(parameterTemplate.Name + suffix,
                                             parameterTemplate.Column);
     }
     else if (parameterTemplate.Type == ParameterType.NameOnly)
     {
         parameter = factory.CreateParameter(parameterTemplate.Name);
     }
     else
     {
         parameter = factory.CreateParameter(parameterTemplate.Name, parameterTemplate.DbType,
                                             parameterTemplate.MaxLength);
     }
     parameter.Value = FixObjectType(value);
     return parameter;
 }
 public static ParameterTemplate FormatName(this ParameterTemplate parameter, IOptions options, bool force = false)
 {
     parameter.Name = Formatter.FormatParameter(parameter.Name, options, force);
     return(parameter);
 }
Exemplo n.º 28
0
        internal FlexLabelItem
            (ParameterTemplate parentRow, ActivityParameter row)
            : base(row)
        {
            if (parentRow.intScheme.Equals((int)FFParameterScheme.Left) ||
                parentRow.intScheme.Equals((int)FFParameterScheme.Right))
            {
                Width  = parentRow.intWidth - parentRow.intLabelSize;
                Height = parentRow.intHeight;
            }
            else
            {
                Width  = parentRow.intWidth;
                Height = parentRow.intHeight - parentRow.intLabelSize;
            }
            Top  = parentRow.intTop;
            Left = parentRow.intLeft;

            switch (parentRow.intScheme)
            {
            case (int)FFParameterScheme.Left:
                Left += parentRow.intLabelSize;
                break;

            case (int)FFParameterScheme.Right:
                Left -= parentRow.intLabelSize;
                break;

            case (int)FFParameterScheme.Top:
                Top += parentRow.intLabelSize;
                break;

            case (int)FFParameterScheme.Bottom:
                Top -= parentRow.intLabelSize;
                break;
            }

            FontSize = ReportSettings.DefaultFontSize;

            if (row == null)
            {
                Text = string.Empty;
            }
            else if (ParameterControlTypeHelper.ConvertToParameterControlType(parentRow.idfsEditor).Equals(FFParameterEditors.CheckBox))
            {
                long val = (bool)row.varValue ? 10100001 : 10100002; //Yes/No
                using (var manager = DbManagerFactory.Factory.Create(ModelUserContext.Instance))
                {
                    Text = BaseReference.Accessor.Instance(null).rftYesNoValue_SelectList(manager).FirstOrDefault(c => c.idfsBaseReference == val, c => String.Empty);
                }
            }
            else if (!string.IsNullOrEmpty(row.strNameValue))
            {
                Text = row.strNameValue;
            }
            else if (row.varValue != null)
            {
                var str = row.varValue.ToString();
                if (parentRow.idfsParameterType.Equals((long)FFParameterTypes.Date))
                {
                    var itms = str.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries);
                    if (itms.Length > 0)
                    {
                        Text = itms[0];
                    }
                }
                else
                {
                    Text = str;
                }
            }
        }
Exemplo n.º 29
0
 public static bool IsCorrectDataType(this ParameterTemplate parameterTemplate, object value)
 {
     return(IsCorrectDataType(parameterTemplate.ParameterType, value));
 }
Exemplo n.º 30
0
 public CommandTemplate(string commandText, ParameterTemplate[] parameterNames, Dictionary<string, int> index)
 {
     _commandText = commandText;
     _parameters = parameterNames;
     _index = index;
 }
Exemplo n.º 31
0
 public static MethodTemplate WithParameter(this MethodTemplate methodTemplate, ParameterTemplate parameter)
 {
     methodTemplate.Parameters.Add(parameter);
     return(methodTemplate);
 }
Exemplo n.º 32
0
 private IDbDataParameter CreateParameter(IDbCommand command, ParameterTemplate parameterTemplate, object value, string suffix = "")
 {
     var factory = _createGetParameterFactoryFunc(command);
     var parameter = parameterTemplate.Column != null
                         ? factory.CreateParameter(parameterTemplate.Name + suffix,
                                                   parameterTemplate.Column)
                         : factory.CreateParameter(parameterTemplate.Name, parameterTemplate.DbType,
                                                   parameterTemplate.MaxLength);
     parameter.Value = FixObjectType(value);
     return parameter;
 }
Exemplo n.º 33
0
 private static void RewriteSqlEqualityToInClause(DatabaseSchema schema, IDbCommand command, ParameterTemplate parameterTemplate, StringBuilder builder)
 {
     if (command.CommandText.Contains("!= " + parameterTemplate.Name))
     {
         command.CommandText = command.CommandText.Replace("!= " + parameterTemplate.Name,
                                                           schema.Operators.NotIn + " (" +
                                                           builder.ToString().Substring(1) +
                                                           ")");
     }
     else
     {
         command.CommandText = command.CommandText.Replace("= " + parameterTemplate.Name,
                                                           schema.Operators.In + " (" +
                                                           builder.ToString().Substring(1) +
                                                           ")");
     }
 }
 public static LocalVariableTemplate Local(this ChainedCodeFragment template, ParameterTemplate type)
 {
     return(new LocalVariableTemplate(type.Name).Chain(template));
 }
Exemplo n.º 35
0
        private static IDbDataParameter CreateSingleParameter(IDbParameterFactory parameterFactory, object value, ParameterTemplate template)    
        {
            if (template.Column != null)
            {
                return CreateSingleParameter(parameterFactory, value, template.Name, template.Column);
            }

            var parameter = default(IDbDataParameter);
            if (template.Type == ParameterType.NameOnly)
            {
                parameter = parameterFactory.CreateParameter(template.Name);
            }
            else
            {
                parameter = parameterFactory.CreateParameter(template.Name, template.DbType, template.MaxLength);
            }
            parameter.Value = CommandHelper.FixObjectType(value);
            return parameter;
        }
Exemplo n.º 36
0
 private static IEnumerable<IDbDataParameter> CreateParameterComplex(ParameterTemplate template, object value, IDbCommand command)
 {
     var range = value as IRange;
     if (range != null)
     {
         yield return CreateSingleParameter(range.Start, command, template.Name + "_start", template.DbType);
         yield return CreateSingleParameter(range.End, command, template.Name + "_end", template.DbType);
         SetBetweenInCommandText(command, template.Name);
     }
     else
     {
         var list = value as IEnumerable<object>;
         if (list != null)
         {
             var builder = new StringBuilder();
             var array = list.ToArray();
             for (int i = 0; i < array.Length; i++)
             {
                 builder.AppendFormat(",{0}_{1}", template.Name, i);
                 yield return CreateSingleParameter(array[i], command, template.Name + "_" + i, template.DbType);
             }
             if (command.CommandText.Contains("!= " + template.Name))
             {
                 command.CommandText = command.CommandText.Replace("!= " + template.Name,
                                                               "NOT IN (" + builder.ToString().Substring(1) + ")");
             }
             else
             {
                 command.CommandText = command.CommandText.Replace("= " + template.Name,
                                                               "IN (" + builder.ToString().Substring(1) + ")");
             }
         }
         else
         {
             yield return CreateSingleParameter(value, command, template.Name, template.DbType);
         }
     }
 }
 public static ParameterTemplate FormatName(this ParameterTemplate parameter, IConfiguration configuration)
 {
     parameter.Name = Formatter.FormatParameter(parameter.Name, configuration);
     return(parameter);
 }
        public virtual void Write(AspDotNetWriteConfiguration configuration, List <ITransferObject> transferObjects, List <FileTemplate> files)
        {
            if (!configuration.Language.IsCsharp())
            {
                throw new InvalidOperationException($"Can not generate ASP.net Controller for language {configuration.Language?.Name ?? "Empty"}. Only Csharp is currently implemented");
            }
            foreach (AspDotNetWriteEntityControllerConfiguration controllerConfiguration in configuration.Controllers)
            {
                EntityTransferObject entity = transferObjects.OfType <EntityTransferObject>().FirstOrDefault(x => x.Name == controllerConfiguration.Entity)
                                              .AssertIsNotNull(nameof(controllerConfiguration.Entity), $"Entity {controllerConfiguration.Entity} not found. Ensure it is read before.");

                string        nameSpace  = (controllerConfiguration.Namespace ?? configuration.Namespace).AssertIsNotNull(nameof(configuration.Namespace), "asp writer requires a namespace");
                ClassTemplate controller = files.AddFile(configuration.RelativePath, configuration.AddHeader)
                                           .AddNamespace(nameSpace)
                                           .AddClass(controllerConfiguration.Name ?? entity.Name + "Controller", Code.Type(configuration.Template.ControllerBase))
                                           .FormatName(configuration)
                                           .WithAttribute("Route", Code.String(controllerConfiguration.Route ?? "[controller]"));

                controller.Usings.AddRange(configuration.Template.Usings);

                TypeTemplate modelType = entity.Model.ToTemplate();

                configuration.Usings.ForEach(x => controller.AddUsing(x));
                controllerConfiguration.Usings.ForEach(x => controller.AddUsing(x));

                FieldTemplate repositoryField = controller.AddField("repository", Code.Type(entity.Name + "Repository")).Readonly();
                controller.AddConstructor().Code.AddLine(Code.This().Field(repositoryField).Assign(Code.New(repositoryField.Type)).Close());

                if (controllerConfiguration.Get != null)
                {
                    controller.AddUsing("System.Linq");
                    MethodTemplate method = controller.AddMethod("Get", Code.Generic("IEnumerable", modelType));
                    if (configuration.Template.UseAttributes)
                    {
                        method.WithAttribute("HttpGet", Code.String(controllerConfiguration.Get.Name ?? "[action]"));
                    }
                    DeclareTemplate queryable = Code.Declare(Code.Generic("IQueryable", modelType), "queryable", Code.This().Field(repositoryField).Method("Get"));
                    method.Code.AddLine(queryable);
                    foreach (PropertyTransferObject property in entity.Model.Properties)
                    {
                        ParameterTemplate parameter = method.AddParameter(property.Type.ToTemplate(), property.Name, Code.Local("default")).FormatName(configuration);
                        method.Code.AddLine(Code.If(Code.Local(parameter).NotEquals().Local("default"), x => x.Code.AddLine(Code.Local(queryable).Assign(Code.Local(queryable).Method("Where", Code.Lambda("x", Code.Local("x").Property(property.Name).Equals().Local(parameter)))).Close())));
                    }
                    method.Code.AddLine(Code.Return(Code.Local(queryable)));
                }
                if (controllerConfiguration.Post != null)
                {
                    MethodTemplate method = controller.AddMethod("Post", Code.Void());
                    if (configuration.Template.UseAttributes)
                    {
                        method.WithAttribute("HttpPost", Code.String(controllerConfiguration.Post.Name ?? "[action]"));
                    }
                    ParameterTemplate parameter = method.AddParameter(modelType, "entity")
                                                  .WithAttribute("FromBody");

                    method.Code.AddLine(Code.This().Field(repositoryField).Method("Add", Code.Local(parameter)).Close());
                }
                if (controllerConfiguration.Patch != null)
                {
                    MethodTemplate method = controller.AddMethod("Patch", Code.Void());
                    if (configuration.Template.UseAttributes)
                    {
                        method.WithAttribute("HttpPatch", Code.String(controllerConfiguration.Patch.Name ?? "[action]"));
                    }
                    ParameterTemplate parameter = method.AddParameter(modelType, "entity")
                                                  .WithAttribute("FromBody");

                    method.Code.AddLine(Code.This().Field(repositoryField).Method("Update", Code.Local(parameter)).Close());
                }
                if (controllerConfiguration.Put != null)
                {
                    MethodTemplate method = controller.AddMethod("Put", Code.Void());
                    if (configuration.Template.UseAttributes)
                    {
                        method.WithAttribute("HttpPut", Code.String(controllerConfiguration.Put.Name ?? "[action]"));
                    }
                    ParameterTemplate parameter = method.AddParameter(modelType, "entity")
                                                  .WithAttribute("FromBody");

                    method.Code.AddLine(Code.This().Field(repositoryField).Method("Update", Code.Local(parameter)).Close());
                }
                if (controllerConfiguration.Delete != null)
                {
                    MethodTemplate method = controller.AddMethod("Delete", Code.Void());
                    if (configuration.Template.UseAttributes)
                    {
                        method.WithAttribute("HttpDelete", Code.String(controllerConfiguration.Delete.Name ?? "[action]"));
                    }
                    List <ParameterTemplate> parameters = new List <ParameterTemplate>();
                    foreach (EntityKeyTransferObject key in entity.Keys)
                    {
                        PropertyTransferObject property = entity.Model.Properties.First(x => x.Name.Equals(key.Name, StringComparison.InvariantCultureIgnoreCase));
                        parameters.Add(method.AddParameter(property.Type.ToTemplate(), property.Name)
                                       .FormatName(configuration));
                    }
                    method.Code.AddLine(Code.This().Field(repositoryField).Method("Delete", parameters.Select(x => Code.Local(x))).Close());
                }
            }
        }