Esempio n. 1
0
        public void GenerateInsertReturnIdAsync()
        {
            // Arrange
            var objUser = new User();
            IList <PropertyInfo> props = new List <PropertyInfo>(objUser.GetType().GetProperties());

            // Act
            var ret = DapperGenerator.Insert("User", props, false, false, false, true, true, false);

            // Assert
            Assert.Contains("INSERT INTO [User] (Id, Name, Email) VALUES (@Id, @Name, @Email)", ret);
            Assert.Contains("return await db.QueryAsync<int>(sql, new { Id = user.Id, Name = user.Name, Email = user.Email }, commandType: CommandType.Text).Single();", ret);
        }
Esempio n. 2
0
        public void GenerateInsertNoIdAsync()
        {
            // Arrange
            var objUser = new User();
            IList <PropertyInfo> props = new List <PropertyInfo>(objUser.GetType().GetProperties());

            // Act
            var ret = DapperGenerator.Insert("User", props, false, false, true, true, false, false);

            // Assert
            Assert.Contains("INSERT INTO [User] (Name, Email) VALUES (@Name, @Email)", ret);
            Assert.Contains("await db.ExecuteAsync(sql, new { Name = user.Name, Email = user.Email }, commandType: CommandType.Text);", ret);
        }
Esempio n. 3
0
        public void GenerateReturnIdInsertAsync()
        {
            // Arrange
            var objUser = new User();
            IList <PropertyInfo> props = new List <PropertyInfo>(objUser.GetType().GetProperties());
            var content = DapperGenerator.Insert("User", props, false, false, false, true, true);

            // Act
            var ret = MethodGenerator.GenerateInsert(content, model, false, true, true);

            // Assert
            Assert.Contains("public async Task<int> InsertUser(User user)", ret);
            Assert.Contains(content, ret);
        }
        private void GenerateCrud()
        {
            try
            {
                Logger.Log("Initializing generation process...");
                SetLoading(true);

                txtOutput.Text = string.Empty;
                foreach (var item in lstFiles.CheckedItems)
                {
                    var model = item.ToString();
                    IList <PropertyInfo> properties       = GetPropertyInfos(model);
                    IList <PropertyInfo> propertiesUpdate = GetPropertyInfos(model);
                    IList <PropertyInfo> propertiesDelete = GetPropertyInfos(model);

                    string output = string.Empty;

                    if (chkClass.Checked)
                    {
                        output += ClassGenerator.GenerateClassBody(model, chkInterface.Checked);

                        if (chkSelect.Checked)
                        {
                            output += MethodGenerator.GenerateSelect(
                                DapperGenerator.Select(model, properties, chkGenerateMethod.Checked, chkClass.Checked, chkAsync.Checked),
                                model, chkClass.Checked, chkAsync.Checked);
                        }

                        if (chkInsert.Checked)
                        {
                            output += MethodGenerator.GenerateInsert(
                                DapperGenerator.Insert(model, properties, chkGenerateMethod.Checked, chkClass.Checked, chkAutoIncrement.Checked, chkAsync.Checked, chkReturnIdentity.Checked),
                                model, chkClass.Checked, chkAsync.Checked, chkReturnIdentity.Checked);
                        }

                        if (chkUpdate.Checked)
                        {
                            output += MethodGenerator.GenerateUpdate(
                                DapperGenerator.Update(model, propertiesUpdate, chkGenerateMethod.Checked, chkClass.Checked, chkAutoIncrement.Checked, chkAsync.Checked),
                                model, chkClass.Checked, chkAsync.Checked);
                        }

                        if (chkDelete.Checked)
                        {
                            output += MethodGenerator.GenerateDelete(
                                DapperGenerator.Delete(model, propertiesDelete, chkGenerateMethod.Checked, chkClass.Checked, chkAsync.Checked),
                                model, chkClass.Checked, chkAsync.Checked);
                        }

                        output += "}";

                        txtOutput.Text += output;

                        if (chkGenerateFiles.Checked)
                        {
                            FileHelper.GenerateClass(output, model, Projectpath);
                        }
                    }
                    else
                    {
                        if (chkGenerateMethod.Checked)
                        {
                            if (chkSelect.Checked)
                            {
                                txtOutput.Text +=
                                    MethodGenerator.GenerateSelect(DapperGenerator.Select(model, properties, chkGenerateMethod.Checked, chkClass.Checked, chkAsync.Checked), model, chkClass.Checked, chkAsync.Checked);
                            }

                            if (chkInsert.Checked)
                            {
                                txtOutput.Text +=
                                    MethodGenerator.GenerateInsert(DapperGenerator.Insert(model, properties, chkGenerateMethod.Checked, chkClass.Checked, chkAutoIncrement.Checked, chkAsync.Checked, chkReturnIdentity.Checked), model, chkClass.Checked, chkAsync.Checked, chkReturnIdentity.Checked);
                            }

                            if (chkUpdate.Checked)
                            {
                                txtOutput.Text +=
                                    MethodGenerator.GenerateUpdate(DapperGenerator.Update(model, propertiesUpdate, chkGenerateMethod.Checked, chkClass.Checked, chkAutoIncrement.Checked, chkAsync.Checked), model, chkClass.Checked, chkAsync.Checked);
                            }

                            if (chkDelete.Checked)
                            {
                                txtOutput.Text +=
                                    MethodGenerator.GenerateDelete(DapperGenerator.Delete(model, propertiesDelete, chkGenerateMethod.Checked, chkClass.Checked, chkAsync.Checked), model, chkClass.Checked, chkAsync.Checked);
                            }
                        }
                        else
                        {
                            if (chkSelect.Checked)
                            {
                                txtOutput.Text += DapperGenerator.Select(model, properties, chkGenerateMethod.Checked, chkClass.Checked, chkAsync.Checked);
                            }

                            if (chkInsert.Checked)
                            {
                                txtOutput.Text += DapperGenerator.Insert(model, properties, chkGenerateMethod.Checked, chkClass.Checked, chkAutoIncrement.Checked, chkAsync.Checked, chkReturnIdentity.Checked);
                            }

                            if (chkUpdate.Checked)
                            {
                                txtOutput.Text += DapperGenerator.Update(model, propertiesUpdate, chkGenerateMethod.Checked, chkClass.Checked, chkAutoIncrement.Checked, chkAsync.Checked);
                            }

                            if (chkDelete.Checked)
                            {
                                txtOutput.Text += DapperGenerator.Delete(model, propertiesDelete, chkGenerateMethod.Checked, chkClass.Checked, chkAsync.Checked);
                            }
                        }
                    }

                    if (chkInterface.Checked)
                    {
                        output = InterfaceGenerator.GenerateInterfaceBody(model);

                        if (chkSelect.Checked)
                        {
                            output += InterfaceGenerator.GenerateSelect(model, chkAsync.Checked);
                        }

                        if (chkInsert.Checked)
                        {
                            output += InterfaceGenerator.GenerateInsert(model, chkAsync.Checked, chkReturnIdentity.Checked);
                        }

                        if (chkUpdate.Checked)
                        {
                            output += InterfaceGenerator.GenerateUpdate(model, chkAsync.Checked);
                        }

                        if (chkDelete.Checked)
                        {
                            output += InterfaceGenerator.GenerateDelete(model, chkAsync.Checked);
                        }

                        output += "}";

                        txtOutput.Text += output;

                        if (chkGenerateFiles.Checked)
                        {
                            FileHelper.GenerateInterface(output, model, Projectpath);
                        }
                    }
                }
                SetLoading(false);
                Logger.Log($"Process Completed Successfully!");
            }
            catch (Exception ex)
            {
                SetLoading(false);
                Logger.Log($"Error during the operation: {ex.Message} InnerException {ex.InnerException} StackTrace {ex.StackTrace} Code {AssemblyHelper.codeGlobal}");
                txtOutputLog.ForeColor = Color.Red;
                txtOutputLog.Text      = $"Error during the operation: {ex.Message} InnerException {ex.InnerException} StackTrace {ex.StackTrace} Code {AssemblyHelper.codeGlobal}";
            }
        }