question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Fluent API instead of MapValue attribute

See original GitHub issue

Hi,

is it possible to define enum mappings WITHOUT using attributes, maybe with some kind of fluent API? The only possibility I see right now is to use the MapValue attribute:

public enum Gender
{
	[MapValue("M")] Male,
	[MapValue("F")] Female,
	[MapValue("U")] Unknown,
	[MapValue("O")] Other,
}

But I don’t like this approach (see my longer explanation below). Thank you in advance for any kind of help! Best regards,

Boris Turk

Our team is developing a bigger ERP system and we decided to use the onion architecture (together with dependency injection):

https://jeffreypalermo.com/2008/07/the-onion-architecture-part-1/

The inner part of our onion architecture are so-called domain objects. These are more or less just simple DTO classes (like Article, Address, Service) and some common enums, e.g.:

public enum DocumentPostingFlag
{
	[StorageValue(" ")]
	None,

	[StorageValue("O")]
	Open,

	[StorageValue("V")]
	Posted,
}

StorageValue is our own attribute, basically the same thing as MapValue. And here comes the problem: we would like to keep our core assemblies (like domain objects) independent of linq2db assembly. This kind of architectural rule looks quite restrictive at first glance, but it pays off in the long term. Most of our domain logic stays decoupled from a specific persistence technology. One day we might be forced to switch to another ORM provider and it will be easier to do that if we don’t make the majority of our system dependent on third-party code.

We were initially playing with the idea that we let our core assemblies depend on linq2db, but ensure that we keep the dependencies at minimum - i.e. only use the MapValue attributes and keep the rest of linq2db initialization (fluent API rules) in one of the marginal assemblies, at the boundary of onion architecture. But I don’t like this approach as it requires us to constantly monitor that the dependencies did not go out of control. This is hard to ensure in a team of 10 developers.

Let me point out that we were already able to isolate the majority of linq2db specifics into its own assembly which is defined at the boundary of our architecture. The only part of the puzzle that is still causing problems is MapValue.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:21 (18 by maintainers)

github_iconTop GitHub Comments

1reaction
MaceWinducommented, Dec 8, 2022

Not at all. Completely forgot about it and you reminded me to fix it

0reactions
dotarjcommented, Dec 8, 2022

Wow, thanks for the amazingly fast response! And sorry for not checking out the issues before asking.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to map a value object identity in EF6 using fluent api?
In EF6, I can map types like these using the code below: modelBuilder.ComplexType<SomeType>() .Property(x => x.SomeProperty) ...
Read more >
Fluent API - Configuring and Mapping Properties and Types
This article is designed to demonstrate how to use the fluent API to configure properties. The code first fluent API is most commonly ......
Read more >
Configure Property Mappings using Fluent API in EF Code- ...
The Fluent API can be used to configure properties of an entity to map it with a db column. Using Fluent API, you...
Read more >
The Fluent API HasValue Method
The Entity Framework Core Fluent API HasValue method is used to configure discriminator values for entities in an inheritance hierarchy when a table...
Read more >
AutoMapper Map value from source with method. Id - Lianmart
Automapper set value. – Merenzo. What I am doing now is post-process the mapped instance. CreateMap<VehicleEntity, VehicleDto> () . And if I add...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found