I have posted a new build with some enhancements and bug fixes. A new enhancement is the transaction support that was added to the "Business Component Layer" Add-On (be sure to reimport the new version of this Add-On into your repository). Here is an example of code:
[Database]--------------------------------------------------------------------
CREATE DATABASE DemoBC (
CREATE TABLE [Category] (
[CategoryID] uniqueidentifier -> ID
[CategoryName] [varchar] (255), -> Name
)
CREATE TABLE [Product] (
[ProductID] uniqueidentifier, -> ID
[ProductName] [varchar] (255), -> Name
[ProductCategoryID] [uniqueidentifier], -> CategoryID
CONSTRAINT [FK_Product_Category] FOREIGN KEY
([ProductCategoryID]) REFERENCES [Category] ([CategoryID])
)
)
[C#.NET]--------------------------------------------------------------------
using System.Data.SqlTypes;
using System.Data.SqlClient;
using DemoBC.BusinessComponents;
// This method is going to be called from different contexts:
// ConnectionString, SqlConnection and SqlTransaction
private void DoSomeStuff(Categories categories) {
foreach (Category myCategory in categories) {
for (int index = 0; index < 10; index++) {
Product newProduct = new Product();
newProduct.Name = string.Format("Product {0}", index);
// You do not need to do this anymore: newProduct.CategoryID = myCategory.ID
// It is done automatically from the category products collection
newProduct = (Product)myCategory.Products.Add(newProduct);
}
foreach (Product myProduct in myCategory.Products.AddedRecords) {
// Here is how we can browse the newly added records
myProduct.Name = new SqlString(string.Format("{0} - Name was updated", myProduct.Name.Value));
myProduct.Update();
}
}
}
// We use a connection string
private void button1_Click(object sender, System.EventArgs e) {
string connectionString = "...";
Categories myCategories = new Categories(connectionString);
DoSomeStuff(myCategories);
}
// We use a SqlConnection
private void button2_Click(object sender, System.EventArgs e) {
string connectionString = "...";
SqlConnection sqlConnection = new SqlConnection(connectionString);
sqlConnection.Open();
Categories myCategories = new Categories(sqlConnection);
DoSomeStuff(myCategories);
// Warning, if you need to keep using the objects loaded by this
// sqlConnection, you should NOT close the connection
sqlConnection.Close();
}
// We use a SqlTransaction
private void button3_Click(object sender, System.EventArgs e) {
string connectionString = "...";
SqlConnection sqlConnection = new SqlConnection(connectionString);
sqlConnection.Open();
SqlTransaction sqlTransaction =
sqlConnection.BeginTransaction("TransactionName");
Categories myCategories = new Categories(sqlTransaction);
DoSomeStuff(myCategories);
sqlTransaction.Commit(); // or sqlTransaction.Rollback();
// Warning, if you need to keep using the objects loaded by this
// sqlConnection, you should NOT close the connection
sqlConnection.Close();
}
[VB.NET]--------------------------------------------------------------------
Imports System.Data.SqlTypes
Imports System.Data.SqlClient
Imports DemoBC.BusinessComponents
' This method is going to be called from different contexts:
' ConnectionString, SqlConnection and SqlTransaction
Private Sub DoSomeStuff(ByVal categories As Categories)
For Each myCategory As Category In categories
For index As Integer = 0 To 9
Dim newProduct As New Product()
newProduct.Name = String.Format("Product {0}", index)
' You do not need to do this anymore: newProduct.CategoryID = myCategory.ID
' It is done automatically from the category products collection
newProduct = CType(myCategory.Products.Add(newProduct), Product)
Next
For Each myProduct As Product In myCategory.Products.AddedRecords
' Here is how we can browse the newly added records
myProduct.Name = New SqlString(String.Format("{0} - Name was updated", myProduct.Name.Value))
myProduct.Update()
Next
Next
End Sub
' We use a connetion string
Private Sub button1_Click(ByVal sender As Object , ByVal e As System.EventArgs) Handles Button1.Click
Dim connectionString As String = "..."
Dim myCategories As New Categories(connectionString)
DoSomeStuff(myCategories)
End Sub
' We use SqlConnection
Private Sub button2_Click(ByVal sender As Object , ByVal e As System.EventArgs) Handles Button2.Click
Dim connectionString As String = "..."
Dim sqlConnection As New SqlConnection(connectionString)
sqlConnection.Open()
Dim myCategories As New Categories(sqlConnection)
DoSomeStuff(myCategories)
' Warning, if you need to keep using the objects loaded by this
' sqlConnection, you should NOT close the connection
sqlConnection.Close()
End Sub
' We use SqlTransaction
Private Sub button3_Click(ByVal sender As Object , ByVal e As System.EventArgs) Handles Button3.Click
Dim connectionString As String = "..."
Dim sqlConnection As New SqlConnection(connectionString)
sqlConnection.Open()
Dim sqlTransaction As SqlTransaction =
sqlConnection.BeginTransaction("TransactionName")
Dim myCategories = New Categories(sqlTransaction)
DoSomeStuff(myCategories)
sqlTransaction.Commit() ' or sqlTransaction.Rollback()
' Warning, if you need to keep using the objects loaded by this
' sqlConnection, you should NOT close the connection
sqlConnection.Close()
End Sub