Skip to content

Commit

Permalink
Added url builder with only
Browse files Browse the repository at this point in the history
  • Loading branch information
joaofx committed Nov 7, 2023
1 parent 01b601c commit 25689a9
Show file tree
Hide file tree
Showing 22 changed files with 172 additions and 273 deletions.
1 change: 1 addition & 0 deletions samples/MiruNext/src/MiruNext/Features/Orders/List.cshtml
@@ -1,3 +1,4 @@
@using Miru.Core
@model MiruNext.Features.Orders.OrderList.Result

<h3>Orders</h3>
Expand Down
1 change: 1 addition & 0 deletions samples/MiruNext/src/MiruNext/Features/Todos/List.cshtml
@@ -1,3 +1,4 @@
@using Miru.Core
@model MiruNext.Features.Todos.TodoList.Result

<h3>Todos</h3>
Expand Down
@@ -1,3 +1,4 @@
@using Miru.Core
@model TableList.Result

<div class="card mb-4">
Expand Down
23 changes: 10 additions & 13 deletions src/Miru.Fabrication/ConventionExpressionExtensions.cs
@@ -1,19 +1,16 @@
using System;
using System.Linq.Expressions;
using Miru.Fabrication.FixtureConventions;

namespace Miru.Fabrication

namespace Miru.Fabrication;

public static class ConventionExpressionExtensions
{
public static class ConventionExpressionExtensions
public static IfFilterExpression IfPropertyIs<TClass, TProperty>(
this ConventionExpression conventionExpression,
Expression<Func<TClass, TProperty>> property)
{
public static IfFilterExpression IfPropertyIs<TClass, TProperty>(
this ConventionExpression conventionExpression,
Expression<Func<TClass, TProperty>> property)
{
var accessor = Baseline.Reflection.ReflectionHelper.GetAccessor(property);
var accessor = Baseline.Reflection.ReflectionHelper.GetAccessor(property);

return conventionExpression.IfProperty(p => p.DeclaringType == accessor.OwnerType &&
p.PropertyType == accessor.PropertyType);
}
return conventionExpression.IfProperty(p => p.DeclaringType == accessor.OwnerType &&
p.PropertyType == accessor.PropertyType);
}
}
5 changes: 0 additions & 5 deletions src/Miru.Fabrication/CustomFabricator.cs
@@ -1,8 +1,3 @@
using System;
using System.Collections.Generic;
using AutoFixture;
using Bogus;

namespace Miru.Fabrication;

public abstract class CustomFabricator<TModel, TFabricatorFor> : ICustomFabricator<TModel>
Expand Down
18 changes: 8 additions & 10 deletions src/Miru.Fabrication/EntityFrameworkConvention.cs
@@ -1,18 +1,16 @@
using System.Collections.ObjectModel;
using Miru.Fabrication.FixtureConventions;

namespace Miru.Fabrication
namespace Miru.Fabrication;

public static class EntityFrameworkConvention
{
public static class EntityFrameworkConvention
public static ConventionExpression AddEntityFramework(this ConventionExpression cfg)
{
public static ConventionExpression AddEntityFramework(this ConventionExpression cfg)
{
cfg.IfProperty(p => p.Name.EndsWith("Id")).Ignore();
cfg.IfProperty(p => p.Name.EndsWith("Id")).Ignore();

cfg.IfProperty(p => p.PropertyType.Implements(typeof(Collection<>)) ||
p.PropertyType.ImplementsGenericOf(typeof(Collection<>))).Ignore();
cfg.IfProperty(p => p.PropertyType.Implements(typeof(Collection<>)) ||
p.PropertyType.ImplementsGenericOf(typeof(Collection<>))).Ignore();

return cfg;
}
return cfg;
}
}
2 changes: 0 additions & 2 deletions src/Miru.Fabrication/FabSupport.cs
@@ -1,6 +1,4 @@
using System;
using AutoFixture;
using Bogus;

namespace Miru.Fabrication;

Expand Down
8 changes: 2 additions & 6 deletions src/Miru.Fabrication/FabricatedSession.cs
@@ -1,13 +1,9 @@
using System;
using System.Collections.Generic;
using Baseline;

namespace Miru.Fabrication;

