Skip to content

Commit 5b8977c

Browse files
Evolving builder design
1 parent 20fe1f6 commit 5b8977c

21 files changed

Lines changed: 110 additions & 196 deletions

File tree

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
1-
using Dotnet6.GraphQL4.Domain.Abstractions.Entities;
1+
using System;
2+
using Dotnet6.GraphQL4.Domain.Abstractions.Entities;
23

34
namespace Dotnet6.GraphQL4.Domain.Abstractions.Builders
45
{
5-
public abstract class Builder<TBuilder, TEntity, TId> : IBuilder<TEntity, TId>
6+
public abstract class Builder<TBuilder, TEntity, TId> : IBuilder<TBuilder, TEntity, TId>
67
where TBuilder : Builder<TBuilder, TEntity, TId>
78
where TEntity : Entity<TId>
89
where TId : struct
910
{
1011
protected TId Id;
11-
1212
public abstract TEntity Build();
1313

14-
public TBuilder WithId(TId id)
15-
{
16-
Id = id;
17-
return (TBuilder) this;
18-
}
14+
public TBuilder With<TProperty>(Func<TBuilder, TProperty> func)
15+
=> ((TBuilder) this).With<TBuilder, TProperty>(func);
1916
}
2017
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
3+
namespace Dotnet6.GraphQL4.Domain.Abstractions.Builders
4+
{
5+
internal static class BuilderExtensions
6+
{
7+
internal static TBuilder With<TBuilder, TProperty>(this TBuilder builder, Func<TBuilder, TProperty> func)
8+
{
9+
func(builder);
10+
return builder;
11+
}
12+
}
13+
}
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
using Dotnet6.GraphQL4.Domain.Abstractions.Entities;
1+
using System;
2+
using Dotnet6.GraphQL4.Domain.Abstractions.Entities;
23

34
namespace Dotnet6.GraphQL4.Domain.Abstractions.Builders
45
{
5-
public interface IBuilder<out TEntity, TId>
6+
public interface IBuilder<out TBuilder, out TEntity, TId>
7+
where TBuilder : IBuilder<TBuilder, TEntity, TId>
68
where TEntity : Entity<TId>
79
where TId : struct
810
{
911
TEntity Build();
12+
TBuilder With<TProperty>(Func<TBuilder, TProperty> func);
1013
}
1114
}

src/Dotnet6.GraphQL4.Domain.Abstractions/DependencyInjection/Extensions/ServiceCollectionExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public static class ServiceCollectionExtensions
1010
public static IServiceCollection AddBuilders(this IServiceCollection services)
1111
=> services
1212
.Scan(selector => selector.FromAssemblies(Application.Assemblies)
13-
.AddClasses(filter => filter.AssignableTo(typeof(IBuilder<,>)))
13+
.AddClasses(filter => filter.AssignableTo(typeof(IBuilder<,,>)))
1414
.UsingRegistrationStrategy(RegistrationStrategy.Skip)
1515
.AsImplementedInterfaces()
1616
.WithScopedLifetime());

src/Dotnet6.GraphQL4.Repositories.Abstractions/DependencyInjection/Extensions/ServiceCollectionExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ public static OptionsBuilder<ApplicationTransactionOptions> ConfigureTransaction
2626
=> services
2727
.AddOptions<ApplicationTransactionOptions>()
2828
.Bind(section)
29-
.Validate(options => options.IsolationLevel is IsolationLevel.Unspecified);
29+
.Validate(options => options.IsolationLevel is not IsolationLevel.Unspecified);
3030
}
3131
}

src/Dotnet6.GraphQL4.Store.Domain/Entities/Products/Backpacks/BackpackBuilder.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,11 @@
33

44
namespace Dotnet6.GraphQL4.Store.Domain.Entities.Products.Backpacks
55
{
6-
public class BackpackBuilder : ProductBuilder<Backpack, Guid>, IBackpackBuilder
6+
public class BackpackBuilder : ProductBuilder<BackpackBuilder, Backpack, Guid>, IBackpackBuilder
77
{
8-
private BackpackType _type;
8+
public BackpackType Type { get; set; }
99

1010
public override Backpack Build()
11-
=> new(Id, Description, IntroduceAt, Name, PhotoUrl, Price, ProductType, Rating, Stock, Option, _type);
12-
13-
public IBackpackBuilder WithType(BackpackType type)
14-
{
15-
_type = type;
16-
return this;
17-
}
11+
=> new(Id, Description, IntroduceAt, Name, PhotoUrl, Price, ProductType, Rating, Stock, Option, Type);
1812
}
1913
}

