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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T01:24:01+00:00 2026-06-09T01:24:01+00:00

I would like to inject the constructor parameter IActionLogger actionLogger, but want the other

  • 0

I would like to inject the constructor parameter IActionLogger actionLogger, but want the other parameters largeBucket, smallBucket and amountToRetrieve are context-sensitive (not sure if that’s the right term).

Question:

Should I just make these constructor parameters an automatic property and just leave the IActionLogger actionLogger parameter in the constructor?

Basically, the calculations will vary based on the largeBucket, smallBucket and amountToRetrieve variables?
I put these variables in the constructor because I need to do some setup beforehand.

public class BucketActionsHandler : IBucketActionsHandler
{
    private List<IAction> _actions = new List<IAction>();
    private Bucket _largeBucket;
    private Bucket _smallBucket;
    private IActionLogger _actionLogger;
    private int _amountToRetrieve;


    public BucketActionsHandler(Bucket largeBucket, Bucket smallBucket, int amountToRetrieve, IActionLogger actionLogger)
    {
        _largeBucket = largeBucket;
        _smallBucket = smallBucket;
        _amountToRetrieve = amountToRetrieve;
        _actionLogger = actionLogger;

        _actions.Add(new LastAction(largeBucket, smallBucket, amountToRetrieve));
        _actions.Add(new EmptySmallerBucketAction(largeBucket, smallBucket, amountToRetrieve));
        _actions.Add(new EmptyLargeBucketAction(largeBucket, smallBucket, amountToRetrieve));
        _actions.Add(new FillLargeBucketAction(largeBucket, smallBucket, amountToRetrieve));
        _actions.Add(new FillSmallBucketAction(largeBucket, smallBucket, amountToRetrieve));
        _actions.Add(new TransferToLargeBucketAction(largeBucket, smallBucket, amountToRetrieve));
        _actions.Add(new TransferToSmallBucketAction(largeBucket, smallBucket, amountToRetrieve));
    }

    private IAction GetNextAction()
    {
        foreach (var action in _actions)
        {
            if (action.SatisfiedCondition())
            {
                return action;
            }
        }
        return null;
    }

    public void CalculateSteps()
    {
        IAction nextAction;
        do
        {
            nextAction = GetNextAction();
            nextAction.Execute();
            if (nextAction == null)
            {
                throw new InvalidOperationException("No valid action available");
            }
        } while(!(nextAction is LastAction));
    }
}
  • 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-09T01:24:03+00:00Added an answer on June 9, 2026 at 1:24 am

    Should I just make these constructor parameters an automatic property

    No, because this would allow you to change this service after it is injected or created, which is a bad thing, because services should be stateless, or at least their internal state changes should have no effect on the correctness of the application. When you change the state of the service, the application code forces this service to be a transient (new instance should be injected each time it is requested), while the application shouldn’t care. This moves the control and decision of what lifetime services have out of the Composition Root (the start-up path of the application), which hinders maintainability.

    Instead, use a factory:

    public interface IBucketActionsHandlerFactory
    {
        IBucketActionsHandler Create(
            Bucket largeBucket,
            Bucket smallBucket,
            int amountToRetrieve);
    }
    

    You can inject this factory into the service that needs it, and let that service supply the proper context variables:

    public class SomeService
    {
        private IBucketActionsHandlerFactory factory;
        private IBucketRepository repository;
    
        public SomeService(IBucketActionsHandlerFactory factory,
            IBucketRepository repository)
        {
            this.factory = factory;
            this.repository = repository;
        }
    
        public void Handle(int amountToRetrieve)
        {
            var largeBucket = this.repository.GetById(LargeBucketId);
            var smallBucket = this.repository.GetById(SmallBucketId);
    
            var handler = this.factory.Create(largeBucket, smallBucket,
                amountToRetrieve);
    
            handler.CalculateSteps();
        }
    }
    

    The factory will be in control of creating new IBucketActionsHandler implementations:

    public class BucketActionsHandlerFactory
        : IBucketActionsHandlerFactory
    {
        private Container container;
    
        public class BucketActionsHandlerFactory(
            Container container)
        {
            this.container = container;
        }
    
        public IBucketActionsHandler Create(
            Bucket largeBucket, Bucket smallBucket,
            int amountToRetrieve)
        {
            return new BucketActionsHandler(
                largeBucket, smallBucket, amountToRetrieve,
                this.container.Get<IActionLogger>());
        }
    }
    

    Your BucketActionsHandlerFactory should be part of the Composition Root, and in this case it is fine to inject a container / kernel into this factory (it is part of the DI infrastructure).

    This way the application is ignorant about what type of handler it gets, but is still able to get a BucketActionsHandler within its current context.

    Alternatively, you can supply the largeBucket, smallBucket, and amountToRetrieve variables to the CalculateSteps method. This allows you to remove the need to a factory:

    public class BucketActionsContext
    {
        public Bucket LargeBucket { get; set; }
        public Bucket SmallBucket { get; set; }
        public int AmountToRetrieve { get; set; }
    }
    
    public class BucketActionsHandler : IBucketActionsHandler
    {
        private IActionLogger _actionLogger;
    
        public BucketActionsHandler(IActionLogger actionLogger)
        {
            _actionLogger = actionLogger;
        }
    
        public void CalculateSteps(
            BucketActionsContext context)
        {
            IAction nextAction;
            do
            {
                nextAction = this.GetNextAction(context);
    
                if (nextAction == null)
                {
                    throw new InvalidOperationException(
                        "No valid action available");
                }
    
                nextAction.Execute();
            } 
            while(!(nextAction is LastAction));
        }
    
        private IAction GetNextAction(
            BucketActionsContext context)
        {
            return (
                from action in this.GetActions(context)
                where action.SatisfiedCondition()
                select action)
                .FirstOrDefault();
        }
    
        private IEnumerable<IAction> GetActions(
            BucketActionsContext context)
        {
            Bucket largeBucket = context.LargeBucket;
            Bucket smallBucket = context.SmallBucket;
            int amountToRetrieve = context.AmountToRetrieve;
    
            yield return new LastAction(largeBucket, smallBucket, amountToRetrieve);
            yield return new EmptySmallerBucketAction(largeBucket, smallBucket, amountToRetrieve);
            yield return new EmptyLargeBucketAction(largeBucket, smallBucket, amountToRetrieve);
            yield return new FillLargeBucketAction(largeBucket, smallBucket, amountToRetrieve);
            yield return new FillSmallBucketAction(largeBucket, smallBucket, amountToRetrieve);
            yield return new TransferToLargeBucketAction(largeBucket, smallBucket, amountToRetrieve);
            yield return new TransferToSmallBucketAction(largeBucket, smallBucket, amountToRetrieve);    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to inject ILog into my classes, not an ILoggerFactoryAdapter, but the
I would like to inject a dependency into an ASP.NET MVC model, but I
I would like my chrome extension to be able to inject a 300px sidebar
Would like to make anapplication in Java that will not automatically parse parameters used
I have a custom HTTP Module. I would like to inject the logger using
I am using the basic Unity MVC Nuget Bootstrapper. I would like to Inject
I have an executable module created by third party. I would like to inject
I would like to have Windsor inject multiple implementations of a service to a
I would like to bring dependency injection to my persistent entities, but I am
Would like to parse IPv4 address from exit-addresses . Format of the file: ExitNode

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.