Commit ba6ba56f authored by Andres Käver's avatar Andres Käver
Browse files

langstr readme

parent 174c88e0
Pipeline #827 passed with stages
in 1 minute and 31 seconds
......@@ -53,9 +53,9 @@ namespace DAL.App.EF.Repositories
public override async Task<GpsSessionType> UpdateAsync(GpsSessionType entity, object? userId = null)
{
var domainEntity = Mapper.Map(entity);
// fix the language string - from mapper we get new ones - so duplicate values will be created in db
// load back from db the originals
domainEntity.Name = await RepoDbContext.LangStrs.Include(t => t.Translations).FirstAsync(s => s.Id == domainEntity.NameId);
domainEntity.Name.SetTranslation(entity.Name);
......
......@@ -81,3 +81,45 @@ After scaffolding do global search & replace:
</dd class> => </dd>
asp-validation-summary="ModelOnly" => asp-validation-summary="All"
~~~
## LangStr and CRUD
LangStr with translations only exist at the Domain level. When entity is mapped into DAL.DTO, they are converted into regular strings (if the property type in DAL.DTO is string).
Using current UICulture. Conversion happens automatically, thanks to this line in LangStr:
~~~
// "foo" + new LangStr("bar") => "foobar"
public static implicit operator string(LangStr? l) => l?.ToString() ?? "null";
~~~
So this Domain->DAL->BLL conversion is nice and automatic.
We will hit some problems on reverse - when mapping from DAL->Domain.
Create is ok, thank to this line in LangStr:
~~~
// langStrProperty = "foobar"
public static implicit operator LangStr(string s) => new LangStr(s);
~~~
This takes the string value from DTO and creates new LangStr. LangStr constructor takes care of setting the translation.
Problems arise in Update. Mapping produces this:
~~~~
entity: {
NameId: '123', // this value depends ofcourse on that: id-s for langstr are included as hidden fields in forms. or restored from db.
Name: {
Id: '0' // so this is new LangStr, and mapper has no idea how to set correct Id here
}
}
~~~~
So EF will just insert new LangStr and LangStrTranslation into db - not updating the already existing LangStrTranslation.
So we need to do this with every LangStr in entity to be updated:
~~~
domainEntity.Name = await RepoDbContext.LangStrs.Include(t => t.Translations).FirstAsync(s => s.Id == domainEntity.NameId);
domainEntity.Name.SetTranslation(entity.Name);
domainEntity.Description = await RepoDbContext.LangStrs.Include(t => t.Translations).FirstAsync(s => s.Id == domainEntity.DescriptionId);
domainEntity.Description.SetTranslation(entity.Description);
~~~
So we attach back the original LangStr from DB, including LangStrTranslations.
And then we update the translation - either existing one is updated or new one is added.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment