构造函数 [英] Constructors

查看:78
本文介绍了构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个我编写的对象持久性框架,这个框架

期望每个对象最终从PersistentObject下降并且具有

a构造函数(ObjectSpace objectSpace)以便对象可以重新创建。


PersistentObject的构造函数将执行类似这样的操作


this.ObjectSpace = objectSpace;

objectSpace.RegisterObjectCreation(this);


对象空间将记录对这个新对象的引用+做一些事情

像这样


如果(!IsLoadingFromDatabase)

newInstance.AfterConstruction();

因此,类上的虚拟AfterConstruction方法只是$

在最初创建对象时调用,而不是在重新创建时调用

从数据库中获取。这给了一个很好的机会来创建

复合对象等。这是我的问题


公共类CustomerAction:PersistentObject

{

公共CustomerAction(客户客户)

:base(customer.ObjectSpace)

{

this.Customer = customer ;

}


protected override void AfterConstruction()

{

//做一些事情有了这个。客户

}

}


我的问题是这个。客户尚未设定时间

调用AfterConstruction。


1)新的CustomerAction(someCustomer);

2)CustomerAction构造函数被称为

3)基础构造函数(调用customer.ObjectSpace)

4)调用CustomerAction.AfterConstruction

5)执行CustomerAction构造函数代码

为什么哦为什么不让dotnet让我在调用

base constructo之前执行一些我自己的代码R'我可以用方法做到这一点。


我发现它如此限制!

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!

推荐答案

彼得,

我不是故意听起来不尊重,但是我经常观察到,当人们提出像你这样的投诉时,这是因为他们有工程上的缺陷<他们的代码模式中的
,不是因为Framework是限制性的。也许

你应该考虑重构你的设计模式吗?

彼得


-

Co -founder,Eggheadcafe.com开发者门户网站:
http://www.eggheadcafe.com

UnBlog:
http://petesbloggerama.blogspot。 com


" Peter Morris [Droopy eyes software]"写道:
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:

我有一个我编写的对象持久性框架,这个框架

期望每个对象最终都来自PersistentObject并且拥有

a构造函数(ObjectSpace objectSpace)以便可以重新创建对象。

PersistentObject的构造函数将执行类似的操作


this.ObjectSpace = objectSpace;

objectSpace.RegisterObjectCreation(this);


对象空间将记录对这个新对象的引用+做某事

喜欢这个


if(!IsLoadingFromDatabase)

newInstance.AfterConstruction();


因此,一个类上的虚拟AfterConstruction方法只能在最初创建对象时调用,而不是在重新创建对象时调用

从数据库中获取。这给了一个很好的机会来创建

复合对象等。这是我的问题


公共类CustomerAction:PersistentObject

{

公共CustomerAction(客户客户)

:base(customer.ObjectSpace)

{

this.Customer = customer ;

}


protected override void AfterConstruction()

{

//做一些事情有了这个。客户

}

}


我的问题是这个。客户尚未设定时间

调用AfterConstruction。


1)新的CustomerAction(someCustomer);

2)CustomerAction构造函数被称为

3)基础构造函数(调用customer.ObjectSpace)

4)调用CustomerAction.AfterConstruction

5)执行CustomerAction构造函数代码

为什么哦为什么不用dotnet让我执行一些自己的代码在调用

基础构造函数之前?我可以用方法做到这一点。


我发现它如此限制!

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!


" Peter Morris [Droopy眼睛软件] < pe ** @droopyeyes.no.com.spamaécrit

dans le message de news:%2****************@TK2MSFTNGP02.phx .gbl ...


|因此,类上的虚拟AfterConstruction方法只有
|在最初创建对象时调用,而不是在重新创建对象时调用

到期

|从数据库中取出。这给了一个很好的机会来创造

|复合物等。这是我的问题

|

| public class CustomerAction:PersistentObject

| {

| public CustomerAction(客户客户)

| :base(customer.ObjectSpace)

| {

| this.Customer = customer;

| }

|

| protected override void AfterConstruction()

| {

| //做一些与此有关的东西。客户

| }

|然后,如果你是从构造函数设置Customer属性,那么为什么不呢?
只需使用Customer属性setter在属性<之后执行任何操作br />
设定?


|为什么哦为什么不让dotnet让我执行一些自己的代码然后再打电话给



|基础构造函数?我可以用方法做到这一点。

|

|我发现它如此限制!


总有一种解决方法,它只需要改变视角

:-)


Joanna


-

Joanna Carter [TeamB]

顾问软件工程师
"Peter Morris [Droopy eyes software]" <pe**@droopyeyes.no.com.spama écrit
dans le message de news: %2****************@TK2MSFTNGP02.phx.gbl...

| 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


"为什么哦为什么不让dotnet让我在调用

基础构造函数之前执行一些我自己的代码?


哦,这不是dotnet的问题。这根本不是问题。

如果您是某人(您的父对象)的孩子(您的对象),您的

父母必须在您之前活着可以活着。

这就是为什么必须首先构建父对象

你的对象。

Rethink你的设计。


" Peter Morris [Droopy eyes software]" < pe ** @ droopyeyes.no.com.spamha

scritto nel messaggio新闻:%2 **************** @ TK2MSFTNGP02.phx.gbl ...
"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]" <pe**@droopyeyes.no.com.spamha
scritto nel messaggio news:%2****************@TK2MSFTNGP02.phx.gbl...

>我有一个我编写的对象持久性框架,这个框架希望每个对象最终都从PersistentObject下降到
有一个构造函数(ObjectSpace objectSpace),以便可以重新创建对象。


PersistentObject的构造函数将执行类似这样的操作


this.ObjectSpace = objectSpace;

objectSpace.RegisterObjectCreation(this);


对象空间将记录对这个新对象的引用+做某事

喜欢这个


if(!IsLoadingFromDatabase)

newInstance.AfterConstruction();


因此,类的虚拟AfterConstruction方法只能在最初创建对象时调用,而不是在重新创建对象时调用

由于从中获取数据库。这给了一个很好的机会来创建复合对象等。这是我的问题


公共类CustomerAction:PersistentObject

{

公共CustomerAction(客户客户)

:base(customer.ObjectSpace)

{

this.Customer = customer ;

}


protected override void AfterConstruction()

{

//做一些事情有了这个。客户

}

}


我的问题是这个。客户尚未设定时间

调用AfterConstruction。


1)新的CustomerAction(someCustomer);

2)CustomerAction构造函数被称为

3)基础构造函数(调用customer.ObjectSpace)

4)调用CustomerAction.AfterConstruction

5)执行CustomerAction构造函数代码

为什么哦为什么不让dotnet让我在调用之前执行一些自己的代码

基础构造函数?我可以用方法做到这一点。


我发现它如此限制!
>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!



这篇关于构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