Skip to content

How project Lombok in java works and is that possible in .net using attributes?

Project Lombok makes it trivial to implement the boilerplate code in the classes. Is that possible with .NET attributes? Is any .net port there?


Well in Lombok a Java class might look like this

import lombok.Data;

@Data public class Cart {
  private int id;
  private DateTime created;
  private int items;
  private int status;

While in C# the same class would look like this

public class Cart {
  public int Id { get; set; }
  public DateTime Created { get; set; }
  public int Items { get; set; }
  public int Status { get; set; }

So C# (3.0 in this example) gets rather close without any other libraries, but as you start adding “final” to some properties the magic “auto constructor” part of Lombok really shines. As for a .Net alternative, as I understand it the .Net annotations don’t provide the ability to intercept the byte code before it goes to the compiler (what Lombok uses to such great effect), so your options are limited to some template system + a build script like nAnt. This would be a mess to maintain.