In order to test smoothly, you need to make sure that the following prerequisites are installed locally:
We need a test project to demonstrate how to create your first Object Visitor.
First, make sure that dotnet 2.1 or above versions of sdk are installed correctly locally, and you can run the following commands to confirm the currently installed version:
dotnet --info
Then, find a folder you like and run the following commands to create a test console program for demonstration:
dotnet new console
Finally, continue with the following commands to install the latest Newbe.ObjectVisitor package:
dotnet add package newbe.objectvisitor
In this way, the project used for testing is created and completed.
Let's use the IDE to open the project we just created and add a simple data model OrderInfo
:
public class OrderInfo
{
public int OrderId { get; set; }
public string Buyer { get; set; }
public decimal TotalPrice { get; set; }
}
We add the following code in Program.cs to complete these logic:
new
a OrderInfo
order
order
with the StringBuilder
string
using System;
using System.Text;
namespace yueluo_dalao_yes
{
class Program
{
static void Main(string[] args)
{
var order = new OrderInfo
{
OrderId = 1,
Buyer = "yueluo",
TotalPrice = 62359.1478M
};
var sb = new StringBuilder();
sb.AppendFormat("{0}: {1}{2}", nameof(order.OrderId), order.OrderId, Environment.NewLine);
sb.AppendFormat("{0}: {1}{2}", nameof(order.Buyer), order.Buyer, Environment.NewLine);
sb.AppendFormat("{0}: {1}{2}", nameof(order.TotalPrice), order.TotalPrice, Environment.NewLine);
Console.WriteLine(sb.ToString());
}
}
public class OrderInfo
{
public int OrderId { get; set; }
public string Buyer { get; set; }
public decimal TotalPrice { get; set; }
}
}
We use the following commands to run the written code:
dotnet run
It's going to get the following results:.
OrderId: 1
Buyer: yueluo
TotalPrice: 62359.1478
Let's do the same with Newbe.ObjectVisitor to implement the logic:
V()
extension methodForEach
method to register behavior of the Visit processusing System;
using System.Text;
namespace yueluo_dalao_yes
{
class Program
{
static void Main(string[] args)
{
var order = new OrderInfo
{
OrderId = 1,
Buyer = "yueluo",
TotalPrice = 62359.1478M
};
var sb2 = new StringBuilder();
var visitor = order.V()
.ForEach((name, value) => sb2.AppendFormat("{0}: {1}{2}", name, value, Environment.NewLine));
visitor.Run();
Console.WriteLine(sb2.ToString());
}
}
public class OrderInfo
{
public int OrderId { get; set; }
public string Buyer { get; set; }
public decimal TotalPrice { get; set; }
}
}
Running this code, you're going to get the same results as the previous section.
First, the use of Object Visitor can dynamically adapt to changes in model classes, the benefit of which is very obvious.
As properties of OrderInfo
change in the file, the "stitched part" code can be dynamically adapted without change.
Also, there are some benefits that are not embodied in this example and will be introduced in subsequent documents:
Attribute
filtering.It is also possible to use reflection to achieve the above effects, but Object Visitor is implemented in a performance-based manner:
You can see benchmarks for using reflection and object visitor by clicking here
Here, you've got a first look at what Newbe.ObjectVisitor is and its basic usage.
However, this is just demo code and the content is very limited, so you can continue reading the next section to learn more about the features of tension stimulation.
You can also join the following groups to discuss with us: