nightmaregaurav.autoassignment
2.0.0
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
<PackageReference Include="nightmaregaurav.autoassignment" Version="2.0.0" />
paket add nightmaregaurav.autoassignment --version 2.0.0
#r "nuget: nightmaregaurav.autoassignment, 2.0.0"
// 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
- DTO (Data Transfer Objects) Mapping
- Entity Framework: Entity to ViewModel Mapping
- Copying Data Between Similar Objects
- Object Cloning and Prototyping
- Unit Testing
Getting Started
To get started with AutoAssignment, follow these simple steps:
- Install the AutoAssignment package from NuGet.
- Import the
AutoAssignment
namespace in your C# file.using AutoAssignment;
- Define relationships between types using the static methods
DefineCreation
andDefineAssignment
somewhere in yourstartup.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; });
- 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 | Versions 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. |
-
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.