Please help me, have a question with EditableRootListBase inherited classPlease help me, have a question with EditableRootListBase inherited class
Old forum URL: forums.lhotka.net/forums/t/7078.aspx
niijyeni posted on Thursday, June 11, 2009
I create two class with this code, and bind to a datagridview,when addnew item, system alway throw a NotSupportedException(Resources.NoSaveChildException).
please help me.thanks everyone.
code:
---------------------------------------------------------------
[Serializable]
public class Container : BusinessBase
{
#region Business Methods
private static PropertyInfo IdProperty =
RegisterProperty(typeof(Container), new PropertyInfo("Id", "Id of Container"));
public Guid Id
{
get { return GetProperty(IdProperty); }
set { SetProperty(IdProperty, value); }
}
private static PropertyInfo NameProperty =
RegisterProperty(typeof(Container), new PropertyInfo("Name", "Name of Container"));
public string Name
{
get { return GetProperty(NameProperty); }
set { SetProperty(NameProperty, value); }
}
private static PropertyInfo CapacityProperty =
RegisterProperty(typeof(Container), new PropertyInfo("Capacity", "Capacity of Container"));
public int Capacity
{
get { return GetProperty(CapacityProperty); }
set { SetProperty(CapacityProperty, value); }
}
#endregion
#region Validation Rules
protected override void AddBusinessRules()
{
// TODO: add validation rules
//ValidationRules.AddRule(RuleMethod, "");
}
#endregion
#region Authorization Rules
protected override void AddAuthorizationRules()
{
// TODO: add authorization rules
//AuthorizationRules.AllowWrite("Name", "Role");
}
private static void AddObjectAuthorizationRules()
{
// TODO: add authorization rules
//AuthorizationRules.AllowEdit(typeof(EditableChild), "Role");
}
#endregion
#region Factory Methods
internal static Container NewContainer()
{
return DataPortal.CreateChild();
}
internal static Container GetContainer(object rootData)
{
return new Container(rootData);
}
private Container()
{
/* Require use of factory methods */
}
private Container(object rootData)
{
Fetch(rootData);
}
#endregion
#region Data Access
protected override void Child_Create()
{
// TODO: load default values
// omit this override if you have no defaults to set
LoadProperty(IdProperty, Guid.NewGuid());
LoadProperty(CapacityProperty, 100);
base.Child_Create();
}
private void Fetch(object rootData)
{
LoadProperty(IdProperty, ((DataAccessLayer.Containers)rootData).ContainerId);
LoadProperty(NameProperty, ((DataAccessLayer.Containers)rootData).ContainerName);
LoadProperty(CapacityProperty, ((DataAccessLayer.Containers)rootData).ContainerCapacity);
MarkOld();
}
protected override void DataPortal_Insert()
{
using (var ctx = ContextManager.GetManager(DataAccessLayer.Database.SmartSyncDemo))
{
var item = new DataAccessLayer.Containers();
item.ContainerId = this.Id;
item.ContainerName = this.Name;
item.ContainerCapacity = this.Capacity;
ctx.DataContext.Containers.Attach(item);
ctx.DataContext.SubmitChanges();
}
}
protected override void DataPortal_Update()
{
using (var ctx = ContextManager.GetManager(DataAccessLayer.Database.SmartSyncDemo))
{
var item = ctx.DataContext.Containers.Single(o => o.ContainerId == ReadProperty(IdProperty));
item.ContainerName = ReadProperty(NameProperty);
item.ContainerCapacity = ReadProperty(CapacityProperty);
ctx.DataContext.SubmitChanges();
}
}
protected override void DataPortal_DeleteSelf()
{
using (var ctx = ContextManager.GetManager(DataAccessLayer.Database.SmartSyncDemo))
{
var item = ctx.DataContext.Containers.Single(o => o.ContainerId == ReadProperty(IdProperty));
ctx.DataContext.Containers.DeleteOnSubmit(item);
ctx.DataContext.SubmitChanges();
}
}
#endregion
}
[Serializable]
public class Containers : EditableRootListBase
{
#region Factory Methods
internal static Containers NewEditableChildList()
{
return DataPortal.Create();
}
internal static Containers GetContainers()
{
return DataPortal.Fetch();
}
private Containers()
{
AllowNew = true;
}
protected override object AddNewCore()
{
var item = Container.NewContainer();
Add(item);
return item;
}
#endregion
#region Data Access
private void DataPortal_Fetch()
{
using (var ctx = ContextManager.GetManager(DataAccessLayer.Database.SmartSyncDemo))
{
var items = from data in ctx.DataContext.Containers
select data;
RaiseListChangedEvents = false;
foreach (var child in items)
this.Add(Container.GetContainer(child));
RaiseListChangedEvents = true;
}
}
#endregion
}
JoeFallon1 replied on Thursday, June 11, 2009
The ERLB list is a list of ROOT objects. Not CHILD objects. That is what the exception is trying to tell you. It won't save a child object. Replace your child objects with root objects and it should work fine.
BTW - the ERLB class seems to be quite popular for some reason. I have never created one in 5 years. It has a very limited use case - binding to a winforms grid. I usually create a normal root BO to contain a standard child collection and everything is fine. But I am using web forms almost all the time so I really have no need for this.
Joersbaker0 replied on Thursday, June 11, 2009
JoeFallon1: BTW - the ERLB class seems to be quite popular for some reason. I have never created one in 5 years. It has a very limited use case - binding to a winforms grid.
This may be the official party line, but what other editable CSLA collection is generally usable when the collection can be large?
BusinessListBase classes save as entire unit (very useful in some scenarios), but this seems to require the entire collection be sent over the data portal just to save one item.
esaulsberry replied on Thursday, June 11, 2009
To extend what Joe is saying, take a look at the Editable Root Collection pattern, which is a root collection of editable children. It works very nicely for data grids, etc. It's p.187 in the 2008 book.
In the ERC,
protected override void DataPortal_Update()
{
Data.
MyDataContext dc = new Data.MyDataContext();
base.Child_Update(dc);
dc.SubmitChanges();
}
niijyeni replied on Thursday, June 11, 2009
If I have table with this structure, named A
ID
ParentID
Name
ParentID(foreign key) -> ID, and some other table may be use ID as foreign key. such as B table have a filed A_ID.
B table structure:
ID
A_ID
Name
All data like a tree, which base class i need it?
How to write some csla class for manager this data.... i'm lost.
need bind to datagridview, thanks everyone.
please help meniijyeni replied on Saturday, June 13, 2009
Does anyone help me...
Lhotka, are you there....Copyright (c) Marimer LLC