Skip to content

AhmetAtasagun/EasyMapper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

EasyMapper

__ [EN] __

Write, Let It Work! No confusing settings, no tedious details.

No dependency injection!
No predefined class!
No complicated settings!

This library is a mapper built on extensions to get rid of pre-coding pre-configurations and to provide ease of writing. After including the library in your project, you can start coding without any pre-configuration.


__ [TR] __

Yaz, Çalışsın! Kafa karıştırıcı ayarlar yok, uğraştıran detaylar yok.

Bağımlılık enjeksiyonu yok!
Ön tanımlama sınıfı yok!
Karmaşık ayarlar yok!

Bu kütüphane, kodlama öncesi ön yapılandırmalardan kurtulmak ve yazım kolaylığı sağlamak adına uzantılar üzerine kurulmuş bir haritalayıcıdır. Kütüphaneyi projenize dahil ettikten sonra hiçbir ön yapılandırma gerekmeden kodlamaya başlayabilirsiniz.

 
 
 


Add to Project


Nuget Package Manager > Search : "Utilities.EasyMapper"
Select EasyMapper library and install right side  
Static Badge  
 
 
 


Basic Usage



__Manual Mapping __

List<ProductDto> productDtos = context.Products
    .Select(s => new ProductDto {
        ProductID = s.ProductID, // int
        Category = s.Category, // class
        ProductName = s.ProductName, // string
        Discontinued = s.Discontinued, // bool
        UnitPrice = s.UnitPrice, // decimal
        Supplier = s.Supplier, // class
        UnitsInStock = s.UnitsInStock, // short
    }).ToList();

ProductDto productDto = context.Products
    .Select(s => new ProductDto {
        ProductID = s.ProductID, // int
        Category = s.Category, // class
        ProductName = s.ProductName, // string
        Discontinued = s.Discontinued, // bool
        UnitPrice = s.UnitPrice, // decimal
        Supplier = s.Supplier, // class
        UnitsInStock = s.UnitsInStock, // short
    }).FirstOrDefault();

__EasyMapper Mapping (new object) __

List<ProductDto> productDtos = context.Products
    .Include("Category").Include("Supplier")
    .ToMap<ProductDto>().ToList();

ProductDto productDto = context.Products
    .Include("Category").Include("Supplier")
    .FirstOrDefault().ToMap<ProductDto>();

ProductDto productDto = context.Products
    .Include("Category").Include("Supplier")
    .FirstOrDefault().ToMap(typeof(ProductDto));

__EasyMapper Mapping (new and update object) __

public void MyCustomMethod(ProductDto productDto)
{
    Product product = context.Products.FirstOrDefault();
    // productDto fields updated from product not default value fields.
    productDto = product.ToMap(productDto);
}
/* Note : Update, Just for Single Mapping!... */

__EasyMapper Mapping With Options (new object) __

var option = new MapOptions(GenerationLevel.Third, "UnitPrice", "UnitsInStock");
List<ProductDto> productDtos = context.Products.ToMap<ProductDto>(option).ToList();

Note :   Entities (Supplier, Category) under Products must not be null!
     Update ToMap(...), Just for Single Mapping!...

 
 
 


Test Objects



// Getting Relational DB data by EfCore DbContext
NorthwindDbContext context = new NorthwindDbContext(/*ConnectionString*/);

// ------- PRODUCTS -------
List<Product> products = context.Products
    .Include("Category").Include("Supplier")
    .Select(s => new Product {
        ProductID = s.ProductID, // int
        Category = s.Category, // class
        ProductName = s.ProductName, // string
        Discontinued = s.Discontinued, // bool
        UnitPrice = s.UnitPrice, // decimal
        Supplier = s.Supplier, // class
        UnitsInStock = s.UnitsInStock, // short
    }).Take(3).ToList();
/*
    {List<Project.Product>}
        {Project.Product}
            ...
        {Project.Product}
            ...
        {Project.Product}
            ProductID = 5,
            Category = {Project.Category},
            ProductName = "Mishi Kobe Niku",
            Discontinued = true,
            UnitPrice = 21.0000,
            Supplier = {Project.Supplier},
            UnitsInStock = 15
*/
// --------------------------

// ------- CATEGORIES -------
List<Product> products = context.Categories.Include("Products")
    .Select(s => new Category {
        CategoryID = s.CategoryID, // int
        CategoryName = s.CategoryName, // string
        Picture = s.Picture, // byte[]
        Products = s.Products, // {List<Product>}
    }).Take(3).ToList();
/*
    {List<Project.Category>}
        {Project.Category}
            ...
        {Project.Category}
            ...
        {Project.Category}
            CategoryID = 3,
            CategoryName = "Beverages"
            Picture = {byte[10746]}
            Products = {List<Product>}
*/
// -------------------------

// ------- SUPPLIERS -------
List<Product> products = context.Suppliers.Include("Products")
    .Select(s => new Supplier {
        SupplierID = s.SupplierID, // int
        CompanyName = s.CompanyName, // string
        Address = s.Address, // string
        Phone = s.Phone, // string
        Products = s.Products, // {List<Product>}
    }).Take(3).ToList();
