below is my Factory Method with one parameter, why the Dapatortal.CreateChild not fire the Child_Create?please help me!
public static NonDynamic NewNonDynamic(int questionId)
{
return DataPortal.CreateChild<NonDynamic>(questionId);
}
private void Child_Create(int questionId)
{
LoadProperty<int>(QuestionIdProperty, questionId);
}
Is this on Silverlight? If so, the method must be public because of restrictions on reflection in Silverlight.
Yes, it 's on Silverlight.
I tried make Child_Create public, still not work
Still not be resolved, please help me out.
Ok, i fixed it by this way:
i move the Child_Create method out of Conditional Compilation, it's works, but i don't know why?
// Child_Create here is fired as expected
#if SIVLERLIGT
#else
// Child_Create here never be fired. this is where i put Child_Create previously
#endif
At least in your post you spelled it "SIVLERLIGT" instead of "SILVERLIGHT" - maybe that's the problem?
Sorry, i spelled wrong. but my real code is spell right. it 's not about the spelling, i sure about this.
Anyway, i have fixed this, although i don't know why!, i 'm sure as using CSLA more time, i will know it.
Thank you very much to help me.
So I guess I'll ask a really obvious question.
Are you calling that factory method from code running on the client? Because that would completely explain the behavior you are seeing - the child data portal methods run on the machine they are called - they never go through the network.
I would bet that you are calling that factory method on the client, therefore the data portal is calling the Child_XYZ method on the client. That would be completely expected behavior.
I call the factory Method in my ViewModel like this:
NonDynamics.Add(NonDynamic.NewNonDynamic(100));
so, is there any problems call the factory method by this way?
by the way, i used to add object to list like below, but this way requires call MarkAsChild() in constructor
NowDynamics.Add(new NonDynamic{QuestionId=100});
There's no problem doing that, as long as you understand that the child data portal runs where it is called - it never goes across the network.
So if the viewmodel (running on the client) calls the factory (running on the client), and the factory calls CreateChild (running on the client), then the Child_Create method will be called (on the client).
If you need it to run on the server, that's fine - you just need to use a "child creator" object - a topic I discuss and demonstrate in the Creating Business Objects and Data Access ebooks in the Using CSLA 4 ebook series.
this example will show the diffrence: play with it...
item.addnew(); //calls addnewCore
public partial class CompanyGroupECL
{
protected override void AddNewCore()
{
var child = CompanyGroupEC.NewChildCompanyGroupEC();
Add(child);
OnAddedNew(child);
//CompanyGroupEC.NewCompanyGroupEC((o, e) =>
// {
// if (e.Error != null)
// {
// throw e.Error;
// }
// else
// {
// var child = e.Object;
// Add(child);
// OnAddedNew(child);
// }
// }
//);
}
}
public partial class CompanyGroupEC
{
#if SILVERLIGHT
internal static CompanyGroupEC NewChildCompanyGroupEC() //always client and not async
{
return DataPortal.CreateChild<CompanyGroupEC>();
}
#else
public static void NewChildCompanyGroupEC(EventHandler<DataPortalResult<CompanyGroupEC>> callback)
{
DataPortal.CreateChild<CompanyGroupEC>(callback);
}
#endif
#if SILVERLIGHT
public static void NewCompanyGroupEC(EventHandler<DataPortalResult<CompanyGroupEC>> callback) // client and async
{
DataPortal.BeginCreate<CompanyGroupEC>(callback, DataPortal.ProxyModes.LocalOnly);
}
#else
public static void NewCompanyGroupEC(EventHandler<DataPortalResult<CompanyGroupEC>> callback)
{
DataPortal.BeginCreate<CompanyGroupEC>(callback);
}
#endif
//local create
protected void DataPortal_Create(Csla.DataPortalClient.LocalProxy<CompanyGroupEC>.CompletedHandler handler)
{
using (BypassPropertyChecks)
{
Name = "Enter Name";
}
AttendanceOfficer = DataPortal.CreateChild<AttendanceOfficerECL>();
Employee = DataPortal.CreateChild<EmployeeECL>();
CompanyGroup = DataPortal.CreateChild<CompanyGroupECL>();
base.DataPortal_Create(handler);
}
#if SILVERLIGHT
//local child create
public override void Child_Create()
{
using (BypassPropertyChecks)
{
Name = "Enter Name";
}
AttendanceOfficer = DataPortal.CreateChild<AttendanceOfficerECL>();
Employee = DataPortal.CreateChild<EmployeeECL>();
CompanyGroup = DataPortal.CreateChild<CompanyGroupECL>();
base.Child_Create();
}
#else
#endif
}
Copyright (c) Marimer LLC