Something I did not understand with DataPortal_Fetch

Something I did not understand with DataPortal_Fetch

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


RichardETVS posted on Thursday, April 12, 2007

Hi

I have a class:

public abstract class ER_US : BusinessBase<ER_US>, IUS

and another:

public class USComposite : US, IUSComposite

When I try a DataPortal_Fetch with the USComposite class, all is working, except for its field        private List<US> _usCollection.

I get an exception because _ usCollection is null. But in the the DataPortal_Create method of the class, I create the object:

    protected override void DataPortal_Create()
    {
        base.DataPortal_Create();
        _typeUS = 4;
        _usCollection = new List<ER_US>();
        MarkNew();
    }


In fact it works well when I create the object, I can add US objects in the _usCollection field, and the DataPortal_Insert seems to work fine. It is juste when I try a fetch that I get this error. But I was thinking that when we fetch an object, its DataPortal_Create method was called. It is not the case.

So, what I misunderstand?

Thanks for any help!


Richard

skagen00 replied on Thursday, April 12, 2007

Maybe you're under the assumption that DataPortal_Create fires when DataPortal_Fetch does. It does not. You'll need to instantiate your collection (one place would be on the variable declaration rather than the DataPortal_Create, and you'll get the blank collection instantiated either way).

RichardETVS replied on Friday, April 13, 2007

Well, I misunderstood. When I read, page 366 of the C# book, that « the result is that the data portal creates a new instance of the business object », I thought it used Data_Poral. Ok, it does not.

 

But where is the best place to instantiate some objects that would be in the create procedure, normally?

 

I can have objects with private fields who need instantiations, and it seems very strange to do that outside the object, after the variable declaration.

 

Looking in the example application, I decided to do the same thing that in the ResourceAssignment class, I instantiate private fields in the private constructor. So far, it seems to work. Is that a good idea?

 

skagen00 replied on Friday, April 13, 2007

Hi Richard,

DataPortal_Create is used traditionally by the factory method such as NewIndividual() to allow you to assign initial values or perhaps things passed through the "New" factory method.

DataPortal_Fetch is used traditionally by the factory method such as GetIndividual() and is used traditionally to call a stored procedure or the like and to receive a set of results - namely for the main individual information and then also its child objects.

But GetIndividual() will not end up flowing through DataPortal_Create and NewIndividual() wil not end up flowing through DataPortal_Fetch.

You can certainly do what you wanted to do in the private constructor, yes - or you could, at the declaration of your variable, just do it there. (i.e. private ItemCollection _myList = ItemCollection.NewItemCollection();)

I think the most advocated way is with the private constructor, as in the fetch you might be reassigning it anyways - (i.e. _myList = ItemCollection.GetItemCollection(dr), where dr is the datareader).

 

 

 

 

RichardETVS replied on Friday, April 13, 2007

Ok, thanks for the explanations :)

Copyright (c) Marimer LLC