WPF使用依赖注入框架AutoMapper

WPF应用中使用AutoMapper和依赖注入框架实现对象映射与依赖管理

1. 准备工作

首先,通过NuGet安装必要的包:

Install-Package AutoMapper
Install-Package Autofac
Install-Package Autofac.Extensions.DependencyInjection
Install-Package Microsoft.Extensions.DependencyInjection

2. 配置AutoMapper

2.1 创建映射配置文件

 
  
// AutoMapperProfile.cs
using AutoMapper;

public class AutoMapperProfile : Profile
{
    public AutoMapperProfile()
    {
        // 配置从ViewModel到Model的映射
        CreateMap()
            .ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.UserId))
            .ForMember(dest => dest.Email, opt => opt.MapFrom(src => src.UserEmail));
    

你可能感兴趣的:(工控上位机项目集合,wpf,面向对象,依赖注入,c#)