Refactoring and prepare to add update template

This commit is contained in:
CodeLiturgy 2022-08-27 21:02:46 +01:00
parent b4362789cc
commit 6540959778
9 changed files with 65 additions and 5 deletions

View File

@ -19,6 +19,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<AdditionalFiles Include=".\Extensions\AddToEntityTemplate.csx" /> <AdditionalFiles Include=".\Extensions\AddToEntityTemplate.csx" />
<AdditionalFiles Include="Context\Templates\AddToEntityTemplate.csx" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@ -0,0 +1,10 @@
namespace BlueWest.WebApi.Context;
public class AttributeNavigation<T>
{
object HasForeignKey<TDependentEntity>(
Expression<Func<TDependentEntity, object?>> foreignKeyExpression)
{
}
}

View File

@ -1,4 +1,5 @@
using BlueWest.Data; using BlueWest.Data;
using BlueWest.WebApi.Context;
using BlueWest.WebApi.EF.Model; using BlueWest.WebApi.EF.Model;
using MapTo; using MapTo;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
@ -16,15 +17,36 @@ namespace BlueWest.WebApi.EF
/// <summary> /// <summary>
/// Countries Database Table /// Countries Database Table
/// </summary> /// </summary>
[EfAddMethods(typeof(CountryCreate), typeof(CountryUnique))] [EfAddMethods(
createDto: typeof(CountryCreate),
returnType: typeof(CountryUnique))
]
[EfAddUpdateMethods(
updateType: typeof(CountryUpdate),
returnType: typeof(CountryUnique),
keyPropertyName: "Id",
keyPropertyType: typeof(System.Int64))
]
public DbSet<Country> Countries { get; set; } public DbSet<Country> Countries { get; set; }
/// <summary> /// <summary>
/// Currencies Database Table /// Currencies Database Table
/// </summary> /// </summary>
// Generate Add extension methods
[EfAddMethods(typeof(CurrencyCreate), typeof(CurrencyUnique))] [EfAddMethods(typeof(CurrencyCreate), typeof(CurrencyUnique))]
[EfAddUpdateMethods(
updateType: typeof(CurrencyUpdate),
returnType: typeof(CurrencyUnique),
keyPropertyName: "Id",
keyPropertyType: typeof(System.Int64))
]
public DbSet<Currency> Currencies { get; set; } public DbSet<Currency> Currencies { get; set; }
////////////////////////////////////////////////
////////////////////////////////////////////////
/// <summary> /// <summary>
/// App Configuration /// App Configuration
/// </summary> /// </summary>

View File

@ -0,0 +1,11 @@
using System;
using System.Linq.Expressions;
using BlueWest.Data;
namespace BlueWest.WebApi.Context;
[AttributeUsage(AttributeTargets.Property)]
public class EfAddUpdateMethodsAttribute : Attribute
{
public EfAddUpdateMethodsAttribute(Type returnType, Type updateType, string keyPropertyName, Type keyPropertyType) { }
}

View File

@ -0,0 +1,10 @@
public static partial class {entityTypeName}Extensions {
public static (bool, {readTypeFullName}) Add{entityTypeName}(
this {contextFullName} dbContext, {createTypeFullName} {toCreateVarName}) {
var {newEntityVarName} = new {entityTypeFullName}({toCreateVarName});
dbContext.{propertyName}.Add({newEntityVarName});
var success = dbContext.SaveChanges() >= 0;
return (success, new {readTypeFullName}({newEntityVarName}));
}
}

View File

@ -0,0 +1,6 @@
using Microsoft.EntityFrameworkCore;
namespace {Namespace}
{
{Code}
}

View File

@ -3,9 +3,9 @@ using Microsoft.EntityFrameworkCore;
namespace {Namespace} namespace {Namespace}
{ {
public static partial class {entityTypeName}Extensions { public static partial class {entityTypeName}Extensions {
public static (bool, {readTypeFullName}) Add{entityTypeName}( public static (bool, {readTypeFullName}) Update{entityTypeName}(
this {contextFullName} dbContext, {createTypeFullName} {toCreateVarName}) { this {contextFullName} dbContext, {updateTypeFullName} {toUpdateVarName}) {
var {newEntityVarName} = new {entityTypeFullName}({toCreateVarName}); var {newEntityVarName} = new {entityTypeFullName}({toUpdateVarName});
dbContext.{propertyName}.Add({newEntityVarName}); dbContext.{propertyName}.Add({newEntityVarName});
var success = dbContext.SaveChanges() >= 0; var success = dbContext.SaveChanges() >= 0;
return (success, new {readTypeFullName}({newEntityVarName})); return (success, new {readTypeFullName}({newEntityVarName}));

@ -1 +1 @@
Subproject commit 340a89bbd2e3d94cfb84f9a365d5e7db10f99bb8 Subproject commit 4d9cc4b029b15049b5d9a8bb8e87f6d9a513b712