C# toolkit Newbe.Claptrap 0.2.10 released, even more fancy

Newbe.Claptrap 0.2.10 is released, even more bells and whistles.

update content

We have added project descriptions in simplified Chinese:

https://gitee.com/yks/Newbe.ObjectVisitor/blob/main/README_zh_Hans.md

Now, you can modify the value of the attribute through the context:

//✔️ from 0.2
// 可以修改属性
o.V().ForEach((context) => ModifyData(context)).Run();

public static void ModifyData(IObjectVisitorContext<Yueluo,string> context)
{
    context.Value = context.Value.SubString(0,1);
}

Now, multiple ForEach operations are supported:

// ✔️ from 0.2
// 多重 foreach
o.V().ForEach((context)=>{}).ForEach((context)=>{}).Run();

Now, support for more fancy attribute condition judgments:

//✔️ from 0.2
// 遍历指定类型的属性
o.V().ForEach<Yueluo, string>((context) => {});
// 和上一条完全一样
o.V().ForEach<Yueluo, string>((context) => {}, x => x.PropertyType == typeof(string));
// 遍历被标记了 RequiredAttribute 的 string 属性
o.V().ForEach<Yueluo, string>((context) => {}, x => x.PropertyType == typeof(string) && x.GetCustomAttribute<RequiredAttribute>());
// 遍历“是”或者实现了 IEnumerable<int> 接口的属性, 例如 List<int>, int[], IEnumerable<int>, HashSet<int> 等等。
o.V().ForEach<Yueluo, IEnumerable<int>>((context) => {}, x => x.IsOrImplOf<IEnumerable<int>>());
// 指定属性类型,并包含一个扩展的参数
o.V().WithExtendObject<Yueluo, StringBuilder>().ForEach<Yueluo, StringBuilder, string>((context) => {});

 

Benchmarks

We have updated two benchmarks:

  1. The FormatString implementation has been optimized, and now there is almost no difference between Quick Style and your own handwritten ObjectVisitor.
  2. The cost of using ObjectVisitor to modify attribute values ​​is about 1-5 microseconds (one thousandth of a millisecond).

Detailed data can be viewed on the project homepage, here only the chart description is given.

FormatString

MOdifyData

article

Some relevant experience articles have been added:

Sample scene

We have added some scenarios and practices that can use the library to implement functions:

  • Convert the database link string to a data model, or format the data model as a link string.
  • Replace fields in the object that meet the mobile phone number format with cipher text to avoid output of sensitive information.
  • IEnumerable<int> Sum all the realized  properties.

You can refer to " Newbe.ObjectVisitor Sample 1 "

A brief description

Newbe.ObjectVisitor helps developers to access all the attributes of an ordinary class in the simplest and most efficient way. So as to achieve: verification, mapping, collection and other operations.

For example, there is such a simple class in your code.

var order = new OrderInfo();

You want to print out all the attributes and values ​​of this class, then you can use reflection to complete:

for(var pInfo in typeof(OrderInfo).GetProperties())
{
    Console.Writeline($"{pInfo.Name}: {pInfo.GetValue(order)}");
}

If you use this library, you can achieve the same effect in the following ways:

// call .V what is a static extension method
// you get a visitor object for order
var visitor = order.V();

visitor.ForEach(context=>{
    var name = context.Name;
    var value = context.Value;
    Console.Writeline($"{name}: {value}");
}).Run();

// you can also make it into one line
order.V().ForEach(c=> Console.Writeline($"{c.Name}: {c.Value}")).Run();

// or using quick style
order.FormatToString();

Then why should I do this?

  • Because it's faster! This library is implemented using expression trees , so it has 10 times faster performance than using reflection directly.
  • Because it is more readable! Through this library, you can use chain API and naming methods to create a delegate, so that your code can achieve the same readable effect as hard-coded.
  • Because it is more scalable! If you use this class library, you have an easy way to access all the properties of a class. Therefore, you do a lot of things you want to do, such as: create a validator to verify your model, modify some attributes that may contain sensitive data to avoid output to the log, create an object mapper similar to AutoMapper but have Better performance, and so on.

GitHub project address: https://github.com/newbe36524/Newbe.ObjectVisitor

Gitee project address: https://gitee.com/yks/Newbe.ObjectVisitor

Newbe.ObjectVisitor

Guess you like

Origin www.oschina.net/news/119983/newbe-claptrap-0-2-10-released