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 8420445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:53:18+00:00 2026-06-10T02:53:18+00:00

today i was thinking about tell! don’t ask! and experimenting with this code. interfaces:

  • 0

today i was thinking about “tell! don’t ask!” and experimenting with this code.

interfaces:

interface IValidationContext
{
  void AddMessage(string text);   
  bool IsValid { set; }
}

interface IValidation
{
  void ValidateInput(Input input, IValidationContext context); 
  void ValidateOutput(Output output, IValidationContext context); 
}

interface ICalculator
{
  Output Calculate(Input input);
}

implementations:

class CalculationService
{
  private readonly ICalulator _calculator;
  private readonly IValidation _validation;

  public CalculationService(ICalculator calculator, IValidation validation)
  {
    _calculator = calculator;
    _validation = validation;
  }

  public Output Calculate(Input input)
  {
     var context = new CalculationContext();
     _validation.ValidateInput(input, context);

     if (context.IsValid)
     {
        var output = _calculator.Calculate(input);
        _validation.ValidateOutput(output, context);
        return output
     }
     return null;
  }
}

class CalculationContext : IValidationContext
{
  public CalculationContext()
  {
    Messages = new List<string>();
  }

  public IList<string> Messages { get; private set; }

  public void AddMessage(string text)
  {
    Messages.Add(text);
  }

  public bool IsValid { set; get; }
}

i know it is not always possible conform a design-principle.
but in the end i stuck with this code where i’m asking an object:

 if (context.IsValid)
 {
   var output = _calculator.Calculate(input);
   _validation.ValidateOutput(output, context);
 }

is it possible solve it whether it is practical or not?

edit 1:
if i modify my IValidationContext and rename it:

interface ICalculationContext
{
   void AddMessage(string text);   
   Output Calculate(ICalculator calculator, Input input);
   bool IsValid { set; }
}

the context don’t need to be askd:

public Output Calculate(Input input)
{
  _validation.ValidateInput(input, context);        
  var output = context.Calculate(_calculator, input);
  _validation.ValidateOutput(output, context);
  return output;
}

now the context is responsible to call calculation based on its internal state.
…it dont feel right…

edit 2:
i read a small article about “tell! don’t ask!” and it states that:
asking an object for its internal state and then tell that object something depending on that state, would violate “tell! don’t ask!” but it’s ok to tell another object something.
does this apply here?

btw. introducing an boolean-isvalid-result for ValidateInput and ValidateOutput.
could change the code to this, which is nice and nobody gets “asked” something:

public Output Calculate(Input input)
{
  var isValid = _validation.ValidateInput(input, context);

  if (isValid)
  {
    var output = _calculator.Calculate(input);
    _validation.ValidateOutput(output, context);
    return output
  }
  return null;
}
  • 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. Editorial Team
    Editorial Team
    2026-06-10T02:53:19+00:00Added an answer on June 10, 2026 at 2:53 am

    This line is the source of the issue

    var context = new CalculationContext();
    

    You should be injecting the CalculationContext into the CalculationService so you can interrogate it outside of the class. The Calculate method tells the validator to validate the input and output. Asking in this context would be code like this:

    public Output Calculate(Input input)
    {
       var validator = _context.Validator;
       if (validator.IsInputValid(input)) {
           // ... snip ...
       }
    }
    

    Here we’re asking the validator whether a particular input is valid, rather than telling it to validate something. Also, we are limiting ourselves to working with the IsValid getter on the IValidationContext object. This is a little bit of a fuzzy situation, because accessing _context.Validator could be seen as violating the Law of Demeter, but this property is defined in an interface and only returns an interface so we are not coupled to any concrete implementation of these classes.

    Here’s a suggestion, assuming the following modifications to the interfaces

    interface IValidationContext
    {
        void AddMessage(string text); 
        IValidation Validator { get; }
        bool IsValid { get; }
    }
    
    interface IValidation
    {
        void ValidateInput(Input input); 
        void ValidateOutput(Output output); 
    }
    
    interface ICalculator
    {
        Output Calculate(Input input);
    }
    
    
    class CalculationService
    {
        private readonly ICalulator _calculator;
        private readonly IValidationContext _context;
    
        public CalculationService(ICalculator calculator, IValidationContext context)
        {
          _calculator = calculator;
          _context = context;
        }
    
        public Output Calculate(Input input)
        {
           _context.Validator.ValidateInput(input);
    
           if (_context.IsValid)
           {
              var output = _calculator.Calculate(input);
              _context.Validator.ValidateOutput(output);
    
              return output;
           }
           return null;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was thinking about this today and I realized I don't have a clear
Today I was thinking about declaring this: private delegate double ChangeListAction(string param1, int number);
I was thinking about this problem today, and I came with the following pseudocode
Came by a curious case today, that got me thinking about how the object
I've been thinking about this far too long and haven't gotten any idea, maybe
I was thinking today about the try/catch blocks existent in another languages. Googled for
With twitter being down today I was thinking about how to best handle calls
I was thinking today about what could be the most complex / impressive application
Today I was thinking about a Python project I wrote about a year back
I was thinking about CSS positioning modes today and realized that I never use

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.