Skip to main content

All Questions

Tagged with
3 votes
4 answers
348 views

Primary constructors for depedency injection

Microsoft identifies dependency injection as one of the uses for primary constructors. public class ExampleController(IService service) : ControllerBase { [HttpGet] public ActionResult<...
Jannick Breunis's user avatar
-1 votes
1 answer
2k views

How are parameters values passed into a MVC controller constructor?

For this MS sample code, I have checked Program.cs, Startup.cs, and other config files, don't see how the two parameters sqlQueryService and sqlCommandService are populated. Appreciate if someone can ...
Jeb50's user avatar
  • 101
-2 votes
1 answer
300 views

Why aren't constructors atomic? [closed]

If thrown exceptions in constructors can lead to memory leaks or partially-constructed objects, then why we don't make them atomic? so neither an object nor it's local variables will get created/...
Rain's user avatar
  • 99
-1 votes
1 answer
113 views

Doing work when passing constructor arguments

This is very closely linked to the question Legitimate "real work" in a constructor? but not quite the same. I am interested in having feedback on whether this is acceptable or has any risks....
shelbypereira's user avatar
22 votes
4 answers
5k views

Constructing an object: should I expose or hide parameters passed to the constructor?

I've a habit I just mechanically do without even thinking too much about it. Whenever a constructor is waiting for some parameters, I consider this a public information that should be available by ...
Jérôme MEVEL's user avatar
-2 votes
2 answers
529 views

C# Static & Constructor Logic [closed]

I learn C# and try to understand the logic between static and Constructor right now. One thing I need ask you about an example which I will give at below. (please ignore the quality of code or how ...
Melih Cinar's user avatar
1 vote
2 answers
333 views

Performing serial communication in constructor to initialize an object

I have a class UnitInfo which represents a collection of unit information with methods to get the unit information in a structured way, such as a specific encoding, etc. This unit info consists of ...
pavuxun's user avatar
  • 281
2 votes
3 answers
5k views

Should a class constructor ever create objects of other classes? [duplicate]

Is it ever good or bad to have a constructor create new instances of classes that it needs versus passing in a reference that you want the new class to have ? So basically it's the difference between:...
WDUK's user avatar
  • 2,092
8 votes
4 answers
706 views

Checking the result of a constructor in C#

I'm working on a code base with a coworker who has a habit of checking the results of a constructor for a null in a fashion similar to this Person p = new Person(); if (p != null) { p.Name = "...
Jason Tyler's user avatar
2 votes
4 answers
872 views

What do OOP languages gain from having constructors that always return an object?

In what seems like a deliberate design decision, C++ does not have a null value for objects and references. This makes using objects and references very elegant since we don't have to perform null ...
Fadeway's user avatar
  • 181
1 vote
1 answer
185 views

Am I waiting for the new C# 8 'record' feature?

I'd like an immutable class, or rather a class whose properties are initialised (whether they have public/private setters or not), in the constructor: public class MyClass { public string ...
JᴀʏMᴇᴇ's user avatar
1 vote
0 answers
514 views

Are constructors with complex initialization logic always bad? [duplicate]

I've recently read this blog post regarding what a constructor should do and I am also reading Eric Evans' book on Domain Driven Design. Both the blog post and the book state that a constructor ...
Ciprian Vilcan's user avatar
3 votes
4 answers
5k views

Public static method calls private constructor

I'm working in a C# codebase that was largely written by a former developer and this pattern is used extensively... public class AuditInserter { public static void Insert( DataContext ...
amarsha4's user avatar
  • 176
33 votes
3 answers
19k views

Optional parameters or overloaded constructors

I am implementing a DelegateCommand, and when I was about to implement the constructor(s), I came up with the following two design choices: 1: Having multiple overloaded constructors public ...
user avatar
12 votes
2 answers
8k views

Unit Test to test the creation of a Domain Object

I have a Unit Test, which looks like this: [Test] public void Should_create_person() { Assert.DoesNotThrow(() => new Person(Guid.NewGuid(), new DateTime(1972, 01, 01)); } I am asserting that ...
w0051977's user avatar
  • 7,139

15 30 50 per page