Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 142745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T07:59:08+00:00 2026-05-11T07:59:08+00:00

I would like to provide a deafult implementation for an event generated by an

  • 0

I would like to provide a deafult implementation for an event generated by an instance of “Authenticator” (hereafter “objAuthenticator”).

Is this an acceptable practice to provide such method that registers for handling objAuthenticator’s own event to provide a deafult implementation?

And also, if it is an acceptable pratice, I have two follow-up questions. I have two overloaded methods for “SendEmailOnAuthenticationFailed”.

  • Which one should I expose to the outside world?
  • Which one would cause less coupling between classes?

    public class Authenticator {         public event EventHandler AuthenticationFailed = delegate { };         protected virtual void OnAuthenticationFailed()         {             var handler = AuthenticationFailed;             handler(this, EventArgs.Empty);         }          public void IsAuthenticated()         {             // Some logic...             // ...             // Woops authentication failed             OnAuthenticationFailed();         }           // Is this a better option?         public void SendEmailOnAuthenticationFailed()         {             SendEmailOnAuthenticationFailed(EmailSender);         }          // Or is this?         public void SendEmailOnAuthenticationFailed(EventHandler emailSender)         {             AuthenticationFailed += emailSender;         }          private void EmailSender(object sender, EventArgs e)         {             Console.WriteLine('Send email: EmailSender');         }     } 

[UPDATE]: Answer to Marc Gravell’s question

I’m very confused about what your code is trying to do

So far, ‘SendEmailOnAuthenticationFailed’ is not exposed to the outside world. What I am trying to do is that (I just added IsAuthenticated) within ‘IsAuthenticated’, when an authentication fails, I would like to send an email without having to write the same event handler everywhere.

[UPDATE2]: I have realized that there is no need to expose ‘AuthenticationFailed’. I will keep the class as closed as possible without exposing the event.

Why can’t we mark more than one answers as ‘answer’? …

[UPDATE3]: Final output: Here is the how I decided to implement.

public class Authenticator {     private readonly IEmailResponder _EmailResponder;      public Authenticator(IEmailResponder emailResponder)     {         _EmailResponder = emailResponder;     }      public void IsAuthenticated()     {         // Some logic...         // ...          // Woops authentication failed         SendEmailForAuthenticationFailure();     }      private void SendEmailForAuthenticationFailure()     {         _EmailResponder.SendEmail(...);     } } 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T07:59:08+00:00Added an answer on May 11, 2026 at 7:59 am

    The answer to the first question is that it is acceptable. For your follow-ups, the answer is it depends. The first one internalizes the email process. If you choose to do this, make sure that you’re injecting the email logic. If you choose the latter, it allows you to inject the behavior. If you do the second one, however, I would suggest that you change the name, since it doesn’t matter if it’s email, or MQ, or tiddlywinks. In fact, the second method isn’t really needed, since they can subscribe directly to the event.

    If you’re concerned about decoupling, you need to think about the single responsibility of the Authenticator class. It should be to authenticate, not to send emails. Try this:

    public class Authenticator {     public event EventHandler AuthenticationFailed = delegate { };      protected virtual void OnAuthenticationFailed()     {         AuthenticationFailed(this, EventArgs.Empty);     } } 

    now, consume authenticator:

    AuthenticationEmailResponder responder = new AuthenticationEmailResponder(emailAddress, emailServer); objAuthenticator.AuthenticationFailed += responder.SendFailureMessage; 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to provide two different versions of my iPhone app on the
I would like to provide the raw text referring to an environment variable to
I have developed about 300 Applications which I would like to provide with multi-language
I would like to write a utility that will provide me with a relatively
I would like to update a Windows Forms application to provide the following features:
I would like to be able to delay the default action of an event
I would like to only force the implementation of a C# getter on a
I would like to use the ASP.NET Membership Provider security framework but would like
I am using some features that are provided in GCC v4+ and would like
The DateTimeConverter class provides conversions between DateTime and string. I would also like to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.