tags:

views:

238

answers:

5

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?

+3  A: 

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...

Jon Skeet
Yah... what he said ^
Spencer Ruport
Thanks Jon, this seems to be the consensus of most everyone else too. Your blog post was quite informative.
Rake36
A: 

This really depends on how your interactions are going on. The translation of a dal.TransactionCode to bal.TransactionCode will most likely not work in any operation where you are trying to set equality.

If you are truly passing it all the way around and NEED to across all layers, I would either define it at the bal layer, or reference the dal from the UI.

Mitchel Sellers
I was worried about the equality aspect too. And since I don't want the DAL to reference the BAL or the UI to reference the DAL, I'm going to try out the Domain object ideas presented by Jon.
Rake36
+1  A: 

If it were me I would create another project with this kind of stuff in it and have all the projects reference that.

Spencer Ruport
+1  A: 

In these cases, I usually seperate out my "data types" into its own small assembly. Then reference that assembly everywhere you need it.

Jeremy
+2  A: 

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.)

jrista
jrista, I am in fact creating an API (albeit a small one) and I think your answer fits in nicely with Jon's above.
Rake36
Another thing that is useful when creating an API. It makes it easy to move to a SOA later on. The API would contain your DTO's, support types (structs, enums, etc.), as well as your service interfaces. Combine the API with some business components and service implementations, and you have your domain. Combine the API with a service client library (say, a WCF client), and you have a full client support framework. Separating out an API can open a whole new world of compositional and distribution possabilities.
jrista