Skip to content

SpatialFocus/AddSetter.Fody

Repository files navigation

SpatialFocus.AddSetter.Fody

An add setter Fody plugin.

Nuget Build & Publish FOSSA Status

Adds private setter to properties. To be used for example in combination with read-only properties in EF Core, that would otherwise not be mapped by convention. Read more in our blog post.

Usage

See also Fody usage.

NuGet installation

Install the SpatialFocus.AddSetter.Fody NuGet package and update the Fody NuGet package:

PM> Install-Package Fody
PM> Install-Package SpatialFocus.AddSetter.Fody

The Install-Package Fody is required since NuGet always defaults to the oldest, and most buggy, version of any dependency.

Add to FodyWeavers.xml

Add <SpatialFocus.AddSetter/> to FodyWeavers.xml

<Weavers>
    <SpatialFocus.AddSetter/>
</Weavers>

Overview

Before code:

public class Person
{
    public int Id { get; }

    public string FirstName { get; }

    public string LastName { get; }
}

What gets compiled

public class Person
{
    public int Id { get; private set; }

    public string FirstName { get; private set; }

    public string LastName { get; private set; }
}

Configuration Options

Do not include by default

If no include or exclude namespaces are defined, all classes in the package are included by default. To change this behavior, add the following attribute to the SpatialFocus.AddSetter node in FodyWeavers.

<Weavers>
    <SpatialFocus.AddSetter DoNotIncludeByDefault='True'/>
</Weavers>

Include types with an attribute

To include a specific class you can mark it with a AddSetter. This works in both of the following cases, when either the DoNotIncludeByDefault setting is turned on, or the namespace is excluded.

[AddSetter]
public class ClassToInclude
{
    ...
}

Include or exclude namespaces

These config options are configured by modifying the SpatialFocus.AddSetter node in FodyWeavers.xml

ExcludeNamespaces

A list of namespaces to exclude.

Can take two forms.

As an element with items delimited by a newline.

<SpatialFocus.AddSetter>
    <ExcludeNamespaces>
        Foo
        Bar
    </ExcludeNamespaces>
</SpatialFocus.AddSetter>

Or as a attribute with items delimited by a pipe |.

<SpatialFocus.AddSetter ExcludeNamespaces='Foo|Bar'/>

IncludeNamespaces

A list of namespaces to include.

Can take two forms.

As an element with items delimited by a newline.

<SpatialFocus.AddSetter>
    <IncludeNamespaces>
        Foo
        Bar
    </IncludeNamespaces>
</SpatialFocus.AddSetter>

Or as a attribute with items delimited by a pipe |.

<SpatialFocus.AddSetter IncludeNamespaces='Foo|Bar'/>

Wildcard support

Use * at the beginning or at the end of an in- or exclude for wildcard matching.

To include the namespace and all sub-namespaces, simply define it like this:

<SpatialFocus.AddSetter>
    <IncludeNamespaces>
        Foo
        Foo.*
    </IncludeNamespaces>
</SpatialFocus.AddSetter>

Combination of exclude and include

You can combine excludes and includes, excludes overrule the includes if both match. But classes with an explicit [AddSetter] attribute overrule the excludes.

License

FOSSA Status


Made with ❤️ by Spatial Focus