10000 GitHub - Tim-Maes/Facet at v1.1.1
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
/ Facet Public

Source generator that instantly scaffolds DTOs, ViewModels and typed LINQ projections without any runtime overhead.

License

Notifications You must be signed in to change notification settings

Tim-Maes/Facet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

50 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

This is a new project, any help, feedback and contributions are highy appreciated.

Facet

"One part of a subject, situation, etc. that has many parts."

Facet is a compile-time source generator that creates partial classes from existing types — by copying only the members you want.

Exclude properties, include public fields, add your own members, or generate a constructor — all with a single [Facet] attribute.

Use Facet to build lightweight DTOs, API shapes, or UI-bound view models without writing boilerplate or mapping code.


Quick Start

1. Install

dotnet add package Facet

2. Create your facet

Define a partial class, attach the Facet attribute and you're done.

using Facet;

public class Person
{
    public string Name { get; set; }
    public string Email { get; set; }
    public int Age { get; set; }
    public Guid RawId;
}

// Generate class that can be mapped from source model

[Facet(typeof(Person), GenerateConstructor = true)]
public partial class PersonDto { }

// Generate class while adding and removing properties
// You can pass an array of properties to exclude

[Facet(typeof(Person), exclude: nameof(Person.Email)]
public partial class PersonWithNote 
{
    public string Note { get; set; }
}

The PersonDto will have a constructor that maps the source type properties.

PersonWithNote is generated and will not have the Email property, but will have the Note property.

3. Usage

var person = new Person
{
    Name = "Tim",
    Email = "hidden@example.com",
    Age = 33,
    RawId = Guid.NewGuid()
};

var dto = new PersonDto(person);

Complex mapping support

If you want to map properties with custom logic, you can use the Facet.Mapping package.

1. Install the mapping package

dotnet add package Facet.Mapping

2. Define your models

Source model:

public class User
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime Registered { get; set; }
}

Target facet:

[Facet(
    typeof(User),
    exclude: new[] { nameof(User.FirstName), nameof(User.LastName), nameof(User.Registered) },
    Configuration = typeof(UserMapper))]
public partial class UserDto
{
    public string FullName { get; set; }
    public string RegisteredText { get; set; }
}

3. Create and use map configuration

Mapping configuration from source to your facet:

public class UserMapper : IFacetMapConfiguration<User, UserDto>
{
    public static void Map(User source, UserDto target)
    {
        target.FullName = $"{source.FirstName} {source.LastName}";
        target.RegisteredText = source.Registered.ToString("yyyy-MM-dd");
    }
}
var user = new User
{
  FirstName = "Tim",
  LastName = "Maes",
  Registered = new DateTime(2020, 1, 15)
}

var dto = new UserDto(user);

Console.WriteLine(dto.FullName);        // Tim Maes
Console.WriteLine(dto.RegisteredText); // 2020-01-15

About

Source generator that instantly scaffolds DTOs, ViewModels and typed LINQ projections without any runtime overhead.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

0