EdlinSoftware.Generators.ReadOnlyInterface
1.0.1
dotnet add package EdlinSoftware.Generators.ReadOnlyInterface --version 1.0.1
NuGet\Install-Package EdlinSoftware.Generators.ReadOnlyInterface -Version 1.0.1
<PackageReference Include="EdlinSoftware.Generators.ReadOnlyInterface" Version="1.0.1" />
<PackageVersion Include="EdlinSoftware.Generators.ReadOnlyInterface" Version="1.0.1" />
<PackageReference Include="EdlinSoftware.Generators.ReadOnlyInterface" />
paket add EdlinSoftware.Generators.ReadOnlyInterface --version 1.0.1
#r "nuget: EdlinSoftware.Generators.ReadOnlyInterface, 1.0.1"
#:package EdlinSoftware.Generators.ReadOnlyInterface@1.0.1
#addin nuget:?package=EdlinSoftware.Generators.ReadOnlyInterface&version=1.0.1
#tool nuget:?package=EdlinSoftware.Generators.ReadOnlyInterface&version=1.0.1
Read-only interface generator
This repository contains Roslyn generator of read-only interfaces.
Russian version of documentation
Installation
Add EdlinSoftware.Generators.ReadOnlyInterface
NuGet package to your project:
dotnet add package EdlinSoftware.Generators.ReadOnlyInterface
Generation of read-only interface
If you want to generate a read-only interface for your type, you must do the following things:
- Make your type
partial
, - Augment it with
WithReadOnly
attribute fromEdlinSoftware.Generators.ReadOnlyInterface
namespace.
using EdlinSoftware.Generators.ReadOnlyInterface;
[WithReadOnly]
internal partial class SimpleClass
{
public long Id { get; set; }
public string Name { get; set; } = string.Empty;
public DateTime BirthDate { get; set; }
public List<string> Hobbies { get; set; } = new();
}
Properties of generated read-only interface
The generated interface will contain all public readable properties of your type, including inherited properties from base types.
Supported types
You can generate read-only interface for:
- Classes
- Structures
- Records
- Interfaces
[WithReadOnly]
internal partial struct SimpleStruct
{
public long Id { get; set; }
public string Name { get; set; }
}
Nested types
You can generate read-only interface for a nested type. But in this case you must make partial not only the type itself, but all containing types:
internal partial class Container
{
[WithReadOnly]
private partial class Nested
{
public long Id { get; set; }
}
}
Type accessibility
Read-only interface can be generated for a type of any accessibility:
- public
- protected
- internal
- protected internal
- private
By default, generated read-only interface has the same accessibility as its underlying type.
But you also may change accessibility of generated read-only interface. It can be done using Accessibility
property of WithReadOnly
attribute:
partial class AccessibilityContainer
{
[WithReadOnly(Accessibility = ReadOnlyInterfaceAccessibility.Public)]
private partial record PublicAccessibility(long Id);
}
As new read-only interface will be generated inside the same containing type, as your underlying type, it is your responsibility to provide valid interface accessibility for this place.
Name of read-only interface
By default the name of a read-only interface is generated from the name of underlying type by adding ReadOnly
prefix. For example, for a class with name Person
there will be generated a read-only interface with name IReadOnlyPerson
. For an interface with name IEntity
there will be generated a read-only interface with name IReadOnlyEntity
.
But you have an ability to change this prefix. It can be done using Prefix
property of WithReadOnly
attribute:
[WithReadOnly(Prefix = "IFrozen")]
partial class PrefixedClass
{
public string Name { get; set; } = string.Empty;
}
In this case, you are fully responsible for providing a prefix which forms a valid .NET name of type.
Types of properties of read-only interface
Usually types of properties of the read only interface are the same as in the initial class. But there can be a case, when such type has read-only interface itself:
[WithReadOnly]
public partial class EntityClass
{
public long Id { get; set; }
}
[WithReadOnly]
public partial class AnotherClass
{
public EntityClass Entity { get; set; }
}
Here Entity
property of AnotherClass
has type EntityClass
which itself has read-only interface. In this case, generated interface will change type of this property to corresponding read-only interface:
public partial interface IReadOnlyAnotherClass
{
IReadOnlyEntityClass Entity { get; }
}
If you don't want such behavior, you can use NotReplaceWithReadOnly
attribute:
[WithReadOnly]
public partial class AnotherClass
{
[NotReplaceWithReadOnly]
public EntityClass Entity { get; set; }
You can set this attribute to particular properties or to the whole type:
[WithReadOnly]
[NotReplaceWithReadOnly]
public partial class AnotherClass
{
public EntityClass Entity { get; set; }
}
Appreciations
NuGet package icon is created by Freepik on Flaticon
Learn more about Target Frameworks and .NET Standard.
This package has no dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.