nickdodd79 / AutoBogus

A C# library complementing the Bogus generator by adding auto creation and population capabilities.
MIT License
436 stars 50 forks source link

Non-Generic Generate support #75

Open gkruszewski opened 2 years ago

gkruszewski commented 2 years ago

Can we add static non-generic counterparts for the Generate methods that accept a Type to the AutoFaker class?

nickdodd79 commented 2 years ago

Hey @gkruszewski

Shouldn't be difficult to add non-generic support. I will see what I can do.

Nick.

pwhe23 commented 2 years ago

This is the basic approach I've been playing around with:

var client1 = new AutoFaker<Client>().Generate();

is equivalent to:

var fakerType = typeof(AutoFaker<>).MakeGenericType(new[]{typeof(Client)});
var faker = Activator.CreateInstance(fakerType);
var client1 = fakerType.GetMethod("Generate", new[] { typeof(string) }).Invoke(faker, new object?[] { null});
jvmlet commented 1 year ago

I was about to open the same issue. Please expose such API, though MakeGenericMethod approach works, it's better to have it without using reflection.

jvmlet commented 1 year ago

@nickdodd79 , would you accept such PR to AutoGenerateContextExtensions ?

public static object Generate(this AutoGenerateContext context,Type typeToGenerate)
    {
      if (context != null)
      {
        // Set the generate type for the current request
        context.GenerateType =typeToGenerate;

        // Get the type generator and return a value
        var generator = AutoGeneratorFactory.GetGenerator(context);
        return generator.Generate(context);
      }

      return default;
    }
relfman-cmg commented 1 year ago

+1 for this. I need this for unit testing where you have a [Theory]

NinjaCross commented 8 months ago

This is a very, very interesting feature request ! May I ask what's the status :) ?

soenneker commented 7 months ago

@NinjaCross I think this project is abandoned.

Not to worry though!

I recently added this non-type parameter feature to soenneker.utils.autobogus

NinjaCross commented 7 months ago

Thankyou @soenneker !