Object Initializers

  • Thread starter Thread starter Fred Chateau
  • Start date Start date
F

Fred Chateau

Would someone please explain the execution paths in the following code? In
particular, what is the difference between instantiating the
ContactManagerModel class in the Object Initializer, as opposed to
instantiating it in the constructor body? Also, is this an example of
dependency injection?

public class ContactController : Controller
{
IContactManagerModel _model;

public ContactController() : this(new ContactManagerModel())
{

}

public ContactController(IContactManagerModel model)
{
_model = model;
}
}
 
Fred said:
Would someone please explain the execution paths in the following code? In
particular, what is the difference between instantiating the
ContactManagerModel class in the Object Initializer, as opposed to
instantiating it in the constructor body?

Depends on what you mean by "object initializer". The actual definition
in the context of C# is an initializer of the form "new ClassName() {
Property1 = value1, Property2 = value2 }", that sort of thing. You
haven't shown any such initializer.

But, in terms of initializing objects, there are three places that can
occur:

-- a field initializer
-- the constructor
-- an object initializer

Initialization takes place in the above order, sort of. Specifically,
when a class has fields that have initializers, the real constructor
winds up looking something like this:

public ClassName()
{
field1 = new Field1Type();
field2 = new Field2Type();
// etc.
base();
this();
}

with construction starting with the above constructor. (This is an
oversimplification, of course…after all, you may have parameters for the
constructors, or constructors calling other constructors in the same
class, etc. But hopefully the basic idea is clear).

Also, the object initializer syntax is effectively outside the
constructor. By the time the properties start being initialized, the
"official" construction of the object is done.

This means that the effective order is:

-- all of the field initializers, starting with the most derived
class and working back up to the least derived class
-- all of the constructors, starting with the least derived class
and working back down to the most derived class
-- finally, all of the properties initialized by the object
initializer part of the syntax
Also, is this an example of
dependency injection?

public class ContactController : Controller
{
IContactManagerModel _model;

public ContactController() : this(new ContactManagerModel())
{

}

public ContactController(IContactManagerModel model)
{
_model = model;
}
}

I guess that depends on what IContactManagerModel does and how
ContactController uses an instance of that. It _could_ be. But without
a complete example, it also might not be. You should be able to answer
the question yourself after you read this:
http://en.wikipedia.org/wiki/Dependency_injection

Pete
 
Thank you for the detailed answer. It won't go to waste, however, I screwed
up the question I meant to ask. :)

The question should have been, what is the difference between instantiating
the ContactManagerModel class in the Constructor Initializer, as opposed to
instantiating it in the constructor body? And, what is the execution path?
In other words, is one constructor called? Are both called? At what point
does the "new ContactManagerModel()" execute?
 
Fred said:
Thank you for the detailed answer. It won't go to waste, however, I screwed
up the question I meant to ask. :)

Okay. Well, I think my previous reply still at least in part answers
the question you did want to ask.
The question should have been, what is the difference between instantiating
the ContactManagerModel class in the Constructor Initializer, as opposed to
instantiating it in the constructor body?

This part is difficult to answer though, without clarification as to
what "difference" you want to know about. There's an obvious difference
in syntax. On the other hand, after the object's constructed, there's
no real practical difference.

However, based on these follow-up questions:
And, what is the execution path?
In other words, is one constructor called? Are both called? At what point
does the "new ContactManagerModel()" execute?

Using your original code example:

The parameterless constructor, which includes the constructor
initializer "this(new ContactManagerModel())" is the first constructor
for the ContactController class called as part of the constructor
execution (obviously base constructors are called prior to it, per my
previous reply).

However, before any code in the parameterless constructor body is
executed (you have none in your example, but there could be some), any
constructors specified in the constructor initializers are called. In
your example, that means the ContactController(IContactManagerModel)
constructor.

Of course, before that constructor is called, any arguments to be passed
must be available. So the instantiation of the ContactManagedModel
object passed as an argument obviously must happen before the
constructor is called.

So, this is the order of execution, with nesting to illustrate entry and
exit. When you execute "new ContactController()", you get:

ContactController() constructor is called
{
"new ContactManagedModel()" is executed
ContactController(IContactManagedModel) constructor is called with
the object instantiated in the previous line passed as the argument
{
the value of the parameter "model" is assigned to the field "_model"
}

// if there were any code in the body of ContactController,
// it would be executed here
}

Does that clear things up?

Pete
 
Back
Top