BufTools.ObjectCreation.FromXmlComments
2.0.1
Prefix Reserved
dotnet add package BufTools.ObjectCreation.FromXmlComments --version 2.0.1
NuGet\Install-Package BufTools.ObjectCreation.FromXmlComments -Version 2.0.1
<PackageReference Include="BufTools.ObjectCreation.FromXmlComments" Version="2.0.1" />
paket add BufTools.ObjectCreation.FromXmlComments --version 2.0.1
#r "nuget: BufTools.ObjectCreation.FromXmlComments, 2.0.1"
// 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.
- 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
- Create an ObjectMother instance:
var mother = new ObjectMother(provider);
- Use the instance to create an object:
var instance = _target.Birth<ExampleModel>();
Product | Versions 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. |
-
.NETStandard 2.1
- BufTools.Extensions.Reflection (>= 2.0.0)
- BufTools.Extensions.XmlComments (>= 2.0.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on BufTools.ObjectCreation.FromXmlComments:
Package | Downloads |
---|---|
BufTools.FluentValidation.TestByExample
Allows testing validators using example values found in the XML |
GitHub repositories
This package is not used by any popular GitHub repositories.
- better error handling and messages