Skip to content

Commit

Permalink
Moved WithObject to generated code, added FromTemplate (#43)
Browse files Browse the repository at this point in the history
* Releasing v2.4-alpha
* Refactored template storage
* Refactores DTO mechanism
* Moved license file
* Added Directory.Build.props file
  • Loading branch information
MelGrubb authored Jan 14, 2024
1 parent 1f487e9 commit e25b90c
Show file tree
Hide file tree
Showing 77 changed files with 1,271 additions and 515 deletions.
14 changes: 14 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Contributing

This project welcomes issues and pull requests, although it is an opinionated project. There are many ways to approach this particular problem, and I (Mel Grubb) captured my own preferences when I started this project.

# Building

# Testing

There are currently five test projects.
- BuilderGenerator.Tests.Core: Defines the domain objects used by other test projects.
- BuilderGenerator.Tests.Unit: Directly exercises the builder mechanism to provide fast feedback and verification.
- BuilderGenerator.Tests.Integration.Net60: Uses the builder generator in a more real-world way to create builders for a sample Net 6 library project.
- BuilderGenerator.Tests.Integration.Net70: Same thing, but targeting .Net 7
- BuilderGenerator.Tests.Integration.Net80: Same thing, but targeting .Net 8
2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2021-2023 Mel Grubb
Copyright (c) 2021-2024 Mel Grubb

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ Install-Package BuilderGenerator
After installation, create a partial class to define your builder in. Decorate it with the ```BuilderFor``` attribute, specifying the type of class that the builder is meant to build (e.g. ```[BuilderFor(typeof(Foo))]```. Define any factory and helper methods in this partial class. Meanwhile, another partial class definition will be auto-generated which contains all the "boring" parts such as the backing fields and "with" methods.

## Version History ##
- v2.4.0
- Test code reorganization
- Moved WithObject from the base class to the generated builder class
- Added WithValuesFrom method to shallow clone an example object.

- v2.3.0
- Major caching and performance improvements
- Internal code cleanup
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@
<IsPackable>false</IsPackable>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
<_Parameter1>BuilderGenerator.IntegrationTests.Net60</_Parameter1>
<_Parameter1>BuilderGenerator.Tests.Integration.Net60</_Parameter1>
</AssemblyAttribute>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
<_Parameter1>BuilderGenerator.Tests.Integration.Net70</_Parameter1>
</AssemblyAttribute>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
<_Parameter1>BuilderGenerator.IntegrationTests.Net60</_Parameter1>
<_Parameter1>BuilderGenerator.Tests.Integration.Net80</_Parameter1>
</AssemblyAttribute>
</ItemGroup>
</Project>
</Project>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace BuilderGenerator.IntegrationTests.Core.Models.Entities;
namespace BuilderGenerator.Tests.Core.Models.Entities;

public abstract class AuditableEntity : Entity
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Collections.Generic;

namespace BuilderGenerator.IntegrationTests.Core.Models.Entities;
namespace BuilderGenerator.Tests.Core.Models.Entities;

public class CollectionTypesSample
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace BuilderGenerator.IntegrationTests.Core.Models.Entities;
namespace BuilderGenerator.Tests.Core.Models.Entities;

public abstract class Entity
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System;
using System.Collections.Generic;
using BuilderGenerator.IntegrationTests.Core.Models.Enums;
using BuilderGenerator.Tests.Core.Models.Enums;

namespace BuilderGenerator.IntegrationTests.Core.Models.Entities;
namespace BuilderGenerator.Tests.Core.Models.Entities;

public class Order : AuditableEntity
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace BuilderGenerator.IntegrationTests.Core.Models.Entities;
namespace BuilderGenerator.Tests.Core.Models.Entities;

public class OrderItem : AuditableEntity
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
#nullable enable

using System.Collections.Generic;

namespace BuilderGenerator.IntegrationTests.Core.Models.Entities;
namespace BuilderGenerator.Tests.Core.Models.Entities;

public partial class User : AuditableEntity
public class User : AuditableEntity
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string? MiddleName { get; set; }

Check warning on line 9 in src/BuilderGenerator.Tests.Core/Models/Entities/User.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.

Check warning on line 9 in src/BuilderGenerator.Tests.Core/Models/Entities/User.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
}

public partial class User
{
public ICollection<Order> Orders { get; set; } = new List<Order>();
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace BuilderGenerator.IntegrationTests.Core.Models.Enums;
namespace BuilderGenerator.Tests.Core.Models.Enums;

public enum OrderStatus
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# BuilderGenerator.Core #
# BuilderGenerator.Tests.Core

This project defines the object model used in the other framework-specific tests. It is defined as a netstandard2.0 library so that it can be used with the widest variety of consuming projects.
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\BuilderGenerator.Tests.Core\BuilderGenerator.Tests.Core.csproj" />
<ProjectReference Include="..\BuilderGenerator.Tests.Unit\BuilderGenerator.Tests.Unit.csproj" />
<ProjectReference Include="..\BuilderGenerator\BuilderGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.4.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="Shouldly" Version="4.2.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\BuilderGenerator.IntegrationTests.Core\BuilderGenerator.IntegrationTests.Core.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using BuilderGenerator.Tests.Core.Models.Entities;

namespace BuilderGenerator.Tests.Integration.Net60.Builders;

[BuilderFor(typeof(CollectionTypesSample))]
public partial class CollectionTypeSampleBuilder
{
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System;
using System.Collections.Generic;
using BuilderGenerator.IntegrationTests.Core.Models.Entities;
using BuilderGenerator.Tests.Core.Models.Entities;

namespace BuilderGenerator.IntegrationTests.Net60.Builders;
namespace BuilderGenerator.Tests.Integration.Net60.Builders;

[BuilderFor(typeof(Order), true)]
public partial class OrderBuilder
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using BuilderGenerator.IntegrationTests.Core.Models.Entities;
using BuilderGenerator.Tests.Core.Models.Entities;

namespace BuilderGenerator.IntegrationTests.Net60.Builders;
namespace BuilderGenerator.Tests.Integration.Net60.Builders;

[BuilderFor(typeof(OrderItem))]
public partial class OrderItemBuilder
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System;
using System.Collections.Generic;
using BuilderGenerator.IntegrationTests.Core.Models.Entities;
using BuilderGenerator.Tests.Core.Models.Entities;

namespace BuilderGenerator.IntegrationTests.Net60.Builders;
namespace BuilderGenerator.Tests.Integration.Net60.Builders;

[BuilderFor(typeof(User))]
public partial class UserBuilder
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# BuilderGenerator.Sample.Net60.FromProject
# BuilderGenerator.Sample.Net70.FromProject

This project provides tests the BuilderGenerator package being used in a .Net 6.0 project using a simple series of inter-related classes defined in the BuiderGenerator.Test.Core project.
This project provides tests the BuilderGenerator package being used in a .Net 7.0 project using a simple series of inter-related classes defined in the BuiderGenerator.Test.Core project.

The reference to the BuilderGenerator is a direct project reference, so it can be used to rapidly check the results of changes, but does not represent the real-world usage of the generator. It tests the functionality of the builder, but not the deployment mechanism.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using System;
using BuilderGenerator.IntegrationTests.Core.Models.Entities;
using BuilderGenerator.IntegrationTests.Net60.Builders;
using BuilderGenerator.Tests.Core.Models.Entities;
using BuilderGenerator.Tests.Integration.Net60.Builders;
using NUnit.Framework;
using Shouldly;

namespace BuilderGenerator.IntegrationTests.Net60.Tests;
namespace BuilderGenerator.Tests.Integration.Net60.Tests;

[TestFixture]
public class BuilderTests
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using System;
using BuilderGenerator.IntegrationTests.Core.Models.Entities;
using BuilderGenerator.IntegrationTests.Core.Models.Enums;
using BuilderGenerator.IntegrationTests.Net60.Builders;
using BuilderGenerator.Tests.Core.Models.Entities;
using BuilderGenerator.Tests.Core.Models.Enums;
using BuilderGenerator.Tests.Integration.Net60.Builders;
using NUnit.Framework;
using Shouldly;

namespace BuilderGenerator.IntegrationTests.Net60.Tests;
namespace BuilderGenerator.Tests.Integration.Net60.Tests;

[TestFixture]
public class OrderBuilderTests
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using System;
using BuilderGenerator.IntegrationTests.Core.Models.Entities;
using BuilderGenerator.IntegrationTests.Net60.Builders;
using BuilderGenerator.Tests.Core.Models.Entities;
using BuilderGenerator.Tests.Integration.Net60.Builders;
using NUnit.Framework;
using Shouldly;

namespace BuilderGenerator.IntegrationTests.Net60.Tests;
namespace BuilderGenerator.Tests.Integration.Net60.Tests;

[TestFixture]
public class UserBuilderTests
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<IsPackable>false</IsPackable>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\BuilderGenerator.Tests.Core\BuilderGenerator.Tests.Core.csproj" />
<ProjectReference Include="..\BuilderGenerator.Tests.Unit\BuilderGenerator.Tests.Unit.csproj" />
<ProjectReference Include="..\BuilderGenerator\BuilderGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.4.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="Shouldly" Version="4.2.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using BuilderGenerator.Tests.Core.Models.Entities;

namespace BuilderGenerator.Tests.Integration.Net70.Builders;

[BuilderFor(typeof(CollectionTypesSample))]
public partial class CollectionTypeSampleBuilder
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using BuilderGenerator.Tests.Core.Models.Entities;

namespace BuilderGenerator.Tests.Integration.Net70.Builders;

[BuilderFor(typeof(Order), true)]
public partial class OrderBuilder
{
public static OrderBuilder Simple()
{
var builder = new OrderBuilder()
.WithId(Guid.NewGuid);

return builder;
}

public static OrderBuilder Typical()
{
var builder = Simple()
.WithItems(
() => new List<OrderItem>
{
OrderItemBuilder.Simple().Build(),
});

return builder;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using BuilderGenerator.Tests.Core.Models.Entities;

namespace BuilderGenerator.Tests.Integration.Net70.Builders;

[BuilderFor(typeof(OrderItem))]
public partial class OrderItemBuilder
{
public static OrderItemBuilder Simple()
{
var builder = new OrderItemBuilder()
.WithId(Guid.NewGuid);

return builder;
}
}
15 changes: 15 additions & 0 deletions src/BuilderGenerator.Tests.Integration.Net70/Builders/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Partial Builders #

This folder contains the hand-written half of any builders. Create static factory methods for each well-known or named object instance you want to create.

## Suggestions ##

In addition to specific factory methods, you should define some general-purpose methods such as:

### Simple/Minimal ###

Creates the simplest, most minimal instance that will pass validation. Only fill in the required fields, and leave everything else at their default values. This can serve as the starting point for other, more specific factory methods. For instance, a minimal Customer may be missing address information, and would have no orders.

### Typical ###

This method should return a "typical" example of the class. For a Customer entity, this might mean that the shipping and billing addresses are filled in, and the Customer has multiple orders in various states.
Loading

0 comments on commit e25b90c

Please sign in to comment.