none
Problem implementing business logic using LINQ To SQL RRS feed

  • Question

  •  

    I’m looking for some help on how to implement a partial class to add business logic validation to a web app using LINQ To SQL. I am using the Northwind Products table to write my business logic against. I tried implementing the code demonstrated by Scott Guthrie in his LINQ to SQL part 5 tutorial posted 07/16/2007.

     

    http://weblogs.asp.net/scottgu/archive/2007/07/16/linq-to-sql-part-5-binding-ui-using-the-asp-linqdatasource-control.aspx.

     

    Scott adds a partial class to the web app and adds an OnValidate() partial method to this partial class to do some validation checking. This Product partial method created is in a separate file from the dbml partial class code. The code is:

     

    Partial Public Class Product

        Private Sub OnValidate()

            If (Me.Discontinued = True) And (Me.UnitsOnOrder > 0) Then

                Throw New ArgumentException("Reorder level can't be greater than 0 if Product is discontinued")

            End If

        End Sub

    End Class

     

    When I place the partial method Private Sub OnValidate() in the dbml partial class and run the program and try to edit a products record, the OnValidate() method is called as you would expect and the OnValidate() business logic is implemented. When I remove the code from the dbml generated partial class and place it in a separate partial class file as listed above, this partial method never gets called when I run the app and try to edit a products record. I looked at the dbml class file and under the Declarations dropdown and I can see the OnValidate() method listed although it is grayed out. There is also the OnValidate() method created by the dbml generated partial class which is empty.

     

    Why does the partial method never get called if it exits in a separate file? Am I doing something wrong in implementing this partial class?

     

    Thanks for any help

     

    Monday, November 10, 2008 9:38 PM

All replies

  •  JDS1076 wrote:

    Why does the partial method never get called if it exits in a separate file? Am I doing something wrong in implementing this partial class?

     

    Is your partial class in the right namespace, and in the same assembly as the dbml-generated code?

    Tuesday, November 11, 2008 3:17 AM
    Answerer
  • Both my dbml file and my custom partial class reside in the App_Code folder and I am using only the code generator generated namespaces. I am also using only the assemblies that were generated when the web.config file was created. Does this help?

     

    Tuesday, November 11, 2008 4:02 AM