BufTools.ObjectCreation.FromXmlComments 2.0.1

The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org. Prefix Reserved
dotnet add package BufTools.ObjectCreation.FromXmlComments --version 2.0.1
NuGet\Install-Package BufTools.ObjectCreation.FromXmlComments -Version 2.0.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="BufTools.ObjectCreation.FromXmlComments" Version="2.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add BufTools.ObjectCreation.FromXmlComments --version 2.0.1
#r "nuget: BufTools.ObjectCreation.FromXmlComments, 2.0.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install BufTools.ObjectCreation.FromXmlComments as a Cake Addin
#addin nuget:?package=BufTools.ObjectCreation.FromXmlComments&version=2.0.1

// Install BufTools.ObjectCreation.FromXmlComments as a Cake Tool
#tool nuget:?package=BufTools.ObjectCreation.FromXmlComments&version=2.0.1

Object Mother

This solution provides an ObjectMother class will create an instance of an object and initialize it from the example values in your XML comments.

The same <example> values are used by Swagger to populate object examples (after providing the XML files to Swagger).

example:

public class ExampleModel
{
	/// <summary>
	/// An example of a string property
	/// </summary>
	/// <example>A String!</example>
	public string StringProperty { get; set; }
}

var instance = objectMother.Birth<ExampleModel>();
  • Results in (instance.StringProperty == "A String!")

Getting Started

The simplest use case for the ObjectMother is to new it up and use it:

var mother = new ObjectMother();
var instance = _target.Birth<ExampleModel>();

Ignoring Properties

If you have properties that you don't want populated from XML comments, then you can add an [Attribute] to the property. Then tell the ObjectMother what attributes you want to ignore.

public class ModelWithIgnore
{
	[JsonIgnore]
	public string Ignored { get; set; }

	/// <summary>
	/// An example int
	/// </summary>
	/// <example>99</example>
	public int IntExample { get; set; }
}
	
var mother = new ObjectMother();
mother.IgnorePropertiesWithAttribute<JsonIgnoreAttribute>();
var instance = _target.Birth<ModelWithIgnore>();

Dependency Injection

The ObjectMother uses Activator to create the object. This is fine for creating any object with a parameterless constructor. If such a constructor is not available, then a IServiceProvider instance is needed.

  • If an IServiceProvider is provided and it fails to create the requested object, then Activator is then used.
  1. You may alredy have a service provider available. If not, one can be constructed:
var sc = new ServiceCollection();
sc.AddSingleton<ExampleModel>();
var provider = sc.BuildServiceProvider();
  • BuildServiceProvider is located in the Microsoft.Extensions.DependencyInjection nuget package
  1. Create an ObjectMother instance:
var mother = new ObjectMother(provider);
  1. Use the instance to create an object:
var instance = _target.Birth<ExampleModel>();
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on BufTools.ObjectCreation.FromXmlComments:

Package Downloads
BufTools.FluentValidation.TestByExample The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org.

Allows testing validators using example values found in the XML

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.1 96 5/8/2024
2.0.0 133 5/7/2024
1.1.0 276 1/26/2023
1.0.0 262 1/23/2023

- better error handling and messages