Basics (How does an enterprise do it?) DAL Entity mapper

Currently I am doing a web app since my last small project in the same field (web app in C#) I wanted to ask a difference between (In DAL or even BL) I will be doing my entities like this
public interface IEntityMapper<in TEntity, TKey>
where TEntity : BaseEntity<TKey>
{
void MapToExistingEntity(TEntity existingEntity, TEntity newEntity);
}
public interface IEntityMapper<in TEntity, TKey>
where TEntity : BaseEntity<TKey>
{
void MapToExistingEntity(TEntity existingEntity, TEntity newEntity);
}
using ZelnyTrh.EF.DAL.Entities;
namespace ZelnyTrh.EF.DAL.Mappers;

public class ApplicationUserEntityMapper : IEntityMapper<ApplicationUser>
{
public void MapToExistingEntity(ApplicationUser existingEntity, ApplicationUser newEntity)
{
ArgumentNullException.ThrowIfNull(nameof(existingEntity));
ArgumentNullException.ThrowIfNull(nameof(newEntity));

// Map properties from newEntity to existingEntity
existingEntity.Name = newEntity.Name;
}
}
using ZelnyTrh.EF.DAL.Entities;
namespace ZelnyTrh.EF.DAL.Mappers;

public class ApplicationUserEntityMapper : IEntityMapper<ApplicationUser>
{
public void MapToExistingEntity(ApplicationUser existingEntity, ApplicationUser newEntity)
{
ArgumentNullException.ThrowIfNull(nameof(existingEntity));
ArgumentNullException.ThrowIfNull(nameof(newEntity));

// Map properties from newEntity to existingEntity
existingEntity.Name = newEntity.Name;
}
}
If something like... maybe abstract class or something else would work well. I have another question in my last project i was using AutoMapper for my BL mapping. What I heard was Mapperly being faster if you can give some insight. and also to challenge myself for future proofing what would be the most common enterprise environment solution would have been? (if they even chose C# at all)
14 Replies
spit on that Thang CHO BOC
this would be my structure to my DAL
No description
tar
tar2w ago
this looks strange to me. wouldn't you check if the corresponding entry exists (e.g. ...FirstOrDefault(x => x.ID == ...) by which you would fetch the existing entry and then just use a mapping method of this entity instead of creating own mapper classes interfaces and more bloat?
spit on that Thang CHO BOC
Hmm? I thought when creating the database code first i had to somehow map the entity that code in the entities folder What I do is in the Dal at least is to tell existing object = new.object
tar
tar2w ago
you have your dbcontext where you can access the existing entries via linq when you add a new entry, you just instanciate it (as usual), add it to your dbcontexts dbset<T> and then save the changes
spit on that Thang CHO BOC
Yeah I see the problem now... So whole time I was doing nothing It hit me when in my old web app there are no references
tar
tar2w ago
when you apply the users input to create/update an entity, you usually do this directly. ofc, you could add a particular ctor or mapping method within the entity class if it makes things easier for you. but you do not need additional mapper classes. last but not least there are DTOs which you could/would need to map. but also: you could implement the mappings directly in the corresponding entity or DTO classes.
spit on that Thang CHO BOC
Yea I have those in my BL Would you mind to check if some things / better methods are better (it was a uni project of 3 people still got A but janky as hell) In conclusion DAL should only hold entities repository (the things it can interact) and some Basic generation to the empty database?
tar
tar2w ago
you need the migrations and api endpoints
spit on that Thang CHO BOC
migrations are done automatically didnt try yet but i think it works EF icon shows up in every entity i made
tar
tar2w ago
automatically? you need to execute dotnet ef migrations add <name>
spit on that Thang CHO BOC
i meant that sorry
tar
tar2w ago
perhaps they are then auto applied to your db when you have implemented the logic e.g. via
if (app is IApplicationBuilder appBuilder) {
using var serviceScope = appBuilder.ApplicationServices.CreateScope();
serviceScope.ServiceProvider.GetService<AppDbContext>()?.Database.Migrate();
}
if (app is IApplicationBuilder appBuilder) {
using var serviceScope = appBuilder.ApplicationServices.CreateScope();
serviceScope.ServiceProvider.GetService<AppDbContext>()?.Database.Migrate();
}
spit on that Thang CHO BOC
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.Extensions.Configuration;
using System.IO;

namespace ZelnyTrh.EF.DAL
{
public class ApplicationDbFactory : IDesignTimeDbContextFactory<ApplicationDbContext>
{
public ApplicationDbContext CreateDbContext(string[] args)
{
// Determine the base path for the configuration
// Assuming that the EF project is in 'ZelnyTrh.EF' and the 'appsettings.json' is in 'ZelnyTrh'
var basePath = Path.Combine(Directory.GetCurrentDirectory(), "..\\ZelnyTrh");

// Ensure the path is absolute and normalized
basePath = Path.GetFullPath(basePath);

// Check if the 'appsettings.json' file exists at the computed path
if (!File.Exists(Path.Combine(basePath, "appsettings.json")))
{
throw new FileNotFoundException("Could not find 'appsettings.json' at path: " + basePath);
}

// Load configuration from 'appsettings.json'
var configuration = new ConfigurationBuilder()
.SetBasePath(basePath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.Build();

// Retrieve the connection string
var connectionString = configuration.GetConnectionString("DefaultConnection");

// Configure DbContextOptions
var optionsBuilder = new DbContextOptionsBuilder<ApplicationDbContext>();
optionsBuilder.UseNpgsql(connectionString);

return new ApplicationDbContext(optionsBuilder.Options);
}
}
}
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.Extensions.Configuration;
using System.IO;

namespace ZelnyTrh.EF.DAL
{
public class ApplicationDbFactory : IDesignTimeDbContextFactory<ApplicationDbContext>
{
public ApplicationDbContext CreateDbContext(string[] args)
{
// Determine the base path for the configuration
// Assuming that the EF project is in 'ZelnyTrh.EF' and the 'appsettings.json' is in 'ZelnyTrh'
var basePath = Path.Combine(Directory.GetCurrentDirectory(), "..\\ZelnyTrh");

// Ensure the path is absolute and normalized
basePath = Path.GetFullPath(basePath);

// Check if the 'appsettings.json' file exists at the computed path
if (!File.Exists(Path.Combine(basePath, "appsettings.json")))
{
throw new FileNotFoundException("Could not find 'appsettings.json' at path: " + basePath);
}

// Load configuration from 'appsettings.json'
var configuration = new ConfigurationBuilder()
.SetBasePath(basePath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.Build();

// Retrieve the connection string
var connectionString = configuration.GetConnectionString("DefaultConnection");

// Configure DbContextOptions
var optionsBuilder = new DbContextOptionsBuilder<ApplicationDbContext>();
optionsBuilder.UseNpgsql(connectionString);

return new ApplicationDbContext(optionsBuilder.Options);
}
}
}
from the student in my project i see
spit on that Thang CHO BOC
GitHub
GitHub - ThangChoBoc/DALandBL
Contribute to ThangChoBoc/DALandBL development by creating an account on GitHub.

Did you find this page helpful?