public static void UpdateItem(this Item item, ItemDto dto) { foreach (var prop in item.GetType().GetProperties().Where(p => p.Name != "Id")) { var itemProp = item.GetType().GetProperty(prop.Name); var dtoProp = dto.GetType().GetProperty(prop.Name); var newValue = dtoProp.GetValue(dto); itemProp.SetValue(item, newValue); } }
public void TableColumnNamesAndValuesSql() { var syntax = SqlSyntax.MySql; syntax.Render(Sql.ColumnNames <ItemDto>("t")).Text.Should().Be("`t`.`ItemId`, `t`.`DisplayName`"); syntax.Render(Sql.ColumnNames(typeof(ItemDto), "t")).Text.Should().Be("`t`.`ItemId`, `t`.`DisplayName`"); var item = new ItemDto { Id = 3, DisplayName = "three" }; syntax.Render(Sql.ColumnNames(item.GetType(), "t")).Text.Should().Be("`t`.`ItemId`, `t`.`DisplayName`"); }
public void SnakeCaseNamesAndValuesSql() { var syntax = SqlSyntax.MySql.WithSnakeCase(); syntax.Render(Sql.ColumnNames <ItemDto>("t")).Text.Should().Be("`t`.`ItemId`, `t`.`display_name`"); syntax.Render(Sql.ColumnNames(typeof(ItemDto), "t")).Text.Should().Be("`t`.`ItemId`, `t`.`display_name`"); var item = new ItemDto { Id = 3, DisplayName = "three" }; syntax.Render(Sql.ColumnNames(item.GetType(), "t")).Text.Should().Be("`t`.`ItemId`, `t`.`display_name`"); var anon = new { item.Id, item.DisplayName }; syntax.Render(Sql.ColumnNames(anon.GetType(), "t")).Text.Should().Be("`t`.`id`, `t`.`display_name`"); }
public void TableColumnNamesAndValuesWhereSql() { var syntax = SqlSyntax.MySql; syntax.Render(Sql.ColumnNamesWhere <ItemDto>(x => x != nameof(ItemDto.Id), "t")).Text.Should().Be("`t`.`DisplayName`"); var item = new ItemDto { Id = 3, DisplayName = "three" }; var(text, parameters) = syntax.Render(Sql.Format($@" insert into Items ({Sql.ColumnNamesWhere(item.GetType(), x => x is not nameof(ItemDto.Id), "t")}) values ({Sql.ColumnParamsWhere(item, x => x is not nameof(ItemDto.Id))});" )); text.Should().Be(@" insert into Items (`t`.`DisplayName`) values (@fdp0);" ); parameters.Should().Equal(("fdp0", item.DisplayName)); }
public void ColumnNamesAndValuesSql() { var syntax = SqlSyntax.MySql; syntax.Render(Sql.ColumnNames <ItemDto>()).Text.Should().Be("`ItemId`, `DisplayName`"); syntax.Render(Sql.ColumnNames(typeof(ItemDto))).Text.Should().Be("`ItemId`, `DisplayName`"); var item = new ItemDto { Id = 3, DisplayName = "three" }; var(text, parameters) = syntax.Render(Sql.Format($"insert into Items ({Sql.ColumnNames(item.GetType())}) values ({Sql.ColumnParams(item)});")); text.Should().Be("insert into Items (`ItemId`, `DisplayName`) values (@fdp0, @fdp1);"); parameters.Should().Equal(("fdp0", item.Id), ("fdp1", item.DisplayName)); var anon = new { item.Id, item.DisplayName }; (text, parameters) = syntax.Render(Sql.Format($"insert into Items ({Sql.ColumnNames(anon.GetType())}) values ({Sql.ColumnParams(anon)});")); text.Should().Be("insert into Items (`Id`, `DisplayName`) values (@fdp0, @fdp1);"); parameters.Should().Equal(("fdp0", anon.Id), ("fdp1", anon.DisplayName)); }