I have a patient class which has a child class notes.
When the patient loads i can load the child class too and make edits and save them back.
But when i have to do a search on all the notes and return only a few with matching criteria i call the GetAll() in my NotesList class. The search does return the records but the updates i make to the searched results do not seem to get saved. Can somebody help me understand why this is happening and how to fix it?
I'm not sure exactly what GetAll does, but I suspect (from your description) that it returns a new list that only references the filtered items.
That new list isn't a BusinessListBase subclass, and so can't be saved. It isn't a CSLA object at all probably.
You need to use a scheme that returns a bound list, and CSLA does help you with this. If you use LINQ queries to do your filter, you can use the ToSyncList extension method.
This will actually work on any ObservableCollection (such as BusinessListBase), and it creates a new list that is "bound" to the original list. So changes to this new list impact the original list too. Then you can save the original list and any changes to the bound list will be saved.
This is my code in the CSLA Notes.cs
public static Notes GetAll(long eID, string notes1) {
return DataPortal.FetchChild<Notes>(encounterID, notes1);
}
This is calling
protected void DataPortal_Fetch(NoteSearchCriteria criteria)
{ DataPortal.FetchChild<Notes>(criteria.EID, criteria.Notes1); }
which in turn is calling the
protected void Child_Fetch(long encounterID,string notes1) {
using (var ctx = Csla.Data.ObjectContextManager<P.Models.Enco.Data.EncoEntities>.GetManager(P.Models.Data.EncoEntities.Name))
IEnumerable<PMIS.Models.Encounters.Data.Notes> data = null;
data = ctx.ObjectContext.GetNotes(encounterID,notes1);
ReadData(data);
}
In my view model i have the following code:
Model = Notes.GetAll(1, e.MethodParameter.ToString());
It does give me the results but the parent object is set to null.
Are you sure your code is correct in the post? It looks like you are calling FetchChild from a root factory method - and that's not right. If that code is correct, your DataPortal_Fetch method will never be called at all, and the data portal won't work correctly at all.
I am sorry. You are right. The dataportal_fetch is not called at all. The GetAll() is calling the Child_Fetch directly. So how can i search my BusinessListBase subclass and not create a new list?
Hi,
1. Preferrably by sending search criteria as parameter to the DataPortal.Fetch<T> (and in turn passed on to DataPortal_Fetch Method) so that the database Query will use Your search criterias in the SQL.
2. If WindowsForms .- I prefer to use the FilteredBindingList class to do filtering on the Client..
3. Non-WindowsForms - you can filter a list on the Client (if list is already fetched) by using linq-2-objects and the Linq Exstension .ToSyncList()
I modified my code as follows:
In my view model I am using the BeginRefresh to update the model asynchronously.
BeginRefresh(callback => Notes.GetAll(1, e.MethodParameter.ToString(), callback));
In my CSLA Model Notes.cs, I have defined my GetAll as follows:
public static void GetAll(long encounterID, string notes1, EventHandler<DataPortalResult<Notes>> callback)
{
DataPortal.BeginFetch<Notes>(new Note { EncounterID = encounterID, Notes1 = notes1 }, callback);
}
And my DataPortal_Fetch as:
protected void DataPortal_Fetch(Note criteria)
{
using (var ctx = Csla.Data.ObjectContextManager<PMIS.Models.Encounters.Data.EncounterEntities>.GetManager(PMIS.Models.Data.EncounterEntities.Name))
{
IEnumerable<PMIS.Models.Encounters.Data.Notes> data = null;
data = ctx.ObjectContext.Notes.Where(n => n.EncounterID == criteria.EncounterID && n.Notes1.Contains(criteria.Notes1));
ReadData(data);
}
}
The ReadData is defined as follows:
private void ReadData(IEnumerable<PMIS.Models.Encounters.Data.Notes> data)
{
RaiseListChangedEvents = false;
foreach (var item in data)
{
this.Add(Note.Get(item));
}
RaiseListChangedEvents = true;
}
The readData is calling the Get method in my Note.cs defined as follows:
internal static Note Get(PMIS.Models.Encounters.Data.Notes data)
{
return DataPortal.FetchChild<Note>(data);
}
This is calling the Child_Fetch in Note.cs
private void Child_Fetch(PMIS.Models.Encounters.Data.Notes data)
{
ReadData(data);
}
The ReadData in Note.cs is to load all the property values.
private void ReadData(PMIS.Models.Encounters.Data.Notes data, bool includeChildren = true)
{
LoadProperty(NotesIDProperty, data.NotesID);
}
Again my model does get updated but my Parent which in this case is Encounter is set to null.
Some Additional information about my project.
I have the following object structure
P PI Encounter Notes In my WPF form there is one Save Button that triggers Model.Save in my Parent "P" object which in turn loads all my child objects. Only when i search my notes, i understand it is not loading from the encounter and hence the parent is being set to null. Please help me with this issue as i need to get this done soon.
JonnyBee, i have usedf your approach and my results are updated they still loose their parent.
Alright i have finally figured out how to set the parent but my update still does not work!!!
Okay. But my notes has a different view model than my encounters. How can i set the Notes property on the Encounter object.
JonnyBee can you give me an example?
Copyright (c) Marimer LLC