The C in CRUD Silverlight:
- Create new Silverlight business application "CRUD"
- Add ADO.NET Entity Data Model to CRUD.web
- Select the db, the tables, build the project
- Add Domain Service Class to CRUD.web
- Select the tables and also select the allow editing option for the table "tname"
- Build the project
- Keep two textboxes "ID" and "NAME" on MainPage.xaml
- Keep a button "SAVE NEW RECORD" on MainPage.xaml
- Build
Add the following code
Partial Public Class MainPage Inherits UserControl Dim dserv As New DomainService1 //default methods generated Private Sub Button1_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button1.Click 'declare a table object Dim table As New tname 'assign values to fields table.ID = TextBox1.Text table.NAME = TextBox2.Text 'add table object entity dserv.tnames.Add(table) 'submit the changes, to make it permenant in the db dserv.SubmitChanges() End Sub
Record inserted
Is this the easiest way for having no side effects to add a new record? Is this the optimal way to add a new record?
1 Answer 1
I don't think it's best-practice to put any logic that isn't strictly presentation-specific directly into a code-behind event handler like this, let alone that a UserControl
knows anything about any DomainService1
object.
If you're shooting for best-practices, you need to look into the Model-View-ViewModel pattern; it's not the job of any UI component to perform any kind of business or data logic.
-
\$\begingroup\$ Seems like the OP is more looking for what's easiest though. \$\endgroup\$Simon Forsberg– Simon Forsberg2013年11月20日 14:26:02 +00:00Commented Nov 20, 2013 at 14:26
-
\$\begingroup\$ @Simon true, but the post is also tagged [best-practices] \$\endgroup\$Mathieu Guindon– Mathieu Guindon2013年11月20日 15:39:06 +00:00Commented Nov 20, 2013 at 15:39
-
\$\begingroup\$ That's true, I would give you an upvote but I'm out of them for the time being (as usual). \$\endgroup\$Simon Forsberg– Simon Forsberg2013年11月20日 15:47:05 +00:00Commented Nov 20, 2013 at 15:47