Method Create

Namespace
Docfx.ResultSnippets
Assembly
Docfx.ResultSnippets.dll

Examples

Example simple class,


    class Person
    {
        public const string TypeName = nameof(Person);
        private static int TypeNameHashCode = StringComparer.Ordinal.GetHashCode(nameof(Person));

        public string? FirstName { get; set; }
        public string? LastName { get; set; }
        public int Age { get; set; }
#pragma warning disable S3264, MA0046
        public event Func<int>? SomeEvent;
#pragma warning restore MA0046, S3264

        public string FullName() => $"{LastName} {LastName}";

        public Person WithFullName(string firstName, string lastName)
        {
            FirstName = firstName;
            LastName = lastName;
            return this;
        }
    }

    [Fact(DisplayName = "Create a ClassDiagram from a simple class")]
    public static async Task Case1()
    {
        var result = ClassDiagramBuilder.Create(
            [typeof(Person)],
            // remove all generated relationships
            relationshipModelTransformer: _ => []
        );
        result.SaveResults();

        await Verify(result).UseDirectory("__snapshots__");
    }

Results in,

classDiagram
class `ClassDiagramTests.Person`
`ClassDiagramTests.Person` : +int Age
`ClassDiagramTests.Person` : +string FirstName
`ClassDiagramTests.Person` : +string LastName
`ClassDiagramTests.Person` : +Func&ltint> SomeEvent
`ClassDiagramTests.Person` : +string TypeName$
`ClassDiagramTests.Person` : -int TypeNameHashCode$
`ClassDiagramTests.Person` : +Equals(object obj) bool
`ClassDiagramTests.Person` : -Finalize() void
`ClassDiagramTests.Person` : +FullName() string
`ClassDiagramTests.Person` : +GetHashCode() int
`ClassDiagramTests.Person` : +GetType() Type
`ClassDiagramTests.Person` : -MemberwiseClone() object
`ClassDiagramTests.Person` : +ToString() string
`ClassDiagramTests.Person` : +WithFullName(string firstName, string lastName) ClassDiagramTests.Person

Example enum,


enum Colour
{
    Red,
    Green,
    Blue,
}

[Fact(DisplayName = "Create a ClassDiagram from an enum")]
public static async Task Case2()
{
    var result = ClassDiagramBuilder.Create([typeof(Colour)]);
    result.SaveResults();

    await Verify(result).UseDirectory("__snapshots__");
}

Results in,

classDiagram
class `ClassDiagramTests.Colour`
<<enumeration>> `ClassDiagramTests.Colour`
`ClassDiagramTests.Colour`: Blue
`ClassDiagramTests.Colour`: Green
`ClassDiagramTests.Colour`: Red

Example interface,


interface IAnimalExample
{
    void Eat();

    string Call();
}

[Fact(DisplayName = "Create a ClassDiagram from an interface")]
public static async Task Case3()
{
    var result = ClassDiagramBuilder.Create([typeof(IAnimalExample)]);
    result.SaveResults();

    await Verify(result).UseDirectory("__snapshots__");
}

Results in,

classDiagram
class `ClassDiagramTests.IAnimalExample`
<<interface>> `ClassDiagramTests.IAnimalExample`
`ClassDiagramTests.IAnimalExample` : +Call() string*
`ClassDiagramTests.IAnimalExample` : +Eat() void*

Example abstract class,


#pragma warning disable S1694
    abstract class BaseAnimal
#pragma warning restore S1694
    {
        protected abstract void Eat();

        protected abstract string Call();
    }

    [Fact(DisplayName = "Create a ClassDiagram from an abstract class")]
    public static async Task Case4()
    {
        var result = ClassDiagramBuilder.Create([typeof(BaseAnimal)]);
        result.SaveResults();

        await Verify(result).UseDirectory("__snapshots__");
    }

Results in,

classDiagram
class `ClassDiagramTests.BaseAnimal`
<<abstract>> `ClassDiagramTests.BaseAnimal`
`ClassDiagramTests.BaseAnimal` : -Call() string*
`ClassDiagramTests.BaseAnimal` : -Eat() void*
`ClassDiagramTests.BaseAnimal` : +Equals(object obj) bool
`ClassDiagramTests.BaseAnimal` : -Finalize() void
`ClassDiagramTests.BaseAnimal` : +GetHashCode() int
`ClassDiagramTests.BaseAnimal` : +GetType() Type
`ClassDiagramTests.BaseAnimal` : -MemberwiseClone() object
`ClassDiagramTests.BaseAnimal` : +ToString() string

