Business Logic Framework - Tutorial 24:
Centralizing the business logic in a typical fashion can result in lack of control and flexibility for the user interface developer. The approach offered by the Business Logic Framework ensures that the control and flexibility is always in the architects' and developers' hands. The target of this tutorial is to demonstrate that you can leverage the centralized logic and additionally do further processing based on your requirements.
| using (Users user = new Users()) | |
| { | |
| string userName = tlsUserName.Text; | |
| if (!String.IsNullOrEmpty(userName)) | |
| { | |
| // LoadByUserName method that we custom wrote in the CustomizedUsers.cs file in the BusinessLogicLayer | |
| // loads the Users object from a given user name and loads the Roles and any associated Customer + Order records. | |
| // LoadByUserName method only searches for records that are active i.e. IsActive is true | |
| // To be able to add additional criteria and modify the behavior, we can do something like the following: | |
| user.CaseConversion = CaseTypes.Upper; | |
| // We can further control the user object by specifying additional criteria that will be used while searching for additional users. | |
| user.IsLockedOut.Value = false; | |
| user.IsLockedOut.UseInSearch = true; | |
| // The above three lines demonstrate that you can excersice extreme amount of control over your business objects even after you have | |
| // centralized your logic. This can be very useful when you have something that is very specific to your interface and is not a fit | |
| // for a global change or addition to the centralized logic. | |
| Users.MembershipStatus result = user.LoadByUserName(userName); | |
| if (result != Users.MembershipStatus.Success) | |
| { | |
| switch (result) | |
| { | |
| case Users.MembershipStatus.InvalidUserName: | |
| { | |
| MessageBox.Show(userName + " is not a valid user name"); | |
| break; | |
| } | |
| default: | |
| { | |
| if (!String.IsNullOrEmpty(user.ErrorString)) | |
| { | |
| MessageBox.Show("Error: " + user.ErrorString); | |
| } | |
| else | |
| { | |
| MessageBox.Show("Unable to load the user information. Please try again!"); | |
| } | |
| break; | |
| } | |
| } | |
| } | |
| else | |
| { | |
| MessageBox.Show("User loaded"); | |
| if (user.UserID_UserRoles_Child != null) | |
| { | |
| // Since the LoadByUserName created a join with the UserID_UserRoles_Child object instance the ResultSet properties of both the user and UserID_UserRoles_Child instances share the same data. | |
| this.dgvRoles.DataSource = user.UserID_UserRoles_Child.ResultSet; | |
| // The shared data set has multiple tables and hence we need to set the DataMember property with the name of the table in the data set that corrosponds to the UserID_UserRoles_Child object instance. | |
| this.dgvRoles.DataMember = user.UserID_UserRoles_Child.GetResultSetName(); | |
| } | |
| if (user.UserID_Customers_Child != null) | |
| { | |
| // Since the UserID_Customers_Child (Customers) object also has a join with the CustomerID_Orders_Child (Orders) and Find method will automatically include all the visible fields from the Orders object | |
| // into the result set of the Customers object. So in this case we get all Customer+Orders records in one table for the User record that was just loaded. | |
| this.dgvOrders.DataSource = user.UserID_Customers_Child.ResultSet; | |
| this.dgvOrders.DataSource = user.UserID_Customers_Child.GetResultSetName(); | |
| } | |
| } | |
| } | |
| else | |
| { | |
| MessageBox.Show("User name must be specified!"); | |
| } | |
| } | |
| Using user As Users = New Users() | |
| Dim userName As String = tlsUserName.Text | |
| If Not [String].IsNullOrEmpty(userName) Then | |
| ' LoadByUserName method that we custom wrote in the CustomizedUsers.cs file in the BusinessLogicLayer | |
| ' loads the Users object from a given user name and loads the Roles and any associated Customer + Order records. | |
| ' LoadByUserName method only searches for records that are active i.e. IsActive is true | |
| ' To be able to add additional criteria and modify the behavior, we can do something like the following: | |
| user.CaseConversion = CaseTypes.Upper | |
| ' We can further control the user object by specifying additional criteria that will be used while searching for additional users. | |
| user.IsLockedOut.Value = True | |
| user.IsLockedOut.UseInSearch = True | |
| ' The above three lines demonstrate that you can excersice extreme amount of control over your business objects even after you have | |
| ' centralized your logic. This can be very useful when you have something that is very specific to your interface and is not a fit | |
| ' for a global change or addition to the centralized logic. | |
| Dim result As Users.MembershipStatus = user.LoadByUserName(userName) | |
| If result <> Users.MembershipStatus.Success Then | |
| Select Case result | |
| Case Users.MembershipStatus.InvalidUserName | |
| MessageBox.Show(userName + " is not a valid user name") | |
| Exit Select | |
| Case Else | |
| If Not [String].IsNullOrEmpty(user.ErrorString) Then | |
| MessageBox.Show("Error: " + user.ErrorString) | |
| Else | |
| MessageBox.Show("Unable to load the user information. Please try again!") | |
| End If | |
| Exit Select | |
| End Select | |
| Else | |
| MessageBox.Show("User loaded") | |
| If user.UserID_UserRoles_Child IsNot Nothing Then | |
| ' Since the LoadByUserName created a join with the UserID_UserRoles_Child object instance the ResultSet properties of both the user and UserID_UserRoles_Child instances share the same data. | |
| Me.dgvRoles.DataSource = user.UserID_UserRoles_Child.ResultSet | |
| ' The shared data set has multiple tables and hence we need to set the DataMember property with the name of the table in the data set that corrosponds to the UserID_UserRoles_Child object instance. | |
| Me.dgvRoles.DataMember = user.UserID_UserRoles_Child.GetResultSetName() | |
| End If | |
| If user.UserID_Customers_Child IsNot Nothing Then | |
| ' Since the UserID_Customers_Child (Customers) object also has a join with the CustomerID_Orders_Child (Orders) and Find method will automatically include all the visible fields from the Orders object | |
| ' into the result set of the Customers object. So in this case we get all Customer+Orders records in one table for the User record that was just loaded. | |
| Me.dgvOrders.DataSource = user.UserID_Customers_Child.ResultSet | |
| Me.dgvOrders.DataSource = user.UserID_Customers_Child.GetResultSetName() | |
| End If | |
| End If | |
| Else | |
| MessageBox.Show("User name must be specified!") | |
| End If | |
| End Using | |
