8wDlpd.png
8wDFp9.png
8wDEOx.png
8wDMfH.png
8wDKte.png

ASP.NET 中 Dapper 自定义映射设置初始化的正确方法

RandomInsano 2月前

19 0

我有一些付费私人在线课程的家庭作业,其中有一个三层架构小程序。最近我添加了 ASP.NET Core 来替代我的控制台演示层。但我也有客户...

我有一个三层架构的小程序作为一些付费私人在线课程的家庭作业。最近我添加了 ASP.NET Core 作为控制台演示层的替代品。但我还在数据层的 Dapper 程序类中进行了自定义映射:

    namespace CarRental.Data.Managers
{
    public class DapperConfigurationManager
    {
        // METHODS

    public void ConfigureGuidToStringMapping()
    {
        SqlMapper.AddTypeHandler(new GuidToStringTypeHandler());

        SqlMapper.RemoveTypeMap(typeof(Guid));
    }

    public void SetCustomMappingForEntities()
    {
        CustomPropertyTypeMap carMap = new CustomPropertyTypeMap(typeof(Car), PropertyInformation);
        CustomPropertyTypeMap customerMap = new CustomPropertyTypeMap(typeof(CustomerTemp), PropertyInformation);
        CustomPropertyTypeMap dealMap = new CustomPropertyTypeMap(typeof(Deal), PropertyInformation);
        CustomPropertyTypeMap inspectionMap = new CustomPropertyTypeMap(typeof(Inspection), PropertyInformation);
        CustomPropertyTypeMap repairMap = new CustomPropertyTypeMap(typeof(Repair), PropertyInformation);
        CustomPropertyTypeMap mechanicMap = new CustomPropertyTypeMap(typeof(Mechanic), PropertyInformation);

        SqlMapper.SetTypeMap(typeof(Car), carMap);
        SqlMapper.SetTypeMap(typeof(CustomerTemp), customerMap);
        SqlMapper.SetTypeMap(typeof(Deal), dealMap);
        SqlMapper.SetTypeMap(typeof(Inspection), inspectionMap);
        SqlMapper.SetTypeMap(typeof(Repair), repairMap);
        SqlMapper.SetTypeMap(typeof(Mechanic), mechanicMap);
    }

    public static PropertyInfo PropertyInformation(Type type, string attribName)
    {
        // BECAUSE THE METHOD IS STRONGLY TYPED, I CANNOT MOVE IT IN FROM THE OUTSIDE.
        Dictionary<string, string> columnProperties = new Dictionary<string, string>
        {
            { "carCarId", "CarId" },
            { "carVinCode", "VinCode" },
            { "carNumberPlate", "NumberPlate" },
            { "carBrand", "Brand" },
            { "carModel", "Model" },
            { "carPrice", "Price" },
            { "carNumberOfSeats", "NumberOfSeats" },
            { "carNumberOfDoors", "NumberOfDoors" },
            { "carMileage", "Mileage" },
            { "carMaxFuelCapacity", "MaxFuelCapacity" },
            { "carCurrentFuel", "CurrentFuel" },
            { "carYear", "Year" },
            { "carIsFitForUse", "IsFitForUse" },
            { "carEngine", "Engine" },
            { "carTransmission", "Transmission" },
            { "carInterior", "Interior" },
            { "carWheels", "Wheels" },
            { "carLights", "Lights" },
            { "carSignal", "Signal" },
            { "carColor", "Color" },
            { "carStatusId", "Status" },
            { "userIdNumber", "IdNumber" },
            { "userFirstName", "FirstName" },
            { "userLastName", "LastName" },
            { "userDateOfBirth", "DateOfBirth" },
            { "userUserName", "UserName" },
            { "userPassword", "Password" },
            { "userPassportNumber", "PassportNumber" },
            { "userDrivingLicenseNumber", "DrivingLicenseNumber" },
            { "userBasicDiscount", "BasicDiscount" },
            { "userCategory", "Category" },
            { "dealId", "Id" },
            { "dealCarId", "CarId" },
            { "dealVinCode", "VinCode" },
            { "dealCustomerId", "CustomerId" },
            { "dealPrice", "Price" },
            { "dealDealType", "DealType" },
            { "dealName", "Name" },
            { "inspectionInspectionId", "InspectionId" },
            { "inspectionCarId", "CarId" },
            { "inspectionInspectorId", "InspectorId" },
            { "inspectionInspectionDate", "InspectionDate" },
            { "inspectionStatusId", "Result" },
            { "repairId", "Id" },
            { "repairDate", "Date" },
            { "repairCarId", "CarId" },
            { "repairMechanicId", "MechanicId" },
            { "repairIsSuccessfull", "IsSuccessfull" },
            { "repairTotalCost", "TotalCost" },
            { "repairTechnicalInfo", "TechnicalInfo" },
            { "mechanId", "Id" },
            { "mechanName", "Name" },
            { "mechanSurename", "Surename" },
            { "mechanYear", "Year" }
        };

        if (columnProperties.ContainsKey(attribName))
        {
            return type.GetProperty(columnProperties[attribName]);
        }

        return type.GetProperty(attribName);
    }
}

}