Example generic class,


    class GenericClass<T>
    {
        public T? SomeT { get; set; }

#pragma warning disable CA1822
        public IEnumerable<T> MoreT() => [];
#pragma warning restore CA1822
    }

    [Fact(DisplayName = "Create a ClassDiagram from a generic class")]
    public static async Task Case5()
    {
        var result = ClassDiagramBuilder.Create(
            [
                typeof(GenericClass<string>),
                typeof(GenericClass<int>),
                typeof(GenericClass<List<Person>>),
            ]
        );
        result.SaveResults();

        await Verify(result).UseDirectory("__snapshots__");
    }

Results in,

classDiagram
class `ClassDiagramTests.GenericClass&ltint>`
`ClassDiagramTests.GenericClass&ltint>` : +int SomeT
`ClassDiagramTests.GenericClass&ltint>` : +Equals(object obj) bool
`ClassDiagramTests.GenericClass&ltint>` : -Finalize() void
`ClassDiagramTests.GenericClass&ltint>` : +GetHashCode() int
`ClassDiagramTests.GenericClass&ltint>` : +GetType() Type
`ClassDiagramTests.GenericClass&ltint>` : -MemberwiseClone() object
`ClassDiagramTests.GenericClass&ltint>` : +MoreT() IEnumerable&ltint>
`ClassDiagramTests.GenericClass&ltint>` : +ToString() string
class `ClassDiagramTests.GenericClass&ltList&ltClassDiagramTests.Person>>`
`ClassDiagramTests.GenericClass&ltList&ltClassDiagramTests.Person>>` : +List&ltClassDiagramTests.Person> SomeT
`ClassDiagramTests.GenericClass&ltList&ltClassDiagramTests.Person>>` : +Equals(object obj) bool
`ClassDiagramTests.GenericClass&ltList&ltClassDiagramTests.Person>>` : -Finalize() void
`ClassDiagramTests.GenericClass&ltList&ltClassDiagramTests.Person>>` : +GetHashCode() int
`ClassDiagramTests.GenericClass&ltList&ltClassDiagramTests.Person>>` : +GetType() Type
`ClassDiagramTests.GenericClass&ltList&ltClassDiagramTests.Person>>` : -MemberwiseClone() object
`ClassDiagramTests.GenericClass&ltList&ltClassDiagramTests.Person>>` : +MoreT() IEnumerable&ltList&ltClassDiagramTests.Person>>
`ClassDiagramTests.GenericClass&ltList&ltClassDiagramTests.Person>>` : +ToString() string
class `ClassDiagramTests.GenericClass&ltstring>`
`ClassDiagramTests.GenericClass&ltstring>` : +string SomeT
`ClassDiagramTests.GenericClass&ltstring>` : +Equals(object obj) bool
`ClassDiagramTests.GenericClass&ltstring>` : -Finalize() void
`ClassDiagramTests.GenericClass&ltstring>` : +GetHashCode() int
`ClassDiagramTests.GenericClass&ltstring>` : +GetType() Type
`ClassDiagramTests.GenericClass&ltstring>` : -MemberwiseClone() object
`ClassDiagramTests.GenericClass&ltstring>` : +MoreT() IEnumerable&ltstring>
`ClassDiagramTests.GenericClass&ltstring>` : +ToString() string

Example inheritance relationships,


interface ILifeform
{
    void Breethe();
}

interface IAnimal : ILifeform
{
    void Eat();

    string Call();
}

abstract class Bird : IAnimal
{
    public void Eat() => throw new NotSupportedException();

    public abstract string Call();

    public void Breethe() => throw new NotSupportedException();
}

class Duck : Bird
{
    public override string Call() => "Quack Quack";
}

class Swan : Bird
{
    public override string Call() => "Honk Honk";
}

class Dog : IAnimal
{
    public void Eat() => throw new NotSupportedException();

    public string Call() => "Woof Woof";

    public void Breethe() => throw new NotSupportedException();
}

[Fact(DisplayName = "Create a ClassDiagram with inheritance")]
public static async Task Case6()
{
    var result = ClassDiagramBuilder.Create(
        [
            typeof(ILifeform),
            typeof(IAnimal),
            typeof(Bird),
            typeof(Swan),
            typeof(Duck),
            typeof(Dog),
        ]
    );
    result.SaveResults();

    await Verify(result).UseDirectory("__snapshots__");
}

Results in,

