Creating a CS extension to add custom Classes

NachitoMax 411 Reputation points
2020-09-17T12:58:12.763+00:00

Hi

Im ok with VB.Net, never made a VS addin before so im looking for some direction / advice. I find that in all of my projects i copy / paste a standard framework of BLL DAL classes for database connection & retrieval. While i dont mind doing that, i thought it would be good to make an addin instead that let me populate a form and create the BLL DAL's instead? Im sure its possible but im yet to find out how. Here was my idea-

Have a standard scaffold code set up with parameters that i could replace as properties.
display a form with datagrid or listbox and add properties and property type to the list
add the list of properties to the relevant class
add the property names to the class functions as needed.
create the classes and place them into a project folder DATA/BLL & DATA/DAL
Check for existing classes
Ive added my BLL DAL code here to give an idea of what i am working with. Are there any good tutorials i can follow that give reasonable explanations? Specifically in the replacement of parameters with properties.

I figured that I could add parameters to NameSpace, Class name, Namespace.Class calls, Properties, Function calls. I can write this in vb.net, just not sure how to approach it as a VS extension. Are there any templates available that would help me understand more? There aren't any with my version of VS (2017)

BLL

Imports Microsoft.VisualBasic

Namespace TestApp.BLL
    Public Class TestData
#Region "Globals"
        Dim ED_DAL As TestApp.DAL.TestData = New TestApp.DAL.TestData
#End Region
#Region "Methods"
        Public Function SelectTestData(ByVal idNumber As String) As List(Of TestApp.BLL.TestData_details)
            SelectTestData = ED_DAL.SelectTestData(idNumber)
        End Function
        Public Function UpdateTestData(ByVal id As Integer,
                                       ByVal idNumber As String,
                                       ByVal type As String,
                                       ByVal parent As String,
                                       ByVal propertyName As String,
                                       ByVal propertyValue As String) As List(Of TestApp.BLL.TestData_details)
            UpdateTestData = ED_DAL.UpdateTestData(id, idNumber, type, parent, propertyName, propertyValue)
        End Function
        Public Sub DeleteTestData(ByVal id As Integer)
            ED_DAL.DeleteTestData(id)
        End Sub
        Public Function InsertTestData(ByVal id As Integer,
                                       ByVal idNumber As String,
                                       ByVal type As String,
                                       ByVal parent As String,
                                       ByVal propertyName As String,
                                       ByVal propertyValue As String) As List(Of TestApp.BLL.TestData_details)
            InsertTestData = ED_DAL.InsertTestData(id, idNumber, type, parent, propertyName, propertyValue)
        End Function
#End Region
    End Class
    Public Class TestData_details
#Region "Properties"
        Public Property _id As Integer = 0
        Public Property _idNumber As String = String.Empty
        Public Property _type As String = String.Empty
        Public Property _parent As String = String.Empty
        Public Property _propertyName As String = String.Empty
        Public Property _propertyValue As String = String.Empty

#End Region
#Region "Constructors"
        Public Sub New()
        End Sub
        Public Sub New(ByVal Id As Integer,
                       ByVal IdNumber As String,
                       ByVal Type As String,
                       ByVal Parent As String,
                       ByVal PropertyName As String,
                       ByVal PropertyValue As String)

            Me._id = Id
            Me._idNumber = IdNumber
            Me._parent = Parent
            Me._type = Type
            Me._parent = Parent
            Me._propertyName = PropertyName
            Me._propertyValue = PropertyValue
        End Sub
#End Region
    End Class
End Namespace

DAL
Imports System.Data
Imports System.Data.SqlClient

Namespace TestApp.DAL
    Public Class TestData
#Region "Globals"
        Dim ED_Conn As ClassConn = New ClassConn()
        Dim conn As SqlConnection = ED_Conn.GetConnection()
