PC Review


Reply
Thread Tools Rate Thread

Constructors

 
 
Peter Morris [Droopy eyes software]
Guest
Posts: n/a
 
      20th Jul 2006
I have an object persistence framework I have written, this framework
expects every object to descend ultimately from PersistentObject and to have
a constructor (ObjectSpace objectSpace) so that objects may be recreated.

PersistentObject's constructor will do something like this

this.ObjectSpace = objectSpace;
objectSpace.RegisterObjectCreation(this);

The object space will record a reference to this new object + do something
like this

if (!IsLoadingFromDatabase)
newInstance.AfterConstruction();


The virtual AfterConstruction method on a class is therefore only ever
called when an object is initially created and not when it is recreated due
to being fetched from the database. This gives a good opportunity to create
composite objects etc. Here is my problem

public class CustomerAction : PersistentObject
{
public CustomerAction(Customer customer)
: base(customer.ObjectSpace)
{
this.Customer = customer;
}

protected override void AfterConstruction()
{
//Do some stuff with this.Customer
}
}

My problem is that this.Customer has not been set by the time
AfterConstruction is called.

1) new CustomerAction(someCustomer);
2) CustomerAction constructor is called
3) Base constructor (customer.ObjectSpace is called)
4) CustomerAction.AfterConstruction is called
5) CustomerAction constructor code is executed

Why oh why wont dotnet let me execute some of my own code before calling the
base constructor? I can do this in methods.

I find it so restrictive!



 
Reply With Quote
 
 
 
 
Joanna Carter [TeamB]
Guest
Posts: n/a
 
      20th Jul 2006
"Peter Morris [Droopy eyes software]" <(E-Mail Removed)> a écrit
dans le message de news: %23sO51Y$(E-Mail Removed)...

| The virtual AfterConstruction method on a class is therefore only ever
| called when an object is initially created and not when it is recreated
due
| to being fetched from the database. This gives a good opportunity to
create
| composite objects etc. Here is my problem
|
| public class CustomerAction : PersistentObject
| {
| public CustomerAction(Customer customer)
| : base(customer.ObjectSpace)
| {
| this.Customer = customer;
| }
|
| protected override void AfterConstruction()
| {
| //Do some stuff with this.Customer
| }
| }

Then, if you are setting the Customer property from the constructor, why not
simply use the Customer property setter to do anything after the property is
set ?

| Why oh why wont dotnet let me execute some of my own code before calling
the
| base constructor? I can do this in methods.
|
| I find it so restrictive!

There is always a way around this, it just requires a change of perspective
:-)

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer


 
Reply With Quote
 
=?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
Guest
Posts: n/a
 
      20th Jul 2006
Peter,
I don't mean to sound disrespectful, but I've observed that often when
people make complaints like yours it is because they have engineering flaws
in their code pattern, not because the Framework is "restrictive". Perhaps
you ought to consider refactoring your design pattern?
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com




"Peter Morris [Droopy eyes software]" wrote:

> I have an object persistence framework I have written, this framework
> expects every object to descend ultimately from PersistentObject and to have
> a constructor (ObjectSpace objectSpace) so that objects may be recreated.
>
> PersistentObject's constructor will do something like this
>
> this.ObjectSpace = objectSpace;
> objectSpace.RegisterObjectCreation(this);
>
> The object space will record a reference to this new object + do something
> like this
>
> if (!IsLoadingFromDatabase)
> newInstance.AfterConstruction();
>
>
> The virtual AfterConstruction method on a class is therefore only ever
> called when an object is initially created and not when it is recreated due
> to being fetched from the database. This gives a good opportunity to create
> composite objects etc. Here is my problem
>
> public class CustomerAction : PersistentObject
> {
> public CustomerAction(Customer customer)
> : base(customer.ObjectSpace)
> {
> this.Customer = customer;
> }
>
> protected override void AfterConstruction()
> {
> //Do some stuff with this.Customer
> }
> }
>
> My problem is that this.Customer has not been set by the time
> AfterConstruction is called.
>
> 1) new CustomerAction(someCustomer);
> 2) CustomerAction constructor is called
> 3) Base constructor (customer.ObjectSpace is called)
> 4) CustomerAction.AfterConstruction is called
> 5) CustomerAction constructor code is executed
>
> Why oh why wont dotnet let me execute some of my own code before calling the
> base constructor? I can do this in methods.
>
> I find it so restrictive!
>
>
>
>

 
Reply With Quote
 