classDiagram
`ClassDiagramTests.IAnimal` <|-- `ClassDiagramTests.Bird`
`ClassDiagramTests.IAnimal` <|-- `ClassDiagramTests.Dog`
`ClassDiagramTests.Bird` <|-- `ClassDiagramTests.Duck`
`ClassDiagramTests.ILifeform` <|-- `ClassDiagramTests.IAnimal`
`ClassDiagramTests.Bird` <|-- `ClassDiagramTests.Swan`
class `ClassDiagramTests.Bird`
<<abstract>> `ClassDiagramTests.Bird`
`ClassDiagramTests.Bird` : +Breethe() void
`ClassDiagramTests.Bird` : +Call() string*
`ClassDiagramTests.Bird` : +Eat() void
`ClassDiagramTests.Bird` : +Equals(object obj) bool
`ClassDiagramTests.Bird` : -Finalize() void
`ClassDiagramTests.Bird` : +GetHashCode() int
`ClassDiagramTests.Bird` : +GetType() Type
`ClassDiagramTests.Bird` : -MemberwiseClone() object
`ClassDiagramTests.Bird` : +ToString() string
class `ClassDiagramTests.Dog`
`ClassDiagramTests.Dog` : +Breethe() void
`ClassDiagramTests.Dog` : +Call() string
`ClassDiagramTests.Dog` : +Eat() void
`ClassDiagramTests.Dog` : +Equals(object obj) bool
`ClassDiagramTests.Dog` : -Finalize() void
`ClassDiagramTests.Dog` : +GetHashCode() int
`ClassDiagramTests.Dog` : +GetType() Type
`ClassDiagramTests.Dog` : -MemberwiseClone() object
`ClassDiagramTests.Dog` : +ToString() string
class `ClassDiagramTests.Duck`
`ClassDiagramTests.Duck` : +Breethe() void
`ClassDiagramTests.Duck` : +Call() string
`ClassDiagramTests.Duck` : +Eat() void
`ClassDiagramTests.Duck` : +Equals(object obj) bool
`ClassDiagramTests.Duck` : -Finalize() void
`ClassDiagramTests.Duck` : +GetHashCode() int
`ClassDiagramTests.Duck` : +GetType() Type
`ClassDiagramTests.Duck` : -MemberwiseClone() object
`ClassDiagramTests.Duck` : +ToString() string
class `ClassDiagramTests.IAnimal`
<<interface>> `ClassDiagramTests.IAnimal`
`ClassDiagramTests.IAnimal` : +Call() string*
`ClassDiagramTests.IAnimal` : +Eat() void*
class `ClassDiagramTests.ILifeform`
<<interface>> `ClassDiagramTests.ILifeform`
`ClassDiagramTests.ILifeform` : +Breethe() void*
class `ClassDiagramTests.Swan`
`ClassDiagramTests.Swan` : +Breethe() void
`ClassDiagramTests.Swan` : +Call() string
`ClassDiagramTests.Swan` : +Eat() void
`ClassDiagramTests.Swan` : +Equals(object obj) bool
`ClassDiagramTests.Swan` : -Finalize() void
`ClassDiagramTests.Swan` : +GetHashCode() int
`ClassDiagramTests.Swan` : +GetType() Type
`ClassDiagramTests.Swan` : -MemberwiseClone() object
`ClassDiagramTests.Swan` : +ToString() string

Example member association relationships,


class Teacher
{
    public IEnumerable<Child> Teaches { get; set; } = null!;
}

class Child
{
    public Teacher Teacher { get; set; } = null!;
}

class Classroom
{
    public Teacher Teacher { get; set; } = null!;
    public IEnumerable<Child> Children { get; set; } = null!;
}

class School
{
    public IEnumerable<Classroom> Classrooms { get; set; } = null!;

    public Teacher Headmaster() => Classrooms.Select(x => x.Teacher).First();
}

[Fact(DisplayName = "Create a ClassDiagram with association")]
public static async Task Case7()
{
    var result = ClassDiagramBuilder.Create(
        [typeof(Teacher), typeof(Child), typeof(Classroom), typeof(School)]
    );
    result.SaveResults();

    await Verify(result).UseDirectory("__snapshots__");
}

Results in,

