Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

.net

lombok

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

like image 762
suhair Avatar asked Oct 18 '10 13:10

suhair


1 Answers

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.

like image 195
Jason Sperske Avatar answered Oct 04 '22 16:10

Jason Sperske