Estou criando uma estrutura MVC que irá trabalhar semelhante ao Entity Framework dentro de um projeto VB.NET onde eu criei um atributo chamado TableAttribute
que irá definir o nome da tabela e dos schemas para o model, assim:
Namespace Models.DataAnnotations
<AttributeUsage(AttributeTargets.Class)>
Public Class TableAttribute : Inherits Attribute
Sub New(Name As String)
Me.Name = Name
End Sub
Public Property Name As String
Public Property Schema As String = "dbo"
End Class
End Namespace
Assim, eu posso acrescentá-lo em meus models que irão servir para fazer a camada de dados da aplicação:
Imports Models.DataAnnotations
Namespace Models
<Table("Usuario", Schema:="Sistema")>
Public Class UsuarioModel
Public Property Id As Integer
Public Property Login As String
Public Property Senha As String
End Class
End Namespace
Além disso, eu tenho um Módulo que é relacionado ao Namespace.Models
que mantém uma série de funções de extensão para os modelos:
Imports System.Reflection
Namespace Models
Public Module ModelsExtensions
<Extension()>
Function GetTable(Obj as Object) as String
Dim type As Type = Obj.GetType()
Dim temp As TableAttribute = type.GetCustomAttributes(GetType(TableAttribute), True)(0)
If (temp IsNot Nothing) Then Return $"{temp.Schema}.{temp.Name}"
Return $"dbo.{type.Name}"
End Function
End Module
End Namespace
Meu problema é que, para que eu consiga pegar o nome de uma tabela qualquer, eu sou obrigado a instanciar a mesma fazendo um new Usuario().GetTable()
.
Existe alguma forma de eu estender um método diretamente para o tipo Usuario
? O resultado que espero ficaria assim: Usuario.GetTable()