我有一些付费私人在线课程的家庭作业,其中有一个三层架构小程序。最近我添加了 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”的默认模板有点令人困惑...很难理解源代码中发生了什么,因为看不到类型。