classDiagram
`ClassDiagramTests.Teacher` "1"  <--  "1" `ClassDiagramTests.Child` : Teacher
`ClassDiagramTests.Child` "*"  <--  "1" `ClassDiagramTests.Classroom` : Children
`ClassDiagramTests.Teacher` "1"  <--  "1" `ClassDiagramTests.Classroom` : Teacher
`ClassDiagramTests.Classroom` "*"  <--  "1" `ClassDiagramTests.School` : Classrooms
`ClassDiagramTests.Teacher` "1"  <--  "1" `ClassDiagramTests.School` : Headmaster
`ClassDiagramTests.Child` "*"  <--  "1" `ClassDiagramTests.Teacher` : Teaches
class `ClassDiagramTests.Child`
`ClassDiagramTests.Child` : +ClassDiagramTests.Teacher Teacher
`ClassDiagramTests.Child` : +Equals(object obj) bool
`ClassDiagramTests.Child` : -Finalize() void
`ClassDiagramTests.Child` : +GetHashCode() int
`ClassDiagramTests.Child` : +GetType() Type
`ClassDiagramTests.Child` : -MemberwiseClone() object
`ClassDiagramTests.Child` : +ToString() string
class `ClassDiagramTests.Classroom`
`ClassDiagramTests.Classroom` : +IEnumerable&ltClassDiagramTests.Child> Children
`ClassDiagramTests.Classroom` : +ClassDiagramTests.Teacher Teacher
`ClassDiagramTests.Classroom` : +Equals(object obj) bool
`ClassDiagramTests.Classroom` : -Finalize() void
`ClassDiagramTests.Classroom` : +GetHashCode() int
`ClassDiagramTests.Classroom` : +GetType() Type
`ClassDiagramTests.Classroom` : -MemberwiseClone() object
`ClassDiagramTests.Classroom` : +ToString() string
class `ClassDiagramTests.School`
`ClassDiagramTests.School` : +IEnumerable&ltClassDiagramTests.Classroom> Classrooms
`ClassDiagramTests.School` : +Equals(object obj) bool
`ClassDiagramTests.School` : -Finalize() void
`ClassDiagramTests.School` : +GetHashCode() int
`ClassDiagramTests.School` : +GetType() Type
`ClassDiagramTests.School` : +Headmaster() ClassDiagramTests.Teacher
`ClassDiagramTests.School` : -MemberwiseClone() object
`ClassDiagramTests.School` : +ToString() string
class `ClassDiagramTests.Teacher`
`ClassDiagramTests.Teacher` : +IEnumerable&ltClassDiagramTests.Child> Teaches
`ClassDiagramTests.Teacher` : +Equals(object obj) bool
`ClassDiagramTests.Teacher` : -Finalize() void
`ClassDiagramTests.Teacher` : +GetHashCode() int
`ClassDiagramTests.Teacher` : +GetType() Type
`ClassDiagramTests.Teacher` : -MemberwiseClone() object
`ClassDiagramTests.Teacher` : +ToString() string

Create(Type, ModelTransformer<Assembly>?, Func<Type, bool>?, ModelTransformer<ClassModel>?, ModelTransformer<RelationshipModel>?)

Creates a class diagram from a type including all related types

public static string Create(Type type, ModelTransformer<Assembly>? assemblyTransformer = null, Func<Type, bool>? typeFilter = null, ModelTransformer<ClassModel>? classModelTransformer = null, ModelTransformer<RelationshipModel>? relationshipModelTransformer = null)

Parameters

type Type

type

assemblyTransformer ModelTransformer<Assembly>

optional assembly transformer, can be used to add assemblies other than the current types assembly

typeFilter Func<Type, bool>

optional type filter

classModelTransformer ModelTransformer<ClassModel>

optional class model transformer

relationshipModelTransformer ModelTransformer<RelationshipModel>

optional relationship model transformer

Returns

string

class model

Exceptions

ArgumentException

if no types are provided

Create(IEnumerable<Assembly>, Func<Type, bool>?, ModelTransformer<ClassModel>?, ModelTransformer<RelationshipModel>?)

Creates a class diagram from a provided set of assemblies

public static string Create(IEnumerable<Assembly> assemblies, Func<Type, bool>? typeFilter = null, ModelTransformer<ClassModel>? classModelTransformer = null, ModelTransformer<RelationshipModel>? relationshipModelTransformer = null)

Parameters

assemblies IEnumerable<Assembly>

assemblies

typeFilter Func<Type, bool>

optional type filter

classModelTransformer ModelTransformer<ClassModel>

optional class model transformer

relationshipModelTransformer ModelTransformer<RelationshipModel>

optional relationship model transformer

Returns

string

class model

Exceptions

ArgumentException

if no types are provided

Create(IEnumerable<Type>, ModelTransformer<ClassModel>?, ModelTransformer<RelationshipModel>?)

Creates a class diagram from a provided set of types

public static string Create(IEnumerable<Type> types, ModelTransformer<ClassModel>? classModelTransformer = null, ModelTransformer<RelationshipModel>? relationshipModelTransformer = null)

Parameters

types IEnumerable<Type>

types

classModelTransformer ModelTransformer<ClassModel>

optional class model transformer

relationshipModelTransformer ModelTransformer<RelationshipModel>

optional relationship model transformer

Returns

string

class model

Exceptions

ArgumentException

if no types are provided

Create(IEnumerable<ClassModel>, IEnumerable<RelationshipModel>)

Creates a class diagram from a provided set of models

public static string Create(IEnumerable<ClassModel> classModels, IEnumerable<RelationshipModel> relationshipModels)

Parameters

classModels IEnumerable<ClassModel>

class models

relationshipModels IEnumerable<RelationshipModel>

relationship models

Returns

string

class diagram