Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

enums across tiers

Tags:

c#

enums

I'd like to use the same enum across three tiers of my application (presentation -> bal -> dal). I defined the enum in the data layer and I want to use the same enum in the presentation layer (the presentation layer does not have a reference to the data layer). Using someone else's answer to what I think is a similar question, I've built the following enum in the business layer:

namespace bal
{
    public enum TransactionCode
    {
        Accepted = dal.TransactionCode.Accepted,
        AcceptedWithErrors = dal.TransactionCode.AcceptedWithErrors,
        InvalidVendorCredentials = dal.TransactionCode.InvalidVendorCredentials,
        BrokerOffline = dal.TransactionCode.BrokerOffline
    }
}

Is this an appropriate way to construct enums between the tiers?

like image 479
Rake36 Avatar asked Dec 14 '22 04:12

Rake36


2 Answers

One approach is to have one "layer" (well, not really a layer as such) which is shared across all layers.

I blogged about this a while ago (in a blog/project which is now dormant, unfortunately). It may seem "impure" but it makes life a lot easier in many ways and reduces duplication. Of course, it does then reduce flexibility too - if you ever want the presentation enum to be different from the data layer enum, you'd have to refactor...

like image 53
Jon Skeet Avatar answered Dec 30 '22 12:12

Jon Skeet


Your enum is actually part of an API. When you think about layered software, its often hard to think about shared types, but most of the time, a set of types is always shared accross layers. Instead of just thinking about the standard layering:

Presentation -> Business -> Data

Try thinking about it like this:

Presentation -> API
|-> Business ----^
    |-> Data ----^

Where API is a shared aspect of your system. The API contains any shared data types, entities, enumerations, service interfaces, etc. The API can be isolated into its own library, and reused in your presentation, while also being the gateway to your domain (which is business and data logic.)

like image 27
jrista Avatar answered Dec 30 '22 13:12

jrista