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

  • Home
  • SEARCH
  • 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 3407032
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:42:04+00:00 2026-05-18T05:42:04+00:00

I currently have an immutable type called Gene , that only has 2 fields:

  • 0

I currently have an immutable type called Gene, that only has 2 fields:

double value;
Interval intervalOfAllowedValues;

I’ll need sometimes to have a Gene switch randomly its value to some other value as long as it is still in the range defined in intervalOfAllowedValues.

I had made for this, a special method

public Gene RandomMutation() { ... }
//it returns a Gene because this class is immutable!

that’d take care of the situation, using a INumberGenerator.GenerateDouble(...). The problem is that either RandomMutation() accepts a IRandomNumberGenerator as argument or then Gene will have to take one by constructor injection.

Neither of the solutions are of my liking:

If RandomMutation() accepts the number generator by argument, it means that now not only does Gene have to know about INumberGenenerator but also the class that contains it.

If on the other hand I pass by constructor injection an INumberGenerator to this Gene, most of the time I’ll have it there without making any use of it, which doesn’t seem nice. I feel it kinda defeats the purpose of having this Gene object exist as an value type.
It also raises the subtle question of whether 2 different Genes are equal if they have different number generators. If yes, how to Unit-Test it?

There is a third option: I take RandomMutation() away from the Gene class. The problem now is that the class that contains a Gene will have to know both about Gene and about Interval, which might not be desirable. Also, behaviour should be near its data, and that wouldn’t be the case when following this approach.

There is yet a 4th(!) approach: making the number generator global (Singleton). That’d work wonders, but it’d go against the philosophy of making every dependency explicit.

How’d you handle this situation?

Thanks

  • 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-18T05:42:05+00:00Added an answer on May 18, 2026 at 5:42 am

    It is not a useful metric to evaluate how much a dependency like INumberGenerator is utilized throughout the lifetime of a particular Gene instance. It is only useful to see how many times it is used by the definition of Gene. If it is used even once, it deserves to be treated like a first-class dependency.

    The bigger issue is in having a struct with dependencies at all. RandomMutation must have an INumberGenerator to function, but no matter whether you use constructor or property injection, anyone can call new Gene() and create an instance with null dependencies. There is simply no way for you to enforce that dependencies have been supplied to a struct.

    You have a couple of options. The first is to define Gene as a class, which will let you declare INumberGenerator as a required dependency through a constructor argument. This is the object-oriented answer.

    Your second option is use a more functional approach, which leaves Gene as a struct and defines the functionality externally. Functional programs tend to separate data structures from the logic which acts upon them (see LINQ), as opposed to OO which strives to combine behavior and data. Your problem sounds like it would best benefit from being modeled in this fashion.

    The core issue still remains, though, which is that you need both a Gene and an INumberGenerator in order to mutate randomly. Since we have determined that Gene probably shouldn’t know about INumberGenerator directly, you can model it as an extension method:

    public static Gene Mutate(this INumberGenerator generator, Gene gene)
    {
        // ...
    }
    

    This takes the INumberGenerator dependency out of Gene and bubbles it up to the object which knows when genes should be mutated.

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

Sidebar

Related Questions

I currently have an MS Access application that connects to a PostgreSQL database via
We currently have code like this: Dim xDoc = XDocument.Load(myXMLFilePath) The only way we
I currently have a DetailsView in ASP.NET that gets data from the database based
I currently have speakers set up both in my office and in my living
I currently have an existing database and I am using the LINQtoSQL generator tool
We currently have a company email server with Exchange, and a bulk email processing
I currently have a fairly robust server-side validation system in place, but I'm looking
I currently have heavily multi-threaded server application, and I'm shopping around for a good
I currently have a class and I'm trying to create an easy GUI to
I currently have a functioning in-house Windows Forms application which extensively uses the DataGridView

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.