DTO automapping: any benefits to use record instead of class

247 views Asked by At

I have the classic situation: several layers and use DTO classes to pass data between each other.

public class DownloadCenterFileElementViewModel
{
    public int FileID { get; init; }
    public string FileNameFriendly { get; init; }
    public string FileNameUnique { get; init; }
    public long ContentLength { get; init; }
    public DateTime DateLastUpdated { get; init; }
}

public class DownloadCenterFileElementDto
{
    public int FileID { get; init; }
    public string FileNameFriendly { get; init; }
    public string FileNameUnique { get; init; }
    public long ContentLength { get; init; }
    public DateTime DateLastUpdated { get; init; }
}

CreateMap<DownloadCenterFileElementDto, DownloadCenterFileElementViewModel>();

so, I use Automapper to map data and can't use the primary constructor form of record.

Is there anything beneficial about using record instead of class in my case?

1

There are 1 answers

0
longkunz On

The performance comparison between classes and records when using a mapper in C# depends on factors like object size, mutability, mapper implementation, and application context. For small and simple objects, the difference might be negligible. Records can offer efficiency with immutable fields. Mapper library choices and manual implementation impact performance. Practical performance may vary, so testing in the specific application context is crucial.