Coder Social home page Coder Social logo

typemerger's Introduction

TypeMerger - Merge two objects into one

TypeMerger is a simple convention-based object merger for .NET Core. It allows two objects of any type to be merged into a new Type. Object properties can be ignored and any collisions can be resolved using a fluent api. The object returned is a new Type that is dynamically generated and loaded using System.Reflection.Emit.

Dependencies

None

How is it used?

Simple usage

This will result in a new object that has All the properties from obj1 and obj2.

var result = TypeMerger.Merge(obj1, obj2);

Ignore Certain Properties

This will result in a new object that has all of the properties from Obj1 and Obj2 Except for SomeProperty from obj1 and AnotherProperty from obj2.

var result = TypeMerger.Ignore(() => obj1.SomeProperty)
                       .Ignore(() => obj2.AnotherProperty)
                       .Merge(obj1, obj2); 

What About Collisions?

If both objects have the same property there is a fluent method that will tell the Merger which object to use for that property. You simply tell the Merger which property to Use.

In this example given obj1 and obj2 that both have SomeProperty, the value from obj2 will be used.

var result = TypeMerger.Use(() => obj2.SomeProperty)
                       .Merge(obj1, obj2);

What about collisions which Use hasn't specified which object's property to use?

If both objects have the same property, and you do not specify which one to Use, then the property from the first object passed to Merge will be used. (Look at the Merge_Types_with_Name_Collision unit test for an example.)

Mix & Match Your Merge

Combining the .Ignore and .Use fluent methods allows you to pick and choose what you want from your objects.

var obj1 = new {
    SomeProperty = "foo",
    SomeAmount = 20,
    AnotherProperty = "yo"
};

var obj2 = new {
    SomeProperty = "bar",
    SomePrivateStuff = "SECRET!!",
    SomeOtherProperty = "more stuff"
};

var result = TypeMerger.Ignore(() => obj1.AnotherProperty)
                       .Use(() => obj2.SomeProperty)
                       .Ignore(() => obj2.SomePrivateStuff)
                       .Merge(obj1, obj2);

The result object will have the following properties and values:

SomeProperty: "bar"
SomeAmount: 20
SomeOtherProperty: "more stuff"

History

The code is based on the original TypeMerger class written by Mark Miller. Updated, enhanced, and now maintained by Kyle Finley.

Original posting: KyleFinley.net/typemerger

typemerger's People

Contributors

jpmorel avatar kfinley avatar lmkz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

typemerger's Issues

IgnoreAllExcept()?

What about IngnoreAllExcept()?

var result = TypeMerger
                  .IgnoreAllExcept(() => obj2.AnotherProperty)
                  .Merge(obj1, obj2);

Result - all properties from obj1 & only obj2.AnotherProperty from obj2.

Do u accept pull requests for this package?

Thank you for the package. Thats something i rly missed in C#

Result object typechecking

Not sure if its possible in C# but what about

var result = TypeMerger<ResultTypeDto>.Merge(obj1, obj2);

If merged type not equal with "ResultTypeDto" cause an error(not sure how to implement it mb via language specific things mb VS Code extension/VS extension)

Use case:

public class ProductWithSalesPersonDto: IProduct {
    public SalesPerson: string;
}
...
var someProducts = 
    from products in _context.Product
    from users in _context.Users
    where products.IsPublished == true || products.UserId == userId
    orderby products.UpdatedAt
    select TypeMerger.TypeMerger<ProductWithSalesPersonDto>.Merge (products, new { users.SalesPerson });

Result type is of type type_type instead of type

I am trying to merge two objects of the same type and I was expecting an object of that type. Instead I retrieved an object of type type_type.

I am using version 2.1 on .NET standard 2.0 with .NET core 3.1

Ignoring base properties does not work?

Example:

class BaseClass {
public string Name {get; set;}
}

class DeveriedClass : BaseClass {
}

var deveriedObject = new DeveriedClass { Name = "Prince Owusu"};

var result = TypeMerger.Ignore(() => deveriedObject .Name )
.Merge(deveriedObject , new {});

Solution: consider replacing
sssdsd

Add the code to NuGet

Can you please add it to NuGet as you promised on your website, that would be awesome.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.