Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exposing enum type defined in a base library

I am trying to revise my logging library. Here is where I'm stuck. I use an enumeration, let's call it ActionType, to identify my operations like UserLogin, PurchaseOrder... hundreds of them. And I use this type in my logger methods. But since I am seperating my logger library from my project specific code in the sake of loose coupling and base library can't access ActionType defined in a project, how can I achieve this. To clarify it let me explain same case in java. Java allows enums to implement interfaces. So I could write:

In base logger library I could define;

public interface IActionType {}

and in one of my several projects

public enum ActionType implements IActionType {UserLogin, PurchaseOrder, .....}

So when I called my logger.log(ActionType.UserLogin, ....) base library would get the underlying action. This would all be suffice. Is there anyway around it to accomplish this in c#? By the way, I considered using IoC containers, but I want something more elegant.

Many thanks for any help...

like image 906
ayk Avatar asked Jan 11 '13 21:01

ayk


People also ask

How do you define enum data type?

Enumeration or Enum in C is a special kind of data type defined by the user. It consists of constant integrals or integers that are given names by a user. The use of enum in C to name the integer values makes the entire program easy to learn, understand, and maintain by the same or even different programmer.

Should enums be public or private?

The enum constructor must be private . You cannot use public or protected constructors for a Java enum .

Can enums be public?

An enum can, just like a class , have attributes and methods. The only difference is that enum constants are public , static and final (unchangeable - cannot be overridden). An enum cannot be used to create objects, and it cannot extend other classes (but it can implement interfaces).

Is enum in standard library Python?

Even though we use the class syntax to create Enums, Enums are not normal Python classes.


2 Answers

Here is approach log4net uses for Level class (yes, it is class, not enum):

public class ActionType : IActionType
{
   public static readonly ActionType UserLogin;
   public static readonly ActionType PurchaseOrder;

   static ActionType()
   {
       UserLogin = new ActionType(1, "User Login");
       // ...
   }

   public ActionType(int value, string name)
   {           
       // verify arguments values
       Value = value;
       Name = name;
   }

   public int Value { get; private set; }
   public string Name { get; private set; }
}

And interface

public interface IActionType
{
    int Value { get; }
    string Name { get; }
}

Usage:

logger.Log(ActionType.UserLogin);
like image 188
Sergey Berezovskiy Avatar answered Oct 04 '22 20:10

Sergey Berezovskiy


Lazy beat me to it but I'll post my solution anyway

public void MyUsage(ITypesafeEnum myEnum)
{
    Console.WriteLine(myEnum.Name);
    Console.WriteLine(myEnum.Val);
}

public interface ITypesafeEnum{
    string Name{get;}
    int Val {get;}
}

public  class TypesafeEnum:ITypesafeEnum{

    public string Name {get;private set;}
    public int Val {get;private set;}
    private TypesafeEnum(){}
    private TypesafeEnum(string name, int val){
        Name = name;
        Val = val;
    }

    public static readonly TypesafeEnum Bedroom = new TypesafeEnum("Bedroom", 1);
    public static readonly TypesafeEnum LivingRoom = new TypesafeEnum("Living Room",2);
}
like image 37
atevans Avatar answered Oct 04 '22 20:10

atevans