Skip to content

SamerAlRawi/DotDiff

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DotDiff

Object comparison and audit library for .net

Examples:

Using Expressions

Using Attributes

Manually Adding KeyPairs

Json Serialization

Expressions Examples

Create a new console application and install the following nuget package

Install-Package DotDiff

Create Class

public class User
{
    public string Email { get; set; }
    public string UserName { get; set; }
    public string Password { get; set; }
    public bool Enabled { get; set; }
    public long Id { get; set; }
    public DateTime LastLogin { get; set; }
}

For Xml serialization add the following line to your Program.cs main:

var user1 = new User{
    //set all properties here
};
var user2 = new User{
    //set all properties here
};

var xml = new XmlAuditBuilder<User>()
                .Audit(user1, user2)
                .Include(_ => _.Email)
                .Include(_ => _.Password)
                .Include(_ => _.UserName)
                .Include(_ => _.Id)
                .Include(_ => _.Enabled)
                .Include(_ => _.LastLogin)
                .Serialize();
            ForegroundColor = ConsoleColor.Green;
            WriteLine(xml);

Attributes Examples

Properties annotated with [Audit] attributes will be tracked for auditing by default. Create a new console application and install the following nuget package

Install-Package DotDiff

Create Class

public class User
{
    [Audit]
    public string Email { get; set; }
    public string UserName { get; set; }
}

For Xml serialization add the following line to your Program.cs main:

var user1 = new User{
    Email = "m1@domain1.com",
    UserName = "user1"
};
var user2 = new User{
    Email = "m2@domain2.com",
    UserName = "user2"
};

var xml = new XmlAuditBuilder<User>()
                .Audit(user1, user2)
                .Serialize();
            ForegroundColor = ConsoleColor.Green;
            WriteLine(xml);

The result will include the email values by default:

<ArrayOfAuditPair>
  <AuditPair>
    <Key>Email</Key>
    <OldValue>m1@domain1.com</OldValue>
    <NewValue>m2@domain2.com</NewValue>
  </AuditPair>
</ArrayOfAuditPair>

Key Pairs Example

For manually added attributes you can use the auditpair overload:

    var xml = new XmlAuditBuilder<User>()
                .Audit(user1, user2)
                .Include(_ => _.Id)
                .Include(
                    new AuditPair{
                        Key = "OtherAttribute123",
                        OldValue = "any value",
                        NewValue = null //or any other value if needed  
                    }
                )
                .Serialize();

Json Example

For Json serialization add the following line to your Program.cs main: all above examples applies just replace XmlAuditBuilder with JsonAuditBuilder

var user1 = new User{
    //set all properties here
};
var user2 = new User{
    //set all properties here
};

var json = new JsonAuditBuilder<User>()
                .Audit(user1, user2)
                .Include(_ => _.Email)
                .Include(_ => _.Password)
                .Include(_ => _.UserName)
                .Include(_ => _.Id)
                .Include(_ => _.Enabled)
                .Include(_ => _.LastLogin)
                .Serialize();
            ForegroundColor = ConsoleColor.Green;
            WriteLine(json);

Future work:

  • collections
  • nested properties and classes

About

Object comparison and audit library for .net

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages