Mshwf.Charger 2.0.0

Charger

Charger is an object mapper, for easily mapping between properties in two objects. (Framework version: 4.5)
fore more info, visit: https://github.com/mshwf/Charger

Install-Package Mshwf.Charger -Version 2.0.0
dotnet add package Mshwf.Charger --version 2.0.0
<PackageReference Include="Mshwf.Charger" Version="2.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Mshwf.Charger --version 2.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Charger

What is Charger?

Charger is an object mapper, for easily mapping between properties in two objects.
The library has 4 methods as follows:

  • ChargeFrom (2 overloads)
  • Squeeze&lt;TTarget&gt; (2 overloads)

Get started

For explaining the use of Charger consider these two example classes:

public class Item
{
    public int ItemId { get; set; }
    public string ItemName { get; set; }
    public string Category { get; set; }
    public string Url { get; set; }
    public Category Cat { get; set; }

}
public class ItemViewModel
{
    public int Id { get; set; }
    public string ItemName { get; set; }
    public string Category { get; set; }
    public string Url { get; set; }
    public DateTime DateUpdated { get; set; }
    public CategoryVM CatVm { get; set; }
}

ChargeFrom


  • If you have an object of type ItemViewModel and you want to map its properties to an Item object,
    easily call the ChargeFrom extension method on the target object (ItemViewModel object), like so:
itemVm.ChargeFrom(item);

Similarily, you can do the same with lists:

itemsVm.ChargeFrom(items);

Note:

  • Target's properties that are not exist (with the same name and type) on the source object will not change, so the DateUpdated
    property will not touched by Charger.
  • If the target list contains items, Charger will just add to them from the source.

Attributes

There are 3 attributes to use on the target properties:

  • NotCharged
    Use this attribute on properties you don't want to charge.

  • SourceProperty(propertyName)
    Use this property on target properties that have different names than the source property. for the ItemViewModel
    class you can charge the Id property from the ItemId on the Item class:

[SourceProperty("ItemId")]
public int Id { get; set; }

It will throw a null reference exception if the proprty doesn't exist on the source object, to ignore the exception,
set the attribute's property AlwaysOnSource to false:

[SourceProperty("ItemId", AlwaysOnSource = false)]
public int Id { get; set; }
  • DeepCharging

So far, the charging is achieved for properties that share the same type, but if you want to charge a custom property type
from another custom property type, in the example above:
````CatVmproperty fromCatyou can useDeepChargingattribute, but since they have different names, you&#39;ll need to combine it with theSourceProperty``` attribute to specify the source's property name:

[DeepCharging, SourceProperty("Cat")]
public CategoryVM CatVm { get; set; }

The DeepCharging uses ChargeFrom method internally, so the target property should be initialized (not null),
otherwise, it will throw a null reference exception, as a workaround you can initialize it using auto-properties:

public CategoryVM CatVm { get; set; } = new CategoryVm();

But you can ignore charging it if it's null, by setting NullTargetAction to NullTargetAction.IgnoreCharging.

Squeeze

So far to use Charger you should have an initialized target object.
But if all what you want is getting a fresh object out of another object, you can use Squeeze property that will just
get you a specified object type:

var newItemModel = item.Squeeze<ItemViewModel>();  //for one object
var newItemsModel = items.Squeeze<ItemViewModel>(); //for list of objects

Install

PM&gt; Install-Package Mshwf.Charger

Contact me: m_shawaf at outlook dot com

Charger

What is Charger?

Charger is an object mapper, for easily mapping between properties in two objects.
The library has 4 methods as follows:

  • ChargeFrom (2 overloads)
  • Squeeze&lt;TTarget&gt; (2 overloads)

Get started

For explaining the use of Charger consider these two example classes:

public class Item
{
    public int ItemId { get; set; }
    public string ItemName { get; set; }
    public string Category { get; set; }
    public string Url { get; set; }
    public Category Cat { get; set; }

}
public class ItemViewModel
{
    public int Id { get; set; }
    public string ItemName { get; set; }
    public string Category { get; set; }
    public string Url { get; set; }
    public DateTime DateUpdated { get; set; }
    public CategoryVM CatVm { get; set; }
}

ChargeFrom


  • If you have an object of type ItemViewModel and you want to map its properties to an Item object,
    easily call the ChargeFrom extension method on the target object (ItemViewModel object), like so:
itemVm.ChargeFrom(item);

Similarily, you can do the same with lists:

itemsVm.ChargeFrom(items);

Note:

  • Target's properties that are not exist (with the same name and type) on the source object will not change, so the DateUpdated
    property will not touched by Charger.
  • If the target list contains items, Charger will just add to them from the source.

Attributes

There are 3 attributes to use on the target properties:

  • NotCharged
    Use this attribute on properties you don't want to charge.

  • SourceProperty(propertyName)
    Use this property on target properties that have different names than the source property. for the ItemViewModel
    class you can charge the Id property from the ItemId on the Item class:

[SourceProperty("ItemId")]
public int Id { get; set; }

It will throw a null reference exception if the proprty doesn't exist on the source object, to ignore the exception,
set the attribute's property AlwaysOnSource to false:

[SourceProperty("ItemId", AlwaysOnSource = false)]
public int Id { get; set; }
  • DeepCharging

So far, the charging is achieved for properties that share the same type, but if you want to charge a custom property type
from another custom property type, in the example above:
````CatVmproperty fromCatyou can useDeepChargingattribute, but since they have different names, you&#39;ll need to combine it with theSourceProperty``` attribute to specify the source's property name:

[DeepCharging, SourceProperty("Cat")]
public CategoryVM CatVm { get; set; }

The DeepCharging uses ChargeFrom method internally, so the target property should be initialized (not null),
otherwise, it will throw a null reference exception, as a workaround you can initialize it using auto-properties:

public CategoryVM CatVm { get; set; } = new CategoryVm();

But you can ignore charging it if it's null, by setting NullTargetAction to NullTargetAction.IgnoreCharging.

Squeeze

So far to use Charger you should have an initialized target object.
But if all what you want is getting a fresh object out of another object, you can use Squeeze property that will just
get you a specified object type:

var newItemModel = item.Squeeze<ItemViewModel>();  //for one object
var newItemsModel = items.Squeeze<ItemViewModel>(); //for list of objects

Install

PM&gt; Install-Package Mshwf.Charger

Contact me: m_shawaf at outlook dot com

Release Notes

Adding DeepCharging, NotCharged and SourceProperty attributes.

Dependencies

This package has no dependencies.

Version History

Version Downloads Last updated
2.0.0 273 10/5/2017
1.3.2 245 9/24/2017
1.0.0 485 5/13/2017