#End Region
#Region "Methods"
        Public Function SelectTestData(ByVal idNumber As String) As List(Of TestApp.BLL.TestData_details)
            SelectTestData = New List(Of TestApp.BLL.TestData_details)
            Dim cmd As New SqlCommand("dbo.usp_tblTestDataSelect", conn) With {
                .CommandType = CommandType.StoredProcedure
            } 'update stored proc
            cmd.Parameters.Add(New SqlParameter("@idNumber", idNumber)) 'update param
            Try
                conn.Open()
                Dim reader As SqlDataReader = cmd.ExecuteReader()
                SelectTestData = ConvertTestData(reader)
            Catch ex As Exception
                'Do Some Error Handling Here...
            Finally
                If (conn.State = ConnectionState.Open) Then
                    conn.Close()
                End If
            End Try
        End Function
        Public Function UpdateTestData(ByVal id As Integer,
                                       ByVal idNumber As String,
                                       ByVal type As String,
                                       ByVal parent As String,
                                       ByVal propertyName As String,
                                       ByVal propertyValue As String) As List(Of TestApp.BLL.TestData_details)
            UpdateTestData = New List(Of TestApp.BLL.TestData_details)

            Dim cmd As New SqlCommand("dbo.usp_tblTestDataUpdate", conn) With {
                .CommandType = CommandType.StoredProcedure
            } 'update stored proc
            'cmd.Parameters.Add(New SqlParameter("@ID", id))
            cmd.Parameters.Add(New SqlParameter("@idNumber", idNumber))
            cmd.Parameters.Add(New SqlParameter("@Type", type))
            cmd.Parameters.Add(New SqlParameter("@Parent", parent))
            cmd.Parameters.Add(New SqlParameter("@Value", parent))
            cmd.Parameters.Add(New SqlParameter("@PropertyName", propertyName))
            cmd.Parameters.Add(New SqlParameter("@PropertyValue", propertyValue))

            Try
                conn.Open()
                Dim reader As SqlDataReader = cmd.ExecuteReader()
                UpdateTestData = ConvertTestData(reader)
            Catch ex As Exception
                'Do Some Error Handling Here...
            Finally
                If (conn.State = ConnectionState.Open) Then
                    conn.Close()
                End If
            End Try
        End Function
        Public Sub DeleteTestData(ByVal id As Integer)
            Dim cmd As New SqlCommand("dbo.usp_tblTestDataDelete", conn) With {
                .CommandType = CommandType.StoredProcedure
            }
            cmd.Parameters.Add(New SqlParameter("@ID", id))
            Try
                conn.Open()
                cmd.ExecuteNonQuery()
            Catch ex As Exception
                'Do Some Error Handling Here...
            Finally
                If (conn.State = ConnectionState.Open) Then
                    conn.Close()
                End If
            End Try
        End Sub
        Public Function InsertTestData(ByVal id As Integer,
                                       ByVal idNumber As String,
                                       ByVal type As String,
                                       ByVal parent As String,
                                       ByVal propertyName As String,
                                       ByVal propertyValue As String) As List(Of TestApp.BLL.TestData_details)
            InsertTestData = New List(Of TestApp.BLL.TestData_details)

            Dim cmd As New SqlCommand("dbo.usp_tblTestDataInsert", conn) With {
                .CommandType = CommandType.StoredProcedure
            }
            'cmd.Parameters.Add(New SqlParameter("@ID", id))
            cmd.Parameters.Add(New SqlParameter("@idNumber", idNumber))
            cmd.Parameters.Add(New SqlParameter("@Type", type))
            cmd.Parameters.Add(New SqlParameter("@Parent", parent))
            cmd.Parameters.Add(New SqlParameter("@PropertyName", propertyName))
            cmd.Parameters.Add(New SqlParameter("@PropertyValue", propertyValue))
            Try
                conn.Open()
                Dim reader As SqlDataReader = cmd.ExecuteReader()
                InsertTestData = ConvertTestData(reader)
            Catch ex As Exception
                'Do Some Error Handling Here...
            Finally
                If (conn.State = ConnectionState.Open) Then
                    conn.Close()
                End If
            End Try
        End Function
#End Region
#Region "Helpers"
        Protected Function ConvertTestData(ByVal reader As SqlDataReader) As List(Of TestApp.BLL.TestData_details)
            ConvertTestData = New List(Of TestApp.BLL.TestData_details)
            If reader.HasRows Then
                While reader.Read
                    ConvertTestData.Add(New TestApp.BLL.TestData_details(CInt(reader("ID")),
                                                                        reader("idNumber").ToString(),
                                                                        reader("Type").ToString(),
                                                                        reader("Parent").ToString(),
                                                                        reader("PropertyName").ToString(),
                                                                        reader("PropertyValue").ToString()))
                End While
            End If
        End Function
#End Region
    End Class
End Namespace

Any guidance is greatly appreciated

Thanks

Nacho

Not Monitored
Not Monitored
Tag not monitored by Microsoft.
36,149 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Dylan Zhu-MSFT 6,406 Reputation points
    2020-09-18T08:02:56.49+00:00

    Hi Nacho,

    Maybe you could use this extension: Windows Forms Wizard Templates to create a form wizard as a VS extension, and read this interface: CodeModel Interface to add your code into source file.

    Best Regards,
    Dylan

    0 comments No comments