1

I have a situation where a DB field is storing an enumeration value whose underlying type is int, so I'm getting back values like 0, 1, and 2, which should represent MyEnum.Foo, MyEnum.Bar, and MyEnum.Baz. I am calling a repository which returns DTOs in order to get a DTO which contains this value. My question is, where should this int be converted to the enum?

My solution has 3 layers: I call them Data, Infrastructure (basically business logic), and Web (basically presentation). As far as I'm aware, the Data layer shouldn't be aware of the BLL layer (ie. Infrastructure), and yet I'm storing the enumeration in Infrastructure - it is, after all, business logic that 1 means Bar, or whatever.

So I can't do my conversion in the Data layer. Am I supposed to get the int returned by the Data layer from the Business Logic Layer, map it to a BLL object, and do the conversion while mapping? So the mapper would convert the int on the Data DTO to an enum on the BLL model object? Or would it be acceptable for my data repository to just reference Infrastructure.Enums (or wherever I store the enums) and provide a field in the DTO it returns of that enum type, meaning that the mapping would happen in my Data.Repository project?

2

Am I supposed to get the int returned by the Data layer from the Business Logic Layer, map it to a BLL object, and do the conversion while mapping?

Yes - that's what we are doing in our application. I think it's better to store them as int in DB and cast back and forth as per your needs. Although it's an overhead and sometimes you tend to forget the casting. (compiler will do it's job that time :) )

You can also add some logic to your business object to handle Enum so that you don't have to convert each time.
The solution discussed here explains a good way to handle enums - https://stackoverflow.com/questions/2646498/best-method-to-store-enum-in-database

Hope it helps.

  • Thanks. And in terms of how you organize your Business Logic Layer, how many projects do you have for this sort of thing - do you have one project per entity, or one project for all Data -> Presentation model conversions, or something else? – Jez Jul 14 '18 at 19:42
  • You don't need to have separate project just for data conversions like these. In our case, we do it at the business model level. you can have a mapping method which will explicitly map your DB object to Business object or vice-versa. – Ninja In Pajamas Jul 14 '18 at 19:50
  • I don't quite understand... the business model level? In terms of a solution, what project and class would that actually correspond to? – Jez Jul 14 '18 at 19:54
  • Is the mapping method in the domain model itself or some accessing class? An example would be very helpful! – Jez Jul 14 '18 at 20:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.