我曾尝试过这样实现它:

    namespace CarRental.WebApi
{
    public class Program
    {
        public static void Main(string[] args)
        {
            // TO CONFIGURE ORM FOR Car-CLASS.

        var carServiceManager = new ServiceManager();

        carServiceManager.InitializeManagment();

        carServiceManager.SupplementData.DapperConfigs.ConfigureGuidToStringMapping();
        carServiceManager.SupplementData.DapperConfigs.SetCustomMappingForEntities();

        var builder = WebApplication.CreateBuilder(args);

        // Add services to the container.

        builder.Services.AddControllers();
        // Learn more about configuring Swagger/OpenAPI at aka.ms/aspnetcore/swashbuckle
        builder.Services.AddEndpointsApiExplorer();
        builder.Services.AddSwaggerGen();
        builder.Services.AddTransient<ServiceManager>(x => new ServiceManager());

        var app = builder.Build();

        // Configure the HTTP request pipeline.
        if (app.Environment.IsDevelopment())
        {
            app.UseSwagger();
            app.UseSwaggerUI();
        }

        app.UseHttpsRedirection();

        app.UseAuthorization();


        app.MapControllers();

        app.Run();
    }
}

}

但是老师说这不行,因为 ServiceManager 实例已创建,并且它可以存活很长时间等等。但她也没有说如何以正确的方式实现它... :SI 从未做过程序员,我只学过“基础 C# 课程”,所以我不知道将其实现到 ASP.NET Core 中的“正确方法”是什么,而明天是家庭作业的截止日期!

— 那么在 ASP.NET 中实现自定义映射的正确方法是什么,因为没有这种映射,我就无法从远程数据库获取数据……

PS 这个到处都是“var”的默认模板有点令人困惑...很难理解源代码中发生了什么,因为看不到类型。

帖子版权声明 1、本帖标题:ASP.NET 中 Dapper 自定义映射设置初始化的正确方法
    本站网址:http://xjnalaquan.com/
2、本网站的资源部分来源于网络,如有侵权,请联系站长进行删除处理。
3、会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。
4、本站一律禁止以任何方式发布或转载任何违法的相关信息,访客发现请向站长举报
5、站长邮箱:yeweds@126.com 除非注明,本帖由RandomInsano在本站《asp.net》版块原创发布, 转载请注明出处!
最新回复 (0)
  • 您并没有真正展示如何使用 Dapper,但 DapperConfigurationManager 类似乎无关紧要。Dapper 的唯一目的是获取您的 SQL 字符串并将其映射到强类型类。您的 ServiceManager 类中应该有类似这样的内容:context.Query(sqlString,new{myParam = myParam});

返回
作者最近主题: