nightmaregaurav.autoassignment 2.0.0

Additional Details

Use the extension method for each class instead and store those files in a Mapping folder for better code organization.

User.cs
class User {
   public string Id { get; set;}
   public string Name { get; set;}
   public string PasswordHash { get; set;}
}

UserViewModel.cs
class UserViewModel {
   public string Id { get; set;}
   public string Name { get; set;}
}

UserMapper.cs
static class UserMapper {
   public UserViewModel ToUserViewModel(this User source)
   {
       return new UserViewModel(){
           Id = source.Id,
           Name = source.name
       }
   }

   // Note: This method is only for demonstration, this should not be created and used in real applications as this will contain business logics which we don't want here.
   public User ToUser(this UserViewModel source)
   {
       return new User(){
           Id = source.Id,
           Name = source.name,
           PasswordHash = ....
       }
   }
}

dotnet add package nightmaregaurav.autoassignment --version 2.0.0                
NuGet\Install-Package nightmaregaurav.autoassignment -Version 2.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="nightmaregaurav.autoassignment" Version="2.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add nightmaregaurav.autoassignment --version 2.0.0                
#r "nuget: nightmaregaurav.autoassignment, 2.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install nightmaregaurav.autoassignment as a Cake Addin
#addin nuget:?package=nightmaregaurav.autoassignment&version=2.0.0

// Install nightmaregaurav.autoassignment as a Cake Tool
#tool nuget:?package=nightmaregaurav.autoassignment&version=2.0.0                

AutoAssignment - Simplified Object Assignment Library

A Light weight and simple alternative to AutoMapper

AutoAssignment is a lightweight and easy-to-use library that simplifies the process of object assignment and creation in C#. It provides a generic approach to handle complex object assignments without the need for writing repetitive code.

Features

  • Assign values from one object to another effortlessly.
  • Create new objects based on the source object's data.
  • Supports any type of objects with flexible assignment rules.

Use Cases

  1. DTO (Data Transfer Objects) Mapping
  2. Entity Framework: Entity to ViewModel Mapping
  3. Copying Data Between Similar Objects
  4. Object Cloning and Prototyping
  5. Unit Testing

Getting Started

To get started with AutoAssignment, follow these simple steps:

  1. Install the AutoAssignment package from NuGet.
  2. Import the AutoAssignment namespace in your C# file.
    using AutoAssignment;
    
  3. Define relationships between types using the static methods DefineCreation and DefineAssignment somewhere in your startup.cs or equivalent program entrypoint.
    Assignment<SourceClass, DestinationClass>.DefineCreation(source => new DestinationClass(source.Prop1, source.Prop2+1));
    Assignment<SourceClass, DestinationClass>.DefineAssignment((source, destination) =>
    {
       destination.Name = source.Name;
       destination.Age = source.Age;
       // Add more assignments as needed
    
       return destination;
    });
    
  4. Start using AutoAssignment to create new objects or assign values from one object to another.
    // Create a new DestinationClass instance based on the SourceClass object
    var destinationObject = Assignment<SourceClass, DestinationClass>.From(sourceObject);
    
    // Assign values from the sourceObject to an existing destinationObject
    destinationObject = Assignment<SourceClass, DestinationClass>.From(sourceObject, destinationObject);
    

Contributions and Issues

Contributions to AutoAssignment are always welcomed and highly appreciated. If you find any issues or have suggestions for improvements, please feel free to open an issue or submit a pull request on the GitHub Repository.

License

AutoAssignment is free and open source, distributed under the MIT License. See the LICENSE file for more details.

This library was made with ❤️ by NightmareGaurav.

Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net7.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.0 261 3/6/2024 2.0.0 is deprecated.
1.0.2 570 8/17/2023
1.0.1 643 8/5/2023