How do I keep my entities up-to-date using SP's

Posts   
 
    
G.I.
User
Posts: 172
Joined: 09-Jun-2005
# Posted on: 04-Oct-2005 10:13:25   

Hi,

I have to following problem. I am programming in C#, I am using SelfServicing and I am using some Stored Procedures (SP's) to insert or update my projects / project tasks, since in the SP's there is some complicated stuff going on, so I don't use Project.Save() at this point.

So, the following is going on: I have a ProjectEntity Project. This Project has a collection of project tasks, called ProjectTask. A ProjectEntity can have 0 to N project tasks.

If the Project is not saved yet, it has a ProjectNo equal to 0, so I can check this or use the Project.IsNew property to check if the Project is new.

If so, I have to insert the project using a SP and in an output parameter I get the ProjectNo. So after the SP has run, I set the ProjectNo value of the ProjectEntity to this new value and set the IsNew property to false.

This ProjectEntity is kept on the Session in my webapplication. In my ASPX page I create a ProjectManager instance which has a function:

public ProjectEntity SaveProject(ProjectEntity Project, bool LLBLGen)

In this function I call InsertProject task function and InsertProject function if it is a new Project

So after calling the InsertProject function I call this:


foreach (ProjectTaskEntity ProjectTask in Project.ProjectTask)
{
    ProjectTask = InsertProjectTask(ProjectTask, t);                                
}

//Function:
private ProjectTaskEntity InsertProjectTask(ProjectTaskEntity ProjectTask, ITransaction t)

Now I have three questions:

1: How should I pass the transaction so in my SaveProject function I can put a try catch around everything and do a correct rollback of everything if something fails? Is this correct already, should I pass it as ref?

2: It complains that I can't set the ProjectTask Entity, because it is ReadOnly. How can I save the ProjectTaskObjNo and ProjectTaskNo then into my ProjectTask Entity? I also tried to use ref, but that the compiler complains again that it's readonly...

3: How should I pass the entities so that I can save them on the session after the functioncall?

So just to make clear, what I am doing is:


SaveProject(ProjectEntity)
{
   Declare Transaction t

   TRY

   InsertProject(ProjectEntity, t)

   Foreach ProjectTask
      InsertProjectTask(ProjectTaskEntity, t)

   t.Commit();

   CATCH
      t.RollBack();

and what I want after the SaveProject function in my ASPX page is saving the ProjectEntity with the new data to the session again. (so including new data also in all project tasks)

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 04-Oct-2005 16:45:42   

1: How should I pass the transaction so in my SaveProject function I can put a try catch around everything and do a correct rollback of everything if something fails? Is this correct already, should I pass it as ref?

The code shows that you already pass the transaction correctly

2: It complains that I can't set the ProjectTask Entity, because it is ReadOnly. How can I save the ProjectTaskObjNo and ProjectTaskNo then into my ProjectTask Entity? I also tried to use ref, but that the compiler complains again that it's readonly...

Change the following code

foreach (ProjectTaskEntity ProjectTask in Project.ProjectTask)
{
    ProjectTask = InsertProjectTask(ProjectTask, t);                                
}

To

foreach (ProjectTaskEntity ProjectTask in Project.ProjectTask)
{
    InsertProjectTask(ProjectTask, t);                              
}

3: How should I pass the entities so that I can save them on the session after the functioncall?

I didn't understand this question.

what I want after the SaveProject function in my ASPX page is saving the ProjectEntity with the new data to the session again. (so including new data also in all project tasks)

(While I personally don't recommend saving large objects in the session) You should refetch the Project object with it's related entities again at the end of the SaveProject function.

G.I.
User
Posts: 172
Joined: 09-Jun-2005
# Posted on: 04-Oct-2005 19:38:50   

Walaa,

thank you for your answer, but I still have some questions:

You say this:


foreach (ProjectTaskEntity ProjectTask in Project.ProjectTask)
{
InsertProjectTask(ProjectTask, t); 
}

Does this mean that after I have run this function, the ProjectTask collection of my Project object is updated with the new values set in this function? Isn't it necessary to pass by ref then?

This is also what I meant with question 3. I have learned that you can for example pass a string to a function. In this function, when you change the value of the string, and you return from the function again, the string value is still the same as before the function unless you pass the string by reference. This is what I also meant with the Project and ProjectTask Entity. Maybe objects are always passed by ref? Is that what I see wrong?

The case with me is that I have a webapplication. The contents of project and project tasks is entered/changed by the user on multiple webpages. This means that I have to store the project and projecttask entity somewhere, and I preferred the Session object. Do you have a better solution then in this situation?

Gr.,

G.i.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 05-Oct-2005 16:17:36   

Hi,

What I meant is that you should not try to update the ProjectTask collection inside the Project object, since it's Read-Only, you can only refetch the Project object again to get the updated data.

The case with me is that I have a webapplication. The contents of project and project tasks is entered/changed by the user on multiple webpages. This means that I have to store the project and projecttask entity somewhere, and I preferred the Session object. Do you have a better solution then in this situation?

In your situation this will be an option, but take care of transactions and database connections left open waiting for a user reaction.

You may want to read about the "Unit of work" in the LLBLGen Pro documentation.