public class FabricatedSession
{
private readonly Dictionary<Type, object> _singletons = new Dictionary<Type, object>();
private readonly List<object> _fabricated = new List<object>();
private readonly Dictionary<Type, object> _singletons = new();
private readonly List<object> _fabricated = new();

public object GetSingleton(Type type)
{
Expand Down
83 changes: 38 additions & 45 deletions src/Miru.Fabrication/FabricationServiceCollectionExtensions.cs
@@ -1,63 +1,56 @@
using System;
using AutoFixture;
using Bogus;
using Microsoft.Extensions.DependencyInjection;
using Miru.Fabrication.FixtureConventions;
namespace Miru.Fabrication;

namespace Miru.Fabrication
public static class FabricationServiceCollectionExtensions
{
public static class FabricationServiceCollectionExtensions
public static IServiceCollection AddFabrication<TFabricator>(
this IServiceCollection services,
Action<ConventionExpression> conventions = null)
where TFabricator : Fabricator
{
public static IServiceCollection AddFabrication<TFabricator>(
this IServiceCollection services,
Action<ConventionExpression> conventions = null)
where TFabricator : Fabricator
{
services.Scan(scan => scan
.FromAssemblies(typeof(TFabricator).Assembly)
.AddClasses(classes => classes.AssignableTo(typeof(ICustomFabricator<>)))
.AsImplementedInterfaces()
.As<ICustomFabricator>()
.WithSingletonLifetime());
services.Scan(scan => scan
.FromAssemblies(typeof(TFabricator).Assembly)
.AddClasses(classes => classes.AssignableTo(typeof(ICustomFabricator<>)))
.AsImplementedInterfaces()
.As<ICustomFabricator>()
.WithSingletonLifetime());

services.AddFabrication(conventions);
services.AddFabrication(conventions);

services.AddSingleton<TFabricator>();
services.ReplaceSingleton<Fabricator>(sp => sp.GetRequiredService<TFabricator>());
services.AddSingleton<TFabricator>();
services.ReplaceSingleton<Fabricator>(sp => sp.GetRequiredService<TFabricator>());

return services;
}
return services;
}

public static IServiceCollection AddFabrication(
this IServiceCollection services,
Action<ConventionExpression> conventions = null)
{
services.AddSingleton<Faker>();
public static IServiceCollection AddFabrication(
this IServiceCollection services,
Action<ConventionExpression> conventions = null)
{
services.AddSingleton<Faker>();

services.AddSingleton<FabricatedSession, FabricatedSession>();
services.AddSingleton<FabricatedSession, FabricatedSession>();

services.AddSingleton(sp =>
{
var fixture = new Fixture();
var faker = sp.GetService<Faker>();
var session = sp.GetService<FabricatedSession>();
services.AddSingleton(sp =>
{
var fixture = new Fixture();
var faker = sp.GetService<Faker>();
var session = sp.GetService<FabricatedSession>();
fixture.AddDefaultMiruConvention(faker);
fixture.AddDefaultMiruConvention(faker);
if (conventions != null)
fixture.AddConvention(faker, conventions);
if (conventions != null)
fixture.AddConvention(faker, conventions);
// TODO: better name for this builder
fixture.Customizations.Add(new FabricationSpecimenBuilder(fixture, session));
// TODO: better name for this builder
fixture.Customizations.Add(new FabricationSpecimenBuilder(fixture, session));
return fixture;
});
return fixture;
});

services.AddSingleton<FabSupport>();
services.AddSingleton<FabSupport>();

services.AddSingleton<Fabricator>();
services.AddSingleton<Fabricator>();

return services;
}
return services;
}
}
101 changes: 49 additions & 52 deletions src/Miru.Fabrication/FabricationSpecimenBuilder.cs
@@ -1,71 +1,68 @@
using System.Reflection;
using AutoFixture;
using AutoFixture.Kernel;
using Miru.Domain;
using Miru.Fabrication.FixtureConventions;

namespace Miru.Fabrication

namespace Miru.Fabrication;

public class FabricationSpecimenBuilder : ISpecimenBuilder
{
public class FabricationSpecimenBuilder : ISpecimenBuilder
{
private readonly Fixture _fixture;
private readonly FabricatedSession _session;
private readonly Fixture _fixture;
private readonly FabricatedSession _session;

public FabricationSpecimenBuilder(
Fixture fixture,
FabricatedSession session)
{
_fixture = fixture;
_session = session;
}
public FabricationSpecimenBuilder(
Fixture fixture,
FabricatedSession session)
{
_fixture = fixture;
_session = session;
}

private bool ShouldReturnNoSpecimen(PropertyInfo propertyInfo)
{
return propertyInfo.PropertyType.IsPrimitive ||
propertyInfo.PropertyType.IsValueType ||
propertyInfo.PropertyType == typeof(string);
}
private bool ShouldReturnNoSpecimen(PropertyInfo propertyInfo)
{
return propertyInfo.PropertyType.IsPrimitive ||
propertyInfo.PropertyType.IsValueType ||
propertyInfo.PropertyType == typeof(string);
}

public object Create(object request, ISpecimenContext context)
{
var propertyInfo = request as PropertyInfo;
var typeInfo = request as TypeInfo;
public object Create(object request, ISpecimenContext context)
{
var propertyInfo = request as PropertyInfo;
var typeInfo = request as TypeInfo;

if (propertyInfo == null || ShouldReturnNoSpecimen(propertyInfo))
return new NoSpecimen();
if (propertyInfo == null || ShouldReturnNoSpecimen(propertyInfo))
return new NoSpecimen();

var propertyIsEntity = propertyInfo.PropertyType.Implements<IEntity>();
var propertyIsEntity = propertyInfo.PropertyType.Implements<IEntity>();

if (propertyIsEntity)
{
var singleton = _session.GetSingleton(propertyInfo.PropertyType);
if (propertyIsEntity)
{
var singleton = _session.GetSingleton(propertyInfo.PropertyType);

if (singleton != null)
return singleton;
}
if (singleton != null)
return singleton;
}

object instance;
object instance;

// if (propertyIsEntity)
// {
// MethodInfo method = _fabricator.GetType().GetMethod(nameof(Fabricator.Make));
// MethodInfo generic = method.MakeGenericMethod(propertyInfo.PropertyType);
// instance = generic.Invoke(this, null);
// }
// else
// {
instance = _fixture.CreateByType(propertyInfo.PropertyType);
// }
// if (propertyIsEntity)
// {
// MethodInfo method = _fabricator.GetType().GetMethod(nameof(Fabricator.Make));
// MethodInfo generic = method.MakeGenericMethod(propertyInfo.PropertyType);
// instance = generic.Invoke(this, null);
// }
// else
// {
instance = _fixture.CreateByType(propertyInfo.PropertyType);
// }

// var instance = _fabricator.Make(propertyInfo.PropertyType);
// var instance = _fabricator.Make(propertyInfo.PropertyType);

// if (instance is OmitSpecimen)
// return new NoSpecimen();
// if (instance is OmitSpecimen)
// return new NoSpecimen();

if (propertyIsEntity && instance != null && instance is not OmitSpecimen)
_session.AddSingleton(propertyInfo.PropertyType, instance);
if (propertyIsEntity && instance != null && instance is not OmitSpecimen)
_session.AddSingleton(propertyInfo.PropertyType, instance);

return instance;
}
return instance;
}
}
3 changes: 0 additions & 3 deletions src/Miru.Fabrication/Fabricator.cs
@@ -1,8 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using AutoFixture;
using Bogus;
using Microsoft.Extensions.DependencyInjection;
using Miru.Fabrication.FixtureConventions;

Expand Down
@@ -1,8 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;
using Bogus;

namespace Miru.Fabrication.FixtureConventions
{
Expand Down
2 changes: 0 additions & 2 deletions src/Miru.Fabrication/FixtureConventions/FixtureExtensions.cs
@@ -1,9 +1,7 @@
using System;
using System.Linq;
using System.Text;
using AutoFixture;
using AutoFixture.Kernel;
using Bogus;

namespace Miru.Fabrication.FixtureConventions
{
Expand Down
@@ -1,7 +1,6 @@
using System;
using System.Linq.Expressions;
using System.Reflection;
using Bogus;

namespace Miru.Fabrication.FixtureConventions
{
Expand Down
3 changes: 0 additions & 3 deletions src/Miru.Fabrication/FixtureExtensions.cs
@@ -1,8 +1,5 @@
using System.Collections.Generic;
using Ardalis.SmartEnum;
using AutoFixture;
using Baseline.Dates;
using Bogus;
using Microsoft.AspNetCore.Http;
using Miru.Domain;
using Miru.Fabrication.FixtureConventions;
Expand Down
13 changes: 13 additions & 0 deletions src/Miru.Fabrication/GlobalUsings.cs
@@ -0,0 +1,13 @@
global using Microsoft.Extensions.DependencyInjection;
global using System;
global using Baseline;
global using Miru.Core;
global using Miru;
global using Miru.Domain;
global using System.Threading;
global using System.Threading.Tasks;
global using System.Collections.Generic;
global using AutoFixture;
global using Bogus;
global using System.Linq.Expressions;
global using Miru.Fabrication.FixtureConventions;
2 changes: 0 additions & 2 deletions src/Miru.Fabrication/ICustomFabricator.cs
@@ -1,5 +1,3 @@
using System.Collections.Generic;

namespace Miru.Fabrication;

public interface ICustomFabricator
Expand Down

0 comments on commit 25689a9

Please sign in to comment.