C # unified error handling

Source: Internet
Author: User

Purpose

Make a unified error handling class

Requirements

C/s

Design

Since System. Windows. Forms. MessageDrive and System. Web. MessageDrive are required

I used the existing namespace in the system, which saves the trouble of referencing it separately.

Since they have the same functions, there must be an interface to constrain two classes of System. IMessageDrive.

I also thought that although the implementation functions are different, the two classes have some public functions, so I think they should all be derived from another parent class. System. MessageDrive

Specific design:

Interface

Namespace System

{

Interface IMessageDrive

{

Void Exception (Exception e );

Void Message (Exception e );

Void Warning (Exception e );

}

Enum MessageType

{

Message,

Warning,

Exception,

Notify,

News,

Push

}

}

Parent class

Namespace System

{

Public class MessageDrive

{

Public virtual void Exception (Exception e)

{

}

Public virtual void Message (Exception e)

{

}

Public virtual void Warning (Exception e)

{

}

}

}

Cs subclass

Namespace System. Windows. Forms

{

Public class MessageDrive: System. MessageDrive, IMessageDrive

{

Public override void Exception (Exception e)

{

Base. Exception (e );

MessageBox. Show (e. Message );

MessageDialog dialog = new MessageDialog (MessageType. Exception );

 

}

Public override void Message (Exception e)

{

MessageBox. Show (e. Message );

MessageDialog dialog = new MessageDialog (MessageType. Message );

}

Public override void Warning (Exception e)

{

MessageBox. Show (e. Message );

MessageDialog dialog = new MessageDialog (MessageType. Warning );

}

}

}

Bs subclass

Namespace System. Web

{

Public class MessageDrive: System. MessageDrive, IMessageDrive

{

Public override void Exception (Exception e)

{

}

Public override void Message (Exception e)

{

}

Public override void Warning (Exception e)

{

}

}

}

Questions:

There is no problem in writing this way, but there is a problem. I think these two classes should be static classes. At least the necessary functions are static functions. But in this way, the problem arises. base must be used in non-static functions and classes, so I began to seek a solution. my goal is to call the general method written in the parent class.

Method 1: directly instantiate the parent class and then call?

Does the parent-child relationship have any significance?

Method 2: Do not use static functions?

This may cause discomfort for the user, because the purpose of this class is to replace class functions such as MessageBox. Show (). If you want to instantiate it, it will obviously cause discomfort.

Method 3: The subclass re-implements the function in the parent class?

In this way, neither the object-oriented nor high cohesion nor low coupling are met, and neither of the two classes can be compiled.

I tend to be in method 1, but I still have doubts and cannot find a good method. So I designed it first. I hope someone can provide me with a good suggestion.

 

 

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.