question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Best way to override Delegate creation while keeping all other customizations

See original GitHub issue

We often use factory methods (Func<T>) in classes. This works well in conjunction with AutoFixture. The only problem I have is that the generated code always returns the same value.

Let´s take a sample class:

public class Sample 
{
  private readonly Func<IFoo> _createFoo;

  public Sample(Func<IFoo> createFoo)
  {
    _createFoo = createFoo;
  }
}

So if I would have to write the code AutoFixture does automatically it would look like this:

var fixture = CreateFixture();
var foo = fixture.Create<IFoo>();
var sample = new Sample(() => foo);

I am of course simplifying what AutoFixture does, but the result is that I will always get the same instance whenever I call the factory method. Can someone please direct me into customizing this behavior so that the result is like this:

var fixture = CreateFixture();
var sample = new Sample(() => fixture.Create<IFoo>());

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
Kralizekcommented, Apr 19, 2020

Should we tag this issue so that if a new major version was to come, we could take the chance to introduce this breaking change?

0reactions
Ergamoncommented, Apr 20, 2020

Yes I would love to see this changed in the future, but as said, there might be someone out there relying on this behavior.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Make a delegate method in custom class overridable in Swift
This is a subclass, it needs override if you want the function to have a different body. class MySubClass : Myclass { override...
Read more >
Solved: Overwriting Created On & Created By?
Solved: I am importing data to an existing entity in CDS using the Get data from Excel button. My data contains information about...
Read more >
Implementing a Custom Delegate | TensorFlow Lite
The preferred method to add a delegate is using SimpleDelegate API. To create a new delegate, you need to implement 2 interfaces and...
Read more >
Events & Delegates in Unity
Learn how to connect scripts & objects using delegates, actions & Unity Events in my in-depth beginner's guide.
Read more >
Delegated properties
A property can delegate its getter and setter to another property. Such delegation is available for both top-level and class properties (member ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found