/*
    {List<Project.Supplier>}
        {Project.Supplier}
            ...
        {Project.Supplier}
            ...
        {Project.Supplier}
            SupplierID = 4,
            CompanyName = "Pavlova, Ltd."
            Address = "74 Rose St. Moonie Ponds"
            Phone = "(03) 444-2343"
            Products = {List<Product>}
*/
// --------------------------

Note :   ProductDto, Product entity has same fields.
     CategoryDto, Category entity has same fields.
     SupplierDto, Supplier entity has same fields

 
 
 


Detailed Usage



__Single Model Mapping __

TDestination ToMap<TDestination>(this object source)

ProductDto productDto = context.Products
    .FirstOrDefault().ToMap<ProductDto>();
/*
    {Project.ProductDto}
        ProductID = 5,
        Category = null,
        ProductName = "Mishi Kobe Niku",
        Discontinued = true,
        UnitPrice = 21.0000,
        Supplier = null,
        UnitsInStock = 15
*/

Product product = context.Products
    .Include("Category")//.Include("Supplier") => Not Include
    .FirstOrDefault();
    // or
Product product = context.Products
    .Select(s => new Product {
        ProductID = s.ProductID,
        Category = s.Category,
        ProductName = s.ProductName,
        Discontinued = s.Discontinued,
        UnitPrice = s.UnitPrice,
        // Supplier = s.Supplier,
        UnitsInStock = s.UnitsInStock,
    }).FirstOrDefault();
    
ProductDto productDto = product.ToMap<ProductDto>();
/*
    {Project.Product}
        ProductID = 5,
        Category = {Project.Category},
            ...
            Products = null
        ProductName = "Mishi Kobe Niku",
        Discontinued = true,
        UnitPrice = 21.0000,
        Supplier = null,
        UnitsInStock = 15
*/

Product product = context.Products
    .Include("Category").Include("Supplier")
    .FirstOrDefault();

ProductDto productDto = product.ToMap<ProductDto>();
/*
    {Project.Product}
        ProductID = 5,
        Category = {Project.Category},
            ...
            Products = null
        ProductName = "Mishi Kobe Niku",
        Discontinued = true,
        UnitPrice = 21.0000,
        Supplier = {Project.SupplierDto},
            ...
            Products = null
        UnitsInStock = 15
*/

__Single Model Mapping With Options __

TDestination ToMap<TDestination>(this object source, MapOptions options)

Product product = context.Products
    .Include("Category").Include("Supplier")
    .FirstOrDefault();

var option = new MapOptions(GenerationLevel.Third);
ProductDto productDto = product.ToMap<ProductDto>(option);
/*
    {Project.Product}
        ProductID = 5,
        Category = {Project.Category},
            ...
            Products = {List<Project.Product>}
                {Project.Product}
                    ...
                    Supplier = {Project.SupplierDto},
                        ...
                        Products = null,
                    Category = {Project.Category},
                        ...
                        Products = null,
                {Project.Product}
                    ...
                    Supplier = {Project.SupplierDto},
                        ...
                        Products = null,
                    Category = {Project.Category},
                        ...
                        Products = null,
                {Project.Product}
                    ...
                    Supplier = {Project.SupplierDto},
                        ...
                        Products = null,
                    Category = {Project.Category},
                        ...
                        Products = null,
        ProductName = "Mishi Kobe Niku",
        Discontinued = true,
        UnitPrice = 21.0000,
        Supplier = {Project.SupplierDto},
            ...
            Products = {List<Project.Product>}
                {Project.Product}
                    ...
                    Supplier = {Project.SupplierDto},
                        ...
                        Products = null,
                    Category = {Project.Category},
                        ...
                        Products = null,
                {Project.Product}
                    ...
                    Supplier = {Project.SupplierDto},
                        ...
                        Products = null,
                    Category = {Project.Category},
                        ...
                        Products = null,
                {Project.Product}
                    ...
                    Supplier = {Project.SupplierDto},
                        ...
                        Products = null,
                    Category = {Project.Category},
                        ...
                        Products = null,
        UnitsInStock = 15
*/
var option = new MapOptions(GenerationLevel.Second, "UnitPrice", "ProductName");
ProductDto productDto = product.ToMap<ProductDto>(option);
/*
    {Project.Product}
        ProductID = 5,
        Category = {Project.Category},
            ...
            Products = {List<Project.Product>}
                {Project.Product}
                    ...
                    Supplier = null,
                    Category = null,
                {Project.Product}
                    ...
                    Supplier = null,
                    Category = null,
                {Project.Product}
                    ...
                    Supplier = null,
                    Category = null,
        ProductName = "",                       (Ignored)
        Discontinued = true,
        UnitPrice = 0,                          (Ignored)
        Supplier = {Project.SupplierDto},
            ...
            Products = {List<Project.Product>}
                {Project.Product}
                    ...
                    Supplier = null,
                    Category = null,
                {Project.Product}
                    ...
                    Supplier = null,
                    Category = null,
                {Project.Product}
                    ...
                    Supplier = null,
                    Category = null,
        UnitsInStock = 15
*/

