Merge Info From Two Enums

  • Thread starter Thread starter Shapper
  • Start date Start date
S

Shapper

Hello,

I have the following class:

public abstract class Reply {
public ReplyStatus Status { get; set; }
} // Reply

public enum ReplyStatus {
Exception,
Success,
Failure
}

Each command on my service layer returns a Reply. For example:

public class SendMailToUserReply : Reply { }

The reply can return the following status:

1 - Exception (exception occurred)
2 - Success (everything worked fine)
3 - Failure
3.1 Because user is not approved (UserNotApproved)
3.2 Because user is not verified (UserNotVerified)
...

Each reply has different reasons for Failure.

So my idea would be to use a partial enum to merge all values:

public enum ReplyStatus {
Exception,
Success,
Failure
}

Merge with: UserNotApproved, UserNotVerified in SendMailToUserReply

But this is not possible.

So I am looking to return the information in (1, 2 and 3) using Enums and that is recognizable when getting an object of type Reply.

What solutions do I have?

Thank You,
Miguel
 
Hello,

I have the following class:

public abstract class Reply {
public ReplyStatus Status { get; set; }
} // Reply

public enum ReplyStatus {
Exception,
Success,
Failure
}

Each command on my service layer returns a Reply. For example:

public class SendMailToUserReply : Reply { }

The reply can return the following status:

1 - Exception (exception occurred)
2 - Success (everything worked fine)
3 - Failure
3.1 Because user is not approved (UserNotApproved)
3.2 Because user is not verified (UserNotVerified)
...

Each reply has different reasons for Failure.

So my idea would be to use a partial enum to merge all values:

public enum ReplyStatus {
Exception,
Success,
Failure
}

Merge with: UserNotApproved, UserNotVerified in SendMailToUserReply

But this is not possible.

So I am looking to return the information in (1, 2 and 3) using Enums and that is recognizable when getting an object of type Reply.

What solutions do I have?

Two alternatives:

1) switch from struct (enum) to class

public class ReplyStatus
{
public bool Success { get; set; }
}

public class ExceptionReplyStatus : ReplyStatus
{
public Exception Cause { get; set; }
}

public class FailureReplyStatus
{
public int ErrorCode { get; set; }
public string ErrorText { get; set; }
}

or something similar.

That will give you full control over everything.

2) switch to exceptions

Simply make it void and throw an exception in case of problems.

FoobarException with two sub classes NestedFoobarException and
FailureFoobarException.

Arne
 
Back
Top