ayk
ayk

Reputation: 1497

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

Upvotes: 7

Views: 1972

Answers (3)

atevans
atevans

Reputation: 373

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);
}

Upvotes: 3

Thomas C. G. de Vilhena
Thomas C. G. de Vilhena

Reputation: 14545

Here is another approach that uses generics:

public void Log<EnumType>(EnumType enumMember)
{
    var name = enumMember.ToString();
    int value = (int)(object)enumMember;
    Console.WriteLine(name + " = " + value);
}

Calling the above method like this:

Log<ActionType>(ActionType.UserLogin);
Log<ActionType>(ActionType.PurchaseOrder);

Results in an output like the following:

UserLogin = 0
PurchaseOrder = 1

Upvotes: 1

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236188

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

Upvotes: 3

Related Questions