__Single Model Update Mapping __

TDestination ToMap<TDestination>(this object source, TDestination destination)

public void MyMethod(ProductFormDto productFormDto)
{
/*
    {Project.ProductFormDto}
        ProductName = "Sushi",
        UnitPrice = 19.0000,
*/
    Product product = context.Products.FirstOrDefault();
/*
    {Project.Product}
        ProductID = 5,
        Category = null,
        ProductName = "Mishi Kobe Niku",
        Discontinued = true,
        UnitPrice = 21.0000,
        Supplier = null,
        UnitsInStock = 15
*/
    product = productFormDto.ToMap(product);
/*
    {Project.Product}
        ProductID = 5,
        Category = null,
        ProductName = "Sushi",      (Updated)
        Discontinued = true,
        UnitPrice = 19.0000,        (Updated)
        Supplier = null,
        UnitsInStock = 15
*/
    context.Products.Add(product);
    context.SaveChanges();
}

//------ If we use normal; --------- 
product = productFormDto.ToMap<Product>();
/*
    {Project.Product}
        ProductID = 0,              (default)
        Category = null,            (default)
        ProductName = "Sushi",
        Discontinued = false,       (default)
        UnitPrice = 19.0000,
        Supplier = null,            (default)
        UnitsInStock = 0            (default)
------------------------------------*/

__List Model Mapping __

IEnumerable<TDestination> ToMap<TDestination>(this object IEnumerable<source>)

List<ProductDto> products = context.Products
    .Select(s => new ProductDto {
        ProductID = s.ProductID, // int
        Category = s.Category, // class
        ProductName = s.ProductName, // string
        Discontinued = s.Discontinued, // bool
        UnitPrice = s.UnitPrice, // decimal
        Supplier = s.Supplier, // class
        UnitsInStock = s.UnitsInStock, // short
    }).ToList();
    // or
List<ProductDto> productDtos = context.Products
    .Include("Category").Include("Supplier")
    .ToMap<ProductDto>().ToList();
/*
    {List<Project.ProductDto>}
        {Project.ProductDto}
            ...
        {Project.ProductDto}
            ...
        {Project.ProductDto}
            ProductID = 5,
            Category = {Project.CategoryDto},
            ProductName = "Mishi Kobe Niku",
            Discontinued = true,
            UnitPrice = 21.0000,
            Supplier = {Project.SupplierDto},
            UnitsInStock = 15
*/
//-------------------------------------------
List<ProductDto> products = context.Products
    .Select(s => new ProductDto {
        ProductID = s.ProductID, // int
        // Category = s.Category, // class
        ProductName = s.ProductName, // string
        Discontinued = s.Discontinued, // bool
        UnitPrice = s.UnitPrice, // decimal
        // Supplier = s.Supplier, // class
        UnitsInStock = s.UnitsInStock, // short
    }).ToList();
    // or
List<ProductDto> productDtos = context.Products
    // .Include("Category").Include("Supplier")  => Not Includes
    .ToMap<ProductDto>().ToList();
/*
    {List<Project.ProductDto>}
        {Project.ProductDto}
            ...
        {Project.ProductDto}
            ...
        {Project.ProductDto}
            ProductID = 5,
            Category = null,
            ProductName = "Mishi Kobe Niku",
            Discontinued = true,
            UnitPrice = 21.0000,
            Supplier = null,
            UnitsInStock = 15
*/

__List Model Mapping With Options __

IEnumerable<TDestination> ToMap<TDestination>(this object IEnumerable<source>, MapOptions options)

var option = new MapOptions(GenerationLevel.Second, "UnitPrice", "UnitsInStock");

List<ProductDto> productDtos = context.Products
    .Include("Category").Include("Supplier")
    .ToMap<ProductDto>(option).ToList();
/*
    {List<Project.ProductDto>}
        {Project.ProductDto}
            ProductID = 5,
            Category = {Project.Category},
                ...
                Products = {List<Project.Product>}
                    {Project.Product}
                        ...
                        Supplier = null,
                        Category = null,
                    {Project.Product}
                        ...
                        Supplier = null,
                        Category = null,
                    {Project.Product}
                        ...
                        Supplier = null,
                        Category = null,
            ProductName = "Mishi Kobe Niku",                       
            Discontinued = true,
            UnitPrice = 0,                          (Ignored)
            Supplier = {Project.SupplierDto},
                ...
                Products = {List<Project.Product>}
                    {Project.Product}
                        ...
                        Supplier = null,
                        Category = null,
                    {Project.Product}
                        ...
                        Supplier = null,
                        Category = null,
                    {Project.Product}
                        ...
                        Supplier = null,
                        Category = null,
            UnitsInStock = 0                        (Ignored)
*/

About

Simple Model to Model or Model to Entity or Entity to Model Automatic Class Mapper

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages