I've encountered the following scenario a couple of times and always seem to be baffled by the best way of doing it... so I'm calling on anyone here to help!?
I have a Customer object. A customer will have a billing address (Address object) with a 1 to 1 relationship.
I have a registration page which will allow a user to enter both Customer details and the billing address (1 to 1 basis), with the address entry being on an AddressEntryControl. The customer details are in a FormView, which contains an AddressEntryControl being bound to Customer.BillingAddress (<%# ((Customer)Container.DataItem).BillingAddress %>).
All binding is done using the CslaDataSource, with the CustomerDataSource_UpdateObject as the following:
protected
void CustomerDataSource_UpdateObject(object sender, Csla.Web.UpdateObjectArgs e){
Customer customer = GetCustomer();Csla.Data.
DataMapper.Map(e.Values, customer, "Id");// etc...
}
The data mapper will (currently) not update the BillingAddress which I assume is to do with the AddressEntryControl not storing the bound address in session... but... should the control's AddressDataSource save the billing address, or should this be the responsibility of the CustomerDataSource??
Has anyone got any best practices?
The BillingAddress property of the Customer object was previously using lazy loading. i.e. The Customer object had a billingAddressId, but also a non-serialized Address and a BillingAddress property that would call Address.GetAddress(this.billingAddressId) if the non serialized billingAddress property is null. If I were to make the BillingAddress a proper variable within the customer class then I guess it would be worth the Customer object data access saving/updating this address object.
One approach that I used before in dealing with 1-1 db relationships
that are entered on a single screen it roll them up into a single object.
In other words, a single stored procedure would join two tables and return
single result set. The update stored procedure would update both
tables. The only trick part is to deal with absence of data in a related
table. That time as part of “select” stored procedure I
selected a flag that indicated absence of data in Address table. In other
words, I used stored procedures in conjunction with customer object as ORM
tool.
Sergey Barskiy
Senior Consultant
office: 678.405.0687 |
mobile: 404.388.1899
Microsoft Worldwide Partner of the Year | Custom
Development Solutions, Technical Innovation
From: pfeds
[mailto:cslanet@lhotka.net]
Sent: Sunday, April 27, 2008 9:24 AM
To: Sergey Barskiy
Subject: [CSLA .NET] Object/Child Object - ASP.NET Databinding to
Page/Web User Control
I've encountered the following scenario a couple of times and always seem to
be baffled by the best way of doing it... so I'm calling on anyone here to
help!?
I have a Customer object. A customer will have a billing address
(Address object) with a 1 to 1 relationship.
I have a registration page which will allow a user to enter both Customer
details and the billing address (1 to 1 basis), with the address entry being on
an AddressEntryControl. The customer details are in a FormView, which
contains an AddressEntryControl being bound to Customer.BillingAddress (<%# ((Customer)Container.DataItem).BillingAddress
%>).
All binding is done using the CslaDataSource, with the
CustomerDataSource_UpdateObject as the following:
protected void
CustomerDataSource_UpdateObject(object sender,
Csla.Web.UpdateObjectArgs e)
{
Customer customer = GetCustomer();
Csla.Data.DataMapper.Map(e.Values, customer, "Id");
// etc...
}
The data mapper will (currently) not update the BillingAddress which I
assume is to do with the AddressEntryControl not storing the bound address
in session... but... should the control's AddressDataSource save the billing
address, or should this be the responsibility of the CustomerDataSource??
Has anyone got any best practices?
The BillingAddress property of the Customer object was previously using lazy
loading. i.e. The Customer object had a billingAddressId, but also a
non-serialized Address and a BillingAddress property that would call
Address.GetAddress(this.billingAddressId) if the non serialized billingAddress
property is null. If I were to make the BillingAddress a proper variable within
the customer class then I guess it would be worth the Customer object data
access saving/updating this address object.
Thanks Sergey - that's is a method I hadn't considered.
I would preferably keep the two objects seperate at the moment if I can. There are other places in the website where I want functionality to pick the child address from a popup list.
I'm having a real nightmare binding to the Address user control within the form view and can't seem to find much help on google. The code is roughly as follows:
<
asp:FormView ID="RegistrationFormView" runat="server"...etc>/* Customer name, email, etc bound to Customer object */
<
uc1:AddressEntryControl ID="AddressEntryControl" runat="server" Address='<%# Bind("BillingAddress") %>' />/* Other customer information such as password bound to Customer object */
</asp:FormView>
The problem is that when I click the submit button on the page I can map the values to the Customer object, but the difficulty is getting the Address information from the AddressEntryControl in the FormView.
Surely somebody has a good method of doing this?
Copyright (c) Marimer LLC