I have three-layers architecture mini-program as my hometask of some paid private online courses. Recently I added ASP.NET Core as replacement for my console presentation-lavel. But I also have custom mapping in a class for Dapper-program in the Data-layer:
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);
}
}
}
I have tried to implement it like that:
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();
}
}
}
But the teacher said it is not ‘ok’, because the instance of ServiceManager created and it can live for long time and so on, and so on. But also she said nothing about how to implement it in the right way… :S I never worked as programmer, I have only ‘basic C# course’, so I have no any idea what is the ‘proper way’ to implement it into ASP.NET Core, and tomorrow is the deadline of the hometask!
— So what is the proper way to implement custom mapping into ASP.NET, because without this mapping I cannot obtain the data from my remote Database…
P. S. This default templates with ‘var’ everywhere is kind of confusing… Hard to understand what happening in the source code, because cannot see types.
2