Laura T
Guest
Posts: n/a
 
      20th Jul 2006
"Why oh why wont dotnet let me execute some of my own code before calling
the base constructor? "

Oh, it's not dotnet problem. It's not a problem at all.
If you are a child (your object) of someone (your parent object), your
parent must be alive before you can be alive.
That's why it's mandatory that the parent object is constructed first before
your object.
Rethink your design.



"Peter Morris [Droopy eyes software]" <(E-Mail Removed)> ha
scritto nel messaggio news:%23sO51Y$(E-Mail Removed)...
>I have an object persistence framework I have written, this framework
>expects every object to descend ultimately from PersistentObject and to
>have a constructor (ObjectSpace objectSpace) so that objects may be
>recreated.
>
> PersistentObject's constructor will do something like this
>
> this.ObjectSpace = objectSpace;
> objectSpace.RegisterObjectCreation(this);
>
> The object space will record a reference to this new object + do something
> like this
>
> if (!IsLoadingFromDatabase)
> newInstance.AfterConstruction();
>
>
> The virtual AfterConstruction method on a class is therefore only ever
> called when an object is initially created and not when it is recreated
> due to being fetched from the database. This gives a good opportunity to
> create composite objects etc. Here is my problem
>
> public class CustomerAction : PersistentObject
> {
> public CustomerAction(Customer customer)
> : base(customer.ObjectSpace)
> {
> this.Customer = customer;
> }
>
> protected override void AfterConstruction()
> {
> //Do some stuff with this.Customer
> }
> }
>
> My problem is that this.Customer has not been set by the time
> AfterConstruction is called.
>
> 1) new CustomerAction(someCustomer);
> 2) CustomerAction constructor is called
> 3) Base constructor (customer.ObjectSpace is called)
> 4) CustomerAction.AfterConstruction is called
> 5) CustomerAction constructor code is executed
>
> Why oh why wont dotnet let me execute some of my own code before calling
> the base constructor? I can do this in methods.
>
> I find it so restrictive!
>
>
>



 
Reply With Quote
 
Thomas T. Veldhouse
Guest
Posts: n/a
 
      20th Jul 2006
Laura T <laura.t@_yahoo.com> wrote:
> "Why oh why wont dotnet let me execute some of my own code before calling
> the base constructor? "
>


That works in C++ correctly [in my opinion], but it doesn't work in .NET. It
is very annoying to work around. I wish it were possible to put:

// constructor
public SubClass(int variable) {
// do something here
base(variable);
}

> Oh, it's not dotnet problem. It's not a problem at all.
> If you are a child (your object) of someone (your parent object), your
> parent must be alive before you can be alive.
> That's why it's mandatory that the parent object is constructed first before
> your object.
> Rethink your design.
>


I think it is inconvenient at best. You can easily do such things in C++
without issue.

--
Thomas T. Veldhouse
Key Fingerprint: 2DB9 813F F510 82C2 E1AE 34D0 D69D 1EDC D5EC AED1

 
Reply With Quote
 
Joanna Carter [TeamB]
Guest
Posts: n/a
 
      20th Jul 2006
"Peter Bromberg [C# MVP]" <(E-Mail Removed)> a écrit dans le
message de news: 702D0BA7-AFE6-4F45-A9C5-(E-Mail Removed)...

| I don't mean to sound disrespectful, but I've observed that often when
| people make complaints like yours it is because they have engineering
flaws
| in their code pattern, not because the Framework is "restrictive".
Perhaps
| you ought to consider refactoring your design pattern?

