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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:10:44+00:00 2026-05-16T11:10:44+00:00

So, the quote comes from Dependency Injection in .NET . Having that in consideration,

  • 0

So, the quote comes from “Dependency Injection in .NET”. Having that in consideration, is the following class wrongly designed?

class FallingPiece { //depicts the current falling piece in a tetris game
    private readonly IPieceGenerator pieceGenerator;
    private IPiece currentPiece;

    public FallingPiece(IPieceGenerator pieceGenerator) {
        this.pieceGenerator = pieceGenerator;
        this.currentPiece = pieceGenerator.Generate(); //I'm performing work in the constructor with a dependency!
    }

    ...
}

So this FallingPiece class has the responsibility of controlling the current falling piece in a tetris game. When the piece hits the bottom or some other place, raises an event signaling that and then, through the factory, generates another new piece that starts falling again from above.

The only alternative I see is to have an Initialize() method that would generate the piece, but IMO that goes a bit against the idea of having the constructor put your object in a valid state.

  • 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-05-16T11:10:44+00:00Added an answer on May 16, 2026 at 11:10 am

    In general, rules of thumbs are exactly that: rules of thumb. Not immutable laws that one should never deviate from — I’m pretty sure you’ll find cases where doing things with your dependencies in your constructor makes more sense than otherwise.

    With that in mind, let’s revisit your particular design:

    So this FallingPiece class has the
    responsability of controlling the
    current falling piece in a tetris
    game. When the piece hits the bottom
    or some other place, raises an event
    signaling that and then, through the
    factory, generates another new piece
    that starts falling again from above.

    Seems weird to me that FallingPiece triggers the piece generator after its finished.

    I’d design it something like this:

    class PieceGenerator
    {
        public FallingPiece NextFallingPiece()
        {
            FallingPiece piece = new FallingPiece(NextPiece());
            return piece;
        }
        // ...
    }
    
    class GameEngine
    {
        public PieceGenerator pieceGenerator = new PieceGenerator();
    
        public void Start()
        {
            CreateFallingPiece();
        }
    
        void CreateFallingPiece()
        {
            FallingPiece piece = pieceGenerator.NextFallingPiece();
            piece.OnStop += piece_OnStop;
            piece.StartFalling();
        }
    
        void piece_OnStop(FallingPiece piece)
        {
            piece.OnStop -= piece_OnStop;
            if (!GameOver())
                CreateFallingPiece();
        }
    }
    

    At least with this design, the GameEngine is fully responsible for telling the Generator when to create its pieces, which seems more idiomatic than the FallingPiece having that duty.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've recently discovered a possible candidate for dependency injection in a .Net application I've
Background: I've wrote a small library that is able to create asp.net controls from
To quote Wikipedia : Two commonly used languages that support many kinds of implicit
Just came across this quote in a book on OOP that I'm reading, A
I want to extend the asp.net validators such that I can make one validator
I have a C++ application that loads lots of data from a database, then
To quote wikipedia: Scrum is facilitated by a ScrumMaster, whose primary job is to
I saw this quote on the question: What is a good functional language on
After understanding (quote), I'm curious as to how one might cause the statement to
I saw this quote in this question : MS support is poor, except when

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.