Skip to content

Commit e73e315

Browse files
Merge pull request #190 from AntonioFalcao/feature/builder
Feature/builder
2 parents 7a23309 + 0e75f49 commit e73e315

20 files changed

Lines changed: 110 additions & 195 deletions

File tree

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
1-
using Dotnet6.GraphQL4.Domain.Abstractions.Entities;
1+
using System;
2+
using Dotnet6.GraphQL4.Domain.Abstractions.Builders.Extensions;
3+
using Dotnet6.GraphQL4.Domain.Abstractions.Entities;
24

35
namespace Dotnet6.GraphQL4.Domain.Abstractions.Builders
46
{
5-
public abstract class Builder<TBuilder, TEntity, TId> : IBuilder<TEntity, TId>
7+
public abstract class Builder<TBuilder, TEntity, TId> : IBuilder<TBuilder, TEntity, TId>
68
where TBuilder : Builder<TBuilder, TEntity, TId>
79
where TEntity : Entity<TId>
810
where TId : struct
911
{
1012
protected TId Id;
11-
1213
public abstract TEntity Build();
1314

14-
public TBuilder WithId(TId id)
15-
{
16-
Id = id;
17-
return (TBuilder) this;
18-
}
15+
public TBuilder With<TProperty>(Func<TBuilder, TProperty> func)
16+
=> ((TBuilder) this).With<TBuilder, TProperty>(func);
1917
}
2018
}
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.Extensions
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.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 { get; 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 { get; set; }
9+
int Size { get; 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 { get; set; }
15+
DateTimeOffset IntroduceAt { get; set; }
16+
string Name { get; set; }
17+
Option Option { get; set; }
18+
string PhotoUrl { get; set; }
19+
decimal Price { get; set; }
20+
ProductType ProductType { get; set; }
21+
int Rating { get; set; }
22+
int Stock { get; set; }
2223
}
2324
}

src/Dotnet6.GraphQL4.Store.Domain/Entities/Products/Kayaks/IKayakBuilder.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.Kayaks
55
{
6-
public interface IKayakBuilder : IProductBuilder<Kayak, Guid>
6+
public interface IKayakBuilder : IProductBuilder<KayakBuilder, Kayak, Guid>
77
{
8-
IKayakBuilder WithAmountOfPerson(int amountOfPerson);
9-
IKayakBuilder WithType(KayakType type);
8+
int AmountOfPerson { get; set; }
9+
KayakType Type { get; set; }
1010
}
1111
}

0 commit comments

Comments
 (0)