I'm sorry Peter, but I know Pete Morris' work and it is not flaws in his
engineering pracices.

Peter, like I, comes from a Delphi background; a language which allows code
before the call to the inherited constructor.

When you have used a language like Delphi or C++ that allows this practice,
then you tend to design towards the capabilities of those languages. Moving
to a different language that has different features does take some getting
used to and I guess that P.M. will have to refactor, just as I have.

However, moving existing design patterns to a different language is not a
trivial task, but it is definitely *not* because of design failures.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer


 
Reply With Quote
 
Joanna Carter [TeamB]
Guest
Posts: n/a
 
      20th Jul 2006
"Laura T" <laura.t@_yahoo.com> a écrit dans le message de news:
Okb$(E-Mail Removed)...

| Oh, it's not dotnet problem. It's not a problem at all.
| If you are a child (your object) of someone (your parent object), your
| parent must be alive before you can be alive.
| That's why it's mandatory that the parent object is constructed first
before
| your object.
| Rethink your design.

This is almost arrogant and doesn't consider the fact that Peter is a
skilled programmer and is making the transition from Delphi, C#'s parent;
which does allow code before inherited constructor calls.

That's why you have to consider that if it weren't for Delphi, you wouldn't
be using C# :-)

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer


 
Reply With Quote
 
Thomas T. Veldhouse
Guest
Posts: n/a
 
      20th Jul 2006
Joanna Carter [TeamB] <(E-Mail Removed)> wrote:
>
> This is almost arrogant and doesn't consider the fact that Peter is a
> skilled programmer and is making the transition from Delphi, C#'s parent;
> which does allow code before inherited constructor calls.
>


If anything, C++ is C#'s parent. In reality, I think Java is <duck!!!>.

> That's why you have to consider that if it weren't for Delphi, you wouldn't
> be using C# :-)
>


Yahoo ... Pascal was late in the OO game ... and Borland did it with Turbo
Pascal (I used to love programming in TurboVision when the whole world was DOS
and DPMI).

--
Thomas T. Veldhouse
Key Fingerprint: 2DB9 813F F510 82C2 E1AE 34D0 D69D 1EDC D5EC AED1

 
Reply With Quote
 
Joanna Carter [TeamB]
Guest
Posts: n/a
 
      20th Jul 2006
"Thomas T. Veldhouse" <(E-Mail Removed)> a écrit dans le message de news:
(E-Mail Removed)...

| If anything, C++ is C#'s parent. In reality, I think Java is <duck!!!>.

<g>

My allusion is to the fact that Anders Heijlsberg (sp?) was architect on
Delphi before doing the same for C# :-)

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer


 
Reply With Quote
 
Thomas T. Veldhouse
Guest
Posts: n/a
 
      20th Jul 2006
Joanna Carter [TeamB] <(E-Mail Removed)> wrote:
> "Thomas T. Veldhouse" <(E-Mail Removed)> a ?crit dans le message de news:
> (E-Mail Removed)...
>
> | If anything, C++ is C#'s parent. In reality, I think Java is <duck!!!>.
>
> <g>
>
> My allusion is to the fact that Anders Heijlsberg (sp?) was architect on
> Delphi before doing the same for C# :-)
>


Gotcha ... I didn't know that. Good old Microsoft ate up its little cousin
;-)

--
Thomas T. Veldhouse
Key Fingerprint: 2DB9 813F F510 82C2 E1AE 34D0 D69D 1EDC D5EC AED1

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Constructors? Arpan Microsoft ASP .NET 1 5th Sep 2006 07:33 AM
constructors Stephen Martinelli Microsoft VB .NET 6 25th Oct 2003 03:39 PM
Re: Constructors Scott Microsoft C# .NET 2 6th Aug 2003 06:51 AM
Re: Constructors Markus Egger Microsoft C# .NET 1 4th Aug 2003 11:10 PM
Constructors Ravikanth[MVP] Microsoft C# .NET 0 4th Aug 2003 10:48 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:50 PM.