Skip to content

surivalluri/obviously

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

77 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Obviously

Semantic Types

NuGet GitHub license

Create semantic types in seconds

Installation

Install this NuGet package Obviously.SemanticTypes (currently in preview).

Usage

Basic

The functionality can be easily used. Declare a partial class and add the attribute SemanticType. The only parameter of this attribute is the actual type of the semantic type. Here it is string.

[SemanticType(typeof(string))]
public partial class EmailAddress { }

⚠ The class must have the partial modifier
ℹ The class can be sealed

What's getting generated

This generator creates

  • The public constructor with a single parameter of the actual type
  • The implementations of
    • the comparable and equatable pattern
    • explicit operator for the actual type.

ℹ This and the others packages are compile-time dependencies. So the compiled assembly does not contain any references on one of the NuGet packages. Even the SemanticType attribute is not in the compiled assembly

Code Generation Example

For details what is getting generated, see Docs/code_generation.md

Advanced

Validation

The input value of the constructor can be validated. Therefore a static method named IsValid has to be implemented. This method must only have a single parameter of the actual type and must have the return type bool.

If the value is not valid, an instance of the semantic type cannot be created.

Validation Example

The example shows the validation of an email address.

[SemanticType(typeof(string))]
public partial class EmailAddress
{
    public static bool IsValid(string value)
    {
        return value.Contains('@');
    }
}

Contribution

  • Create a fork and make a Pull Request
  • Submit a bug
  • Submit an idea

Credits

License

This project is licensed under the MIT License - see the MIT file for details

About

Roslyn code generator for semantic types

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 100.0%