src/Dotnet6.GraphQL4.Store.Domain/Entities/Products/Backpacks/IBackpackBuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
namespace Dotnet6.GraphQL4.Store.Domain.Entities.Products.Backpacks
55
{
6-
public interface IBackpackBuilder : IProductBuilder<Backpack, Guid>
6+
public interface IBackpackBuilder : IProductBuilder<BackpackBuilder, Backpack, Guid>
77
{
8-
IBackpackBuilder WithType(BackpackType type);
8+
BackpackType Type { set; }
99
}
1010
}

src/Dotnet6.GraphQL4.Store.Domain/Entities/Products/Boots/BootBuilder.cs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,12 @@
33

44
namespace Dotnet6.GraphQL4.Store.Domain.Entities.Products.Boots
55
{
6-
public class BootBuilder : ProductBuilder<Boot, Guid>, IBootBuilder
6+
public class BootBuilder : ProductBuilder<BootBuilder, Boot, Guid>, IBootBuilder
77
{
8-
private int _size;
9-
private BootType _type;
8+
public BootType Type { get; set; }
9+
public int Size { get; set; }
1010

1111
public override Boot Build()
12-
=> new(Id, Description, IntroduceAt, Name, PhotoUrl, Price, ProductType, Rating, Stock, Option, _type, _size);
13-
14-
public IBootBuilder WithType(BootType type)
15-
{
16-
_type = type;
17-
return this;
18-
}
19-
20-
public IBootBuilder WithSize(int size)
21-
{
22-
_size = size;
23-
return this;
24-
}
12+
=> new(Id, Description, IntroduceAt, Name, PhotoUrl, Price, ProductType, Rating, Stock, Option, Type, Size);
2513
}
2614
}

src/Dotnet6.GraphQL4.Store.Domain/Entities/Products/Boots/IBootBuilder.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33

44
namespace Dotnet6.GraphQL4.Store.Domain.Entities.Products.Boots
55
{
6-
public interface IBootBuilder : IProductBuilder<Boot, Guid>
6+
public interface IBootBuilder : IProductBuilder<BootBuilder,Boot, Guid>
77
{
8-
IBootBuilder WithType(BootType type);
9-
IBootBuilder WithSize(int size);
8+
BootType Type { set; }
9+
int Size { set; }
1010
}
1111
}

src/Dotnet6.GraphQL4.Store.Domain/Entities/Products/IProductBuilder.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,19 @@
66

77
namespace Dotnet6.GraphQL4.Store.Domain.Entities.Products
88
{
9-
public interface IProductBuilder<out TProduct, TId> : IBuilder<TProduct, TId>
9+
public interface IProductBuilder<out TBuilder, out TProduct, TId> : IBuilder<TBuilder, TProduct, TId>
10+
where TBuilder : IBuilder<TBuilder, TProduct, TId>
1011
where TProduct : Entity<TId>, IProduct
1112
where TId : struct
1213
{
13-
IProductBuilder<TProduct, TId> WithDescription(string description);
14-
IProductBuilder<TProduct, TId> WithIntroduceAt(DateTimeOffset introduceAt);
15-
IProductBuilder<TProduct, TId> WithName(string name);
16-
IProductBuilder<TProduct, TId> WithOption(Option option);
17-
IProductBuilder<TProduct, TId> WithPhotoUrl(string photoUrl);
18-
IProductBuilder<TProduct, TId> WithPrice(decimal price);
19-
IProductBuilder<TProduct, TId> WithProductType(ProductType productType);
20-
IProductBuilder<TProduct, TId> WithRating(int rating);
21-
IProductBuilder<TProduct, TId> WithStock(int stock);
14+
string Description { set; }
15+
DateTimeOffset IntroduceAt { set; }
16+
string Name { set; }
17+
Option Option { set; }
18+
string PhotoUrl { set; }
19+
decimal Price { set; }
20+
ProductType ProductType { set; }
21+
int Rating { set; }
22+
int Stock { set; }
2223
}
2324
}

0 commit comments

Comments
 (0)