DataPortal_XYZ override methods

DataPortal_XYZ override methods

Old forum URL: forums.lhotka.net/forums/t/4665.aspx


manulisa posted on Friday, April 11, 2008

Hi,

In the base class, all the DataPortal_XYZ methods are defined as protected virtual, so we can create an override in our # Data Region # for each of them:

protected override void DataPortal_Create()

protected override void DataPortal_Fetch(object criteria)

protected override void DataPortal_Insert()

protected override void DataPortal_Delete(object criteria)

protected override void DataPortal_Update()

But in the book Expert C# Objects, i see sometimes that the "override" is used, and some times the override is replaced by a "private" one, for example:

[RunLocal()]

private void DataPortal_Create(Criteria criteria)

{

      _id = Guid.NewGuid();

      _started.Date = DateTime.Today;

      ValidationRules.CheckRules();

}

 

why not : protected override void DataPortal_Create(Criteria criteria) ?

 

Instead, for some others , like  Insert, Update & Delete, the "protected override" is used.

So to summarize:

Fetch, Create -> "private" used instead of "override"

Update, Insert, Delete -> "override" is used.

 

So, my question is,  why the "Fetch" and "Create" methods are redefined as "private", even when there exists a "protected virtual" which can be overriden, and for the other ones like Insert,Update,Delete, the "override" is used ...

I'm a bit confused here ...

Thanx for any response,

Manulisa.

 

 

 

 

 

sergeyb replied on Friday, April 11, 2008

This may be done to support multiple signatures of a method.  Data Portal will attempt to find the one that matches most closely to the DataPortal.XYZ call.

 

Sergey Barskiy

Senior Consultant

office: 678.405.0687 | mobile: 404.388.1899

cid:_2_0648EA840648E85C001BBCB886257279
Microsoft Worldwide Partner of the Year | Custom Development Solutions, Technical Innovation

 

From: manulisa [mailto:cslanet@lhotka.net]
Sent: Friday, April 11, 2008 8:08 AM
To: Sergey Barskiy
Subject: [CSLA .NET] DataPortal_XYZ override methods

 

Hi,

In the base class, all the DataPortal_XYZ methods are defined as protected virtual, so we can create an override in our # Data Region # for each of them:

protected override void DataPortal_Create()

protected override void DataPortal_Fetch(object criteria)

protected override void DataPortal_Insert()

protected override void DataPortal_Delete(object criteria)

protected override void DataPortal_Update()

But in the book Expert C# Objects, i see sometimes that the "override" is used, and some times the override is replaced by a "private" one, for example:

[RunLocal()]

private void DataPortal_Create(Criteria criteria)

{

      _id = Guid.NewGuid();

      _started.Date = DateTime.Today;

      ValidationRules.CheckRules();

}

 

why not : protected override void DataPortal_Create(Criteria criteria) ?

 

Instead, for some others , like  Insert, Update & Delete, the "protected override" is used.

So to summarize:

Fetch, Create -> "private" used instead of "override"

Update, Insert, Delete -> "override" is used.

 

So, my question is,  why the "Fetch" and "Create" methods are redefined as "private", even when there exists a "protected virtual" which can be overriden, and for the other ones like Insert,Update,Delete, the "override" is used ...

I'm a bit confused here ...

Thanx for any response,

Manulisa.

 

 

 

 

 



JoeFallon1 replied on Friday, April 11, 2008

Sergey is correct.

Notice the 2 signatures in your example are different!

protected override void DataPortal_Create()

private void DataPortal_Create(Criteria criteria)

The 1st signature is in CSLA and the 2nd in your BO. The 2nd does not in fact override the first since the signature is different. So when you call DataPortal.Create if you pass no parameters then the 1st signature is called, if you pass 1 parameter then the 2nd is called.

Joe

 

tmg4340 replied on Friday, April 11, 2008

It's not quite as simple as "Fetch and Create use private, others use override"... Smile [:)]

Versions of CSLA prior to .NET 2.0 only had the virtual methods.  A BO would override the appropriate one in their code based on whether criteria was needed or not.  But you'll notice that the criteria parameter is an "object" type.  This is because there wasn't a base criteria class at the time - Rocky's implementation relied on the criteria class being nested within the BO, so you could create almost anything for your criteria object.  I know you can still do this in the 3.0.x versions, and I think 3.5 still supports it as well.  A base criteria class only came about after some discussion with folks using code generation.

When .NET 2.0 came along, Rocky modified the DataPortal code to look for DP_ routines by both name and parameter type(s).  Doing so allows us to write DP_ routines that define strongly-typed criteria parameters, and the DataPortal will find them.  The virtual methods are kept for backwards compatibility, as well as fallback methods - if a matching routine cannot be found using the parameter types, the DataPortal falls back to these "generic" methods.

As far as "private" vs. "protected override", DP_ routines that use strongly-typed criteria parameters have no corresponding method in the base class (unless you have them in a custom base class of your own), so there is nothing to override.  The choice of private vs. protected scope is a question of class design - if your BO is not going to be subclassed, you don't need to make the routine protected.  You are certainly free to override the existing virtual methods in your BO's, but it's not a recommended practice for new code.

HTH

- Scott

manulisa replied on Saturday, April 12, 2008

Hi,

Ok, thanx to all of you, things are more clear now to me.Smile [:)]

Greetz,

manulisa.

 

 

Copyright (c) Marimer LLC