How do I go about implementing ServiceStack cache in VB.net? I've seen many C# examples, but I am not able to transfer this onto vb.net.
The point I get stack in the 1st and 2nd argument of the ServiceStack.ServiceHost.RequestContextExtensions.ToOptimizedResultUsingCache
1st should be: ServiceStack.ServiceHost.IRequestContext - not sure
what IRequestContext is
2nd should be:
ServiceStack.CacheAccess.Providers.MemoryCacheClient - how do I set
this do cache default in config i.e. MemoryCacheClient
Code below, any suggestion much appreciated.
Global.asax.vb
Public Class Global_asax
Inherits System.Web.HttpApplication
Public Class HelloAppHost
Inherits AppHostBase
Public Sub New()
MyBase.New("Web Services", GetType(Wrapper).Assembly)
End Sub
Public Overrides Sub Configure(ByVal container As Container)
Routes.Add(Of GetProduct)("/GetProduct").Add(Of GetProduct)("/GetProduct/{*}")
Plugins.Add(New Cors.CorsFeature(allowedHeaders:="Content-Type, Authorization"))
container.Register(Of ICacheClient)(New MemoryCacheClient())
End Sub
End Class
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
Dim apphost = New HelloAppHost()
apphost.Init()
End Sub
End Class
WS.vb
Public Class Wrapper
Public Class WrapperGetProduct
Implements IService(Of GetProduct)
Public Function Execute(ByVal request As GetProduct) As Object Implements ServiceStack.ServiceHost.IService(Of GetProduct).Execute
Dim cachekey As String = "some_key"
Dim expireInTimespan = New TimeSpan(1, 0, 0)
Return ServiceStack.ServiceHost.RequestContextExtensions.ToOptimizedResultUsingCache(
ServiceStack.ServiceHost.IRequestContext, // not sure what this should be
ServiceStack.CacheAccess.Providers.MemoryCacheClient, // not sure what this should be - how do I set this to cache setted in configuration (in memory cache)?
cachekey, expireInTimespan,
Function() request.HandleRequest()
)
End Function
End Class
End Class
Use the new API
ToOptimizedResultUsingCache is an extension method for RequestContext which services inherit/expose, same with Cache (resolved automatically via IOC).
Example below converted from C#, caching/wrapping an existing service (AppConfig and Repository are resolved via IOC, registered in AppHost configure method).
Imports System.Collections.Generic
Imports ServiceStack.Common
Imports ServiceStack.ServiceHost
Imports ServiceStack.ServiceInterface.ServiceModel
Imports ServiceStack.Common.Web
Public Class SearchTerm
Public Property Latitude() As Double
Get
Return m_Latitude
End Get
Set
m_Latitude = Value
End Set
End Property
Private m_Latitude As Double
Public Property Longitude() As Double
Get
Return m_Longitude
End Get
Set
m_Longitude = Value
End Set
End Property
Private m_Longitude As Double
Public Property Term() As String
Get
Return m_Term
End Get
Set
m_Term = Value
End Set
End Property
Private m_Term As String
End Class
<Route("/lookup/searchterm", "GET")> _
Public Class SearchTermRequest
Implements IReturn(Of SearchTermResponse)
Public Property Term() As String
Get
Return m_Term
End Get
Set
m_Term = Value
End Set
End Property
Private m_Term As String
End Class
Public Class SearchTermResponse
Implements IHasResponseStatus
Public Property ResponseStatus() As ResponseStatus
Get
Return m_ResponseStatus
End Get
Set
m_ResponseStatus = Value
End Set
End Property
Private m_ResponseStatus As ResponseStatus
Public Property Results() As List(Of SearchTerm)
Get
Return m_Results
End Get
Set
m_Results = Value
End Set
End Property
Private m_Results As List(Of SearchTerm)
End Class
<Route("/cached/lookup/searchterm")> _
Public Class CachedSearchTermRequest
Implements IReturn(Of CachedSearchTermResponse)
Public ReadOnly Property CacheKey() As String
Get
Return UrnId.Create(Of CachedSearchTermRequest)(String.Format("{0}", Me.Term))
End Get
End Property
Public Property Term() As String
Get
Return m_Term
End Get
Set
m_Term = Value
End Set
End Property
Private m_Term As String
End Class
Public Class CachedSearchTermResponse
Implements IHasResponseStatus
Public Property ResponseStatus() As ResponseStatus
Get
Return m_ResponseStatus
End Get
Set
m_ResponseStatus = Value
End Set
End Property
Private m_ResponseStatus As ResponseStatus
Public Property Results() As List(Of SearchTerm)
Get
Return m_Results
End Get
Set
m_Results = Value
End Set
End Property
Private m_Results As List(Of SearchTerm)
End Class
Public Class SearchTermService
Inherits Service
Public Property Repository() As IRepository
Get
Return m_Repository
End Get
Set
m_Repository = Value
End Set
End Property
Private m_Repository As IRepository
Public Function [Get](request As SearchTermRequest) As SearchTermResponse
Return New SearchTermResponse() With { _
Key .Results = Me.Repository.SearchTermGet(request) _
}
End Function
End Class
Public Class CachedSearchTermService
Inherits Service
Public Property AppConfig() As AppConfig
Get
Return m_AppConfig
End Get
Set
m_AppConfig = Value
End Set
End Property
Private m_AppConfig As AppConfig
Public Function [Get](request As CachedSearchTermRequest) As Object
Dim cacheKey As String = request.CacheKey
Return Me.RequestContext.ToOptimizedResultUsingCache(
MyBase.Cache, cacheKey, New TimeSpan(0, Me.AppConfig.CacheTimeMinutes, 0),
Function()
Using service = Me.ResolveService(Of SearchTermService)()
Return service.[Get](request.TranslateTo(Of SearchTermRequest)()).TranslateTo(Of CachedSearchTermResponse)()
End Using
End Function
)
End Function
End Class
Related
Hy, Can I create an interface with a sub-project?
Public Interface IDraft
Property LienVue() As String
End Interface
Public Class C2018
Implements IDraft
Private m_LienVue As String
Sub New(Optional ByVal LienVue As String = "")
m_LienVue = LienVue
End Sub
Property LienVue() As String Implements IDraft.LienVue
Get
Return getlienvue()
End Get
Set(value As String)
m_LienVue = value
End Set
End Property
Private Function getlienvue() As String
Return "xxxxxxxx"
End Function
End Class
The c2018 class is in a sub-project of my main project. The goal is to add a reference to a different API from the main project
Not an experienced programmer, so probably not a hard question.
Developing a small application in VB.net in WPF.
I made 3 classes, EngineeringObject<==Inherits==PartOfInstallation<==Inherits==SensorActor
In the class SensorActor I'm trying to get a property of PartOfInstallation with the function MyBase.Name. But this goes directly to EngineeringObject. How do I solve this?
Public Class EngineeringObject
''Private declarations, alleen objecten die erven kunnen hieraan, of dmv van getters en setters
'Name of part
Private sName As String = "Naam"
'81346 Id's
Private sSystemId As String = "Functie" 'VentilationSystem, Pumpsystem
Private sLocationId As String = "Locatie" 'Room 0.0
Private sObjectId As String = "Object" 'Fan, Pump
'General
Private sPartNumber As String
Private sLinkToDatasheet As String
'Property's
Public Property Name() As String
Get
Return sName
End Get
Set(ByVal value As String)
sName = value
End Set
End Property
Public Property SystemId() As String
Get
Return sSystemId
End Get
Set(ByVal value As String)
sSystemId = value
End Set
End Property
Public Property PartNumber() As String
Get
Return sPartNumber
End Get
Set(ByVal value As String)
sPartNumber = value
End Set
End Property
Public Property LinkToDatasheet() As String
Get
Return sLinkToDatasheet
End Get
Set(ByVal value As String)
sLinkToDatasheet = value
End Set
End Property
Public Sub New()
End Sub
End Class
Public Class PartOfInstallation
Inherits EngineeringObject
'src: https://stackoverflow.com/questions/21308881/parent-creating-child-object
'src: https://stackoverflow.com/questions/16244548/how-to-create-a-list-of-parent-objects-where-each-parent-can-have-a-list-of-chil
Private lSensorActor As New List(Of SensorActor)
Public Function GetSensorActor()
Return Me.lSensorActor
End Function
Public Sub CreateSensorActor()
lSensorActor.Add(New SensorActor)
End Sub
End Class
Public Class SensorActor
Inherits PartOfInstallation
Dim sMyPartOfInstallation As String
Public Property MyPartOfInstallation As String
Get
Return sMyPartOfInstallation
End Get
Set(value As String)
sMyPartOfInstallation = MyBase.Name
End Set
End Property
End Class
If I understand it correctly, based on your comments, you want every SensorActor instantiated within a PartOfInstallation instance to get the same name as that instance.
If so, then just add a second constructor to your SensorActor class allowing you to pass a name for it as well:
Public Class SensorActor
Inherits PartOfInstallation
...your code...
Public Sub New() 'Empty constructor, for if/when you don't want to set the name immediately.
End Sub
Public Sub New(ByVal Name As String)
Me.Name = Name
End Sub
End Class
Now in your PartOfInstallation class you can do:
Public Sub CreateSensorActor()
lSensorActor.Add(New SensorActor(Me.Name)) 'Here, "Me" refers to the current PartOfInstallation instance.
End Sub
Alternatively you can make the SensorActor constructor take a PartOfInstallation instance instead, allowing you to copy any properties you like:
Public Class SensorActor
Inherits PartOfInstallation
...your code...
Public Sub New()
End Sub
Public Sub New(ByVal BasedOnPOI As PartOfInstallation)
Me.Name = BasedOnPOI.Name
End Sub
End Class
Thus making the code in the PartOfInstallation class:
Public Sub CreateSensorActor()
lSensorActor.Add(New SensorActor(Me))
End Sub
Read more about constructors: Object Lifetime: How Objects Are Created and Destroyed (Visual Basic) | Microsoft Docs
The result below, if there's room for improvement... always welcome.
SensorActor
Public Class SensorActor
Inherits PartOfInstallation
Dim sTemp As String
Public Overloads Property SystemId() As String
Get
Return Me.sSystemId
End Get
Set(ByVal value As String)
Me.sSystemId = sTemp + "." + value
End Set
End Property
Public Sub New(ByVal BasedOnPOI As PartOfInstallation)
sTemp = BasedOnPOI.SystemId
End Sub
End Class
PartOfInstallation
Public Class PartOfInstallation
Inherits EngineeringObject
'src: https://stackoverflow.com/questions/21308881/parent-creating-child-object
'src: https://stackoverflow.com/questions/16244548/how-to-create-a-list-of-parent-objects-where-each-parent-can-have-a-list-of-chil
Private lSensorActor As New List(Of SensorActor)
Public Function GetSensorActor()
Return Me.lSensorActor
End Function
Public Sub CreateSensorActor()
lSensorActor.Add(New SensorActor(Me))
End Sub
End Class
EngineeringObject
Public Class EngineeringObject
''Private declarations, alleen objecten die erven kunnen hieraan, of dmv van getters en setters
'Name of part
Private sName As String = "Naam"
'81346 Id's
Friend sSystemId As String = "Functie" 'VentilationSystem, Pumpsystem
Private sLocationId As String = "Locatie" 'Room 0.0
Private sObjectId As String = "Object" 'Fan, Pump
'General
Private sPartNumber As String
Private sLinkToDatasheet As String
'Property's
Public Property Name() As String
Get
Return sName
End Get
Set(ByVal value As String)
sName = value
End Set
End Property
Public Property SystemId() As String
Get
Return sSystemId
End Get
Set(ByVal value As String)
sSystemId = "=" + value
End Set
End Property
Public Property PartNumber() As String
Get
Return sPartNumber
End Get
Set(ByVal value As String)
sPartNumber = value
End Set
End Property
Public Property LinkToDatasheet() As String
Get
Return sLinkToDatasheet
End Get
Set(ByVal value As String)
sLinkToDatasheet = value
End Set
End Property
Public Sub New()
End Sub
End Class
I have an abstract class FileFolderBase with 2 classes deriving from it File and Folder, the class ProjectFolder is again deriving from the Folder class.
I have a WCF service, called BrowseService with 2 functions: GetRoot, which returns a list of ProjectFolder instances, and OpenFolder, which returns one Folder instance.
Here's the service code
iBrowseService.vb:
Imports System.ServiceModel
<ServiceContract()>
Public Interface IBrowseService
<OperationContract()>
Function OpenFolder(ByVal path As String) As Domain.Folder
<OperationContract()>
Function GetRoot(ByVal projectCodes As String) As List(Of Domain.ProjectFolder)
End Interface
BrowseService.svc.vb:
Imports System.ServiceModel.Activation
<AspNetCompatibilityRequirements(RequirementsMode:=AspNetCompatibilityRequirementsMode.Allowed)>
Public Class BrowseService
Implements IBrowseService
Public Function OpenFolder(ByVal path As String) As Domain.Folder Implements IBrowseService.OpenFolder
Dim service As MyService = New MyService()
Dim folder As Domain.Folder = service.OpenFolder(path)
Return folder
End Function
Public Function GetRoot(ByVal projectCodes As String) As List(Of Domain.ProjectFolder) Implements IBrowseService.GetRoot
Dim service As MyService = New MyService()
Dim folders As List(Of Domain.ProjectFolder)
If Not String.IsNullOrEmpty(projectCodes) Then
Dim codes As List(Of String) = projectCodes.Split(",").ToList
folders = service.GetRoot(codes)
Else
folders = service.GetRoot(Nothing)
End If
Return folders
End Function
End Class
Nothing overly complicated there, now the odd thing is, the GetRoot function works perfectly and returns a set of ProjectFolder instances when I call it. The OpenFolder function however, won't work at all. The WCF test client gives a 'This operation is not supported in the WCF Test Client cause it uses the type Folder' message and when I use the service in an application, I get a CommunicationsException, most inner exception message 'An existing connection was forcibly closed by the remote host', when invoking it.
I can't find any cause why ProjectFolder can be used, while Folder can't, especially cause ProjectFolder derives from Folder.
Here are the sources for the 4 type classes:
FileFolderBase
Imports System.Runtime.Serialization
<Serializable()>
<DataContract(IsReference:=True)>
Public MustInherit Class FileFolderBase
Implements IComparable(Of FileFolderBase)
#Region "Attributes"
Protected varPkey As Long
Protected varName As String
Protected varFullName As String
Protected varProjectFolder As ProjectFolder
Protected varLastModified As Date
#End Region
#Region "Properties"
<DataMember>
Public Property Pkey() As Long
Get
Return varPkey
End Get
Set(value As Long)
varPkey = value
End Set
End Property
<DataMember>
Public Property Name() As String
Get
Return varName
End Get
Set(ByVal value As String)
varName = value
End Set
End Property
<DataMember>
Public Property FullName() As String
Get
Return varFullName
End Get
Set(ByVal value As String)
varFullName = value
End Set
End Property
<DataMember>
Public Property ProjectFolder() As ProjectFolder
Get
Return varProjectFolder
End Get
Set(value As ProjectFolder)
varProjectFolder = value
End Set
End Property
<DataMember>
Public Property LastModified() As Date
Get
Return Me.varLastModified
End Get
Set(ByVal value As Date)
Me.varLastModified = value
End Set
End Property
#End Region
#Region "Methods"
'Equals function and = and <> operators
#End Region
#Region "CompareMethods"
'Comparison functions, used for sorting and stuff
#End Region
End Class
File
Imports System.Runtime.Serialization
<Serializable()>
<DataContract(IsReference:=True)>
Public Class File
Inherits FileFolderBase
#Region "Attributes"
Private varSize As Long
Private varComment As String
#End Region
#Region "Constructors"
Public Sub New()
Me.varPkey = 0
End Sub
Public Sub New(ByVal pkey As Long, ByVal name As String, ByVal fullName As String, ByVal lastModified As Date, ByVal size As Long, ByVal comment As String, ByVal project As ProjectFolder)
Me.varPkey = pkey
Me.varName = name
Me.varFullName = fullName
Me.varLastModified = lastModified
Me.varSize = size
Me.varComment = comment
Me.varProjectFolder = project
End Sub
#End Region
#Region "Properties"
<DataMember>
Public Property Size() As Long
Get
Return Me.varSize
End Get
Set(ByVal value As Long)
Me.varSize = value
End Set
End Property
<DataMember>
Public Property Comment() As String
Get
Return Me.varComment
End Get
Set(ByVal value As String)
Me.varComment = value
End Set
End Property
#End Region
#Region "Methods"
'Equals function and = and <> operators
#End Region
#Region "CompareMethods"
'Comparison functions, used for sorting and stuff
#End Region
End Class
Folder
Imports System.Runtime.Serialization
<Serializable()>
<DataContract(IsReference:=True)>
Public Class Folder
Inherits FileFolderBase
#Region "Attributes"
Protected varItems As List(Of FileFolderBase)
Protected varParent As Folder
#End Region
#Region "Constructors"
Public Sub New()
Me.varPkey = 0
End Sub
Public Sub New(ByVal pkey As Long, ByVal name As String, ByVal fullName As String, ByVal lastModified As Date, ByVal parent As Folder, ByVal project As ProjectFolder)
Me.varPkey = pkey
Me.varName = name
Me.varFullName = fullName
Me.varLastModified = lastModified
Me.varParent = parent
Me.varProjectFolder = project
varItems = New List(Of FileFolderBase)()
End Sub
#End Region
#Region "Properties"
<DataMember>
Public Property Items() As List(Of FileFolderBase)
Get
Return Me.varItems
End Get
Set(ByVal value As List(Of FileFolderBase))
Me.varItems = value
End Set
End Property
<DataMember>
Public Property Parent() As Folder
Get
Return varParent
End Get
Set(ByVal value As Folder)
Me.varParent = value
End Set
End Property
#End Region
#Region "Methods"
'Equals function and = and <> operators
#End Region
End Class
ProjectFolder
Imports System.Runtime.Serialization
<Serializable()>
<DataContract(IsReference:=True)>
Public Class ProjectFolder
Inherits Folder
#Region "Attributes"
Private varProject As Project
#End Region
#Region "Constructors"
Public Sub New()
MyBase.New()
End Sub
Public Sub New(ByVal project As Project, ByVal parent As Folder)
MyBase.New(0, project.Name, project.Name, Date.Now, parent, Nothing)
Me.varProject = project
End Sub
#End Region
#Region "Properties"
<DataMember>
Public Property Project() As Project
Get
Return Me.varProject
End Get
Set(ByVal value As Project)
Me.varProject = value
End Set
End Property
<DataMember>
Public Overloads Property Items() As List(Of FileFolderBase)
Get
Return Me.varItems
End Get
Set(ByVal value As List(Of FileFolderBase))
Me.varItems = value
End Set
End Property
<DataMember>
Public Overloads Property ProjectFolder() As ProjectFolder
Get
Return Me
End Get
Set(value As ProjectFolder)
End Set
End Property
#End Region
#Region "Methods"
'Equals function and = and <> operators
#End Region
End Class
Please help, cause I have no clue how this can be possible.
I’m working on some partial classes but I can’t figure out how to do it.
This is my classes:
Partial Public Class Form
Private InfoField() As Info
Private FormgroupField() As FormGroup
Private tittle As String
Public Property Info() As Info()
Get
Return Me. InfoField
End Get
Set
Me. InfoField = value
End Set
End Property
Public Property FormGroup() As FormGroup()
Get
Return Me.GromGroupField
End Get
Set
Me.FormGroupField = value
End Set
End Property
Public Property tittle() As String
Get
Return Me.tittleField
End Get
Set
Me.tittleField = value
End Set
End Property
End class
Partial Public Class Info
Private ChangeFormField() As ChangeForm
Private formYearField() As FormYea
Private idField As String
Public Property ChangeForm() As ChangeForm()
Get
Return Me.changeFormField
End Get
Set
Me.changeFormField = value
End Set
End Property
Public Property FormYear() As FormYear()
Get
Return Me.formYearField
End Get
Set
Me.formYearField = value
End Set
End Property
Public Property id() As String
Get
Return Me.idField
End Get
Set
Me.idField = value
End Set
End Property
End Class
Partial Public Class ChangeForm
Private idField As String
Private valueField As String
<properties goes here>
End Class
Partial Public Class FormYear
Private idField As String
Private valueField As String
<properties goes here>
End Class
And for the class FormGroup the organization is the same.
I want to build partial classes to extend these classes, so when I use all this classes in another project I only have to deal with (see) the topmost class (Form) and not the other classes (like Info and FormGroup. This is what I like to do:
Partial Public Class Form
Public Sub Init()
Me.Info = New Info
Me.FormGroup = New FormGroup
Me.Info.Init()
Me.FormGroup.Init()
End Sub
End Class
Partial Public Class Info
Public Sub Init()
Me.FormYear = New FormYear
Me.ChangeForm = New ChangeForm
Me.changeForm.Init()
End Sub
But I can’t write
Me.Info = New Info
Me.FormGroup = New FormGroup
because it is arrays with classes. How can I do it in my Form and Info class?
Thanks in advance.
You must first create an array, then loop over the array and assign each element. Also, unless you have a good, strong reason, do this in the constructor rather than a separate init method.
Public Class Form
Public Sub New()
'In VB, you give the max index, not the length.
'I prefer listing this as (whatever I want for length) - 1
Me.Info = New Info(size - 1) {}
For i = 0 to size - 1
Me.Info(i) = New Info()
Next
'similarly for other fields
End Sub
End Class
Alternatively, if you find yourself with a lot of array fields, and they all have default constructors, you could create a FixedCollection class that would encapsulate the repetitive initialization code.
Public Class FixedCollection(Of T As New)
Inherits Collection(Of T)
Public Sub New(ByVal size As Integer)
MyBase.New(New T(size - 1) {})
For i = 0 to size - 1
Me.Items(i) = New T()
Next
End Sub
'alternate constructors if you need additional initialization
'beyond construction of each element
Public Sub New(ByVal size As Integer, ByVal creator As Func(Of T))
MyBase.New(New T(size - 1) {})
If creator Is Nothing Then Throw New ArgumentNullException("creator")
For i = 0 to size - 1
Me.Items(i) = creator()
Next
End Sub
'this overload allows you to include the index in the collection
'if it would matter to creation
Public Sub New(ByVal size As Integer, ByVal creator As Func(Of Integer, T))
MyBase.New(New T(size - 1) {})
If creator Is Nothing Then Throw New ArgumentNullException("creator")
For i = 0 to size - 1
Me.Items(i) = creator(i)
Next
End Sub
'other collection overrides as needed here
End Class
EDIT: Added constructor overloads for when an element constructor is not enough.
If you only use the constructors with a creator parameter, you could remove the New constraint on T.
Use the overloads as follows:
Public Class Form
Private InfoField As New FixedCollection(Of Info)(10,
Function()
Dim ret As New Info()
ret.Init()
End Function)
End Class
Based on your comments, it seems like the Init methods are an unfortunate necessity. If possible, I would recommend that you find a way to get the generated constructor changed to call this method (defined in the generated code using partial methods) for you rather than forcing you to call it yourself.
You can initialize an Array of a Class like this:
Public FieldTypes As FieldTypeInfo() =
{
New FieldTypeInfo("Byte", 1),
New FieldTypeInfo("Int16", 2),
New FieldTypeInfo("Int32", 3),
New FieldTypeInfo("Integer", 3),
New FieldTypeInfo("Int64", 4),
New FieldTypeInfo("UInt16", 5),
New FieldTypeInfo("UInt32", 6),
New FieldTypeInfo("UInteger", 6),
New FieldTypeInfo("UInt64", 7)
}
I have a class that add extra information about a column for linq2sql (see code below)
right now, I have to explicitly tell what column I want that info on, how would you put that code with a loop on every column in every table from a dbml file?
I was doing my test on a very very small DB, now I have to implement it on a much more bigger database and I really don't want to do it manually for every tables/columns
it will take hours.
how it's being used:
Partial Class Contact ''contact is a table inside a dbml file.
Private _ContactIDColumn As ExtraColumnInfo
Private _ContactNameColumn As ExtraColumnInfo
Private _ContactEmailColumn As ExtraColumnInfo
Private _ContactTypeIDColumn As ExtraColumnInfo
Private Sub OnCreated()
initColumnInfo()
End Sub
Private Sub initColumnInfo()
Dim prop As PropertyInfo
prop = Me.GetType.GetProperty("ContactID")
_ContactIDColumn = New ExtraColumnInfo(DirectCast(prop.GetCustomAttributes(GetType(ColumnAttribute), False)(0), ColumnAttribute))
prop = Me.GetType.GetProperty("ContactName")
_ContactNameColumn = New ExtraColumnInfo(DirectCast(prop.GetCustomAttributes(GetType(ColumnAttribute), False)(0), ColumnAttribute))
prop = Me.GetType.GetProperty("ContactEmail")
_ContactEmailColumn = New ExtraColumnInfo(DirectCast(prop.GetCustomAttributes(GetType(ColumnAttribute), False)(0), ColumnAttribute))
prop = Me.GetType.GetProperty("ContactTypeID")
_ContactTypeIDColumn = New ExtraColumnInfo(DirectCast(prop.GetCustomAttributes(GetType(ColumnAttribute), False)(0), ColumnAttribute))
prop = Nothing
End Sub
Public ReadOnly Property ContactIDColumn() As ExtraColumnInfo
Get
Return _ContactIDColumn
End Get
End Property
Public ReadOnly Property ContactNameColumn() As ExtraColumnInfo
Get
Return _ContactNameColumn
End Get
End Property
Public ReadOnly Property ContactEmailColumn() As ExtraColumnInfo
Get
Return _ContactEmailColumn
End Get
End Property
Public ReadOnly Property ContactTypeIDColumn() As ExtraColumnInfo
Get
Return _ContactTypeIDColumn
End Get
End Property
End Class
what is ExtraColumnInfo:
Public Class ExtraColumnInfo
Private _column As ColumnAttribute
Private _MaxLength As Nullable(Of Integer)
Private _Precision As Nullable(Of Integer)
Private _Scale As Nullable(Of Integer)
Private _IsIdentity As Boolean
Private _IsUniqueIdentifier As Boolean
Sub New(ByVal column As ColumnAttribute)
Dim match As Match
_column = column
Match = Regex.Match(DBType, "^.*\((\d+)\).*$", RegexOptions.Compiled Or RegexOptions.IgnoreCase)
_MaxLength = If(match.Success, Convert.ToInt32(match.Groups(1).Value), Nothing)
match = Regex.Match(DBType, "^.*\((\d+),(\d+)\).*$", RegexOptions.Compiled Or RegexOptions.IgnoreCase)
_Precision = If(Match.Success, Convert.ToInt32(Match.Groups(1).Value), Nothing)
match = Regex.Match(DBType, "^.*\((\d+),(\d+)\).*$", RegexOptions.Compiled Or RegexOptions.IgnoreCase)
_Scale = If(match.Success, Convert.ToInt32(match.Groups(2).Value), Nothing)
match = Regex.Match(DBType, "^.*\bIDENTITY\b.*$", RegexOptions.Compiled Or RegexOptions.IgnoreCase)
_IsIdentity = match.Success
match = Regex.Match(DBType, "^.*\bUniqueIdentifier\b.*$", RegexOptions.Compiled Or RegexOptions.IgnoreCase)
_IsUniqueIdentifier = match.Success
End Sub
'others available information
'AutoSync
'Expression
'IsVersion
'Name
'Storage
'TypeId
'UpdateCheck
'maybe more
Public ReadOnly Property CanBeNull() As Boolean
Get
Return _column.CanBeNull
End Get
End Property
Public ReadOnly Property IsDbGenerated() As Boolean
Get
Return _column.IsDbGenerated
End Get
End Property
Public ReadOnly Property IsPrimaryKey() As Boolean
Get
Return _column.IsPrimaryKey
End Get
End Property
Public ReadOnly Property DBType() As String
Get
Return _column.DbType
End Get
End Property
Public ReadOnly Property MaxLength() As System.Nullable(Of Integer)
Get
Return _MaxLength
End Get
End Property
Public ReadOnly Property Precision() As System.Nullable(Of Integer)
Get
Return _Precision
End Get
End Property
Public ReadOnly Property Scale() As System.Nullable(Of Integer)
Get
Return _Scale
End Get
End Property
Public ReadOnly Property IsIdentity() As Boolean
Get
Return _IsIdentity
End Get
End Property
Public ReadOnly Property IsUniqueIdentifier() As Boolean
Get
Return _IsUniqueIdentifier
End Get
End Property
End Class
The only thing that comes to mind, and it's a far from optimal solution, would be to include this functionality into a base class and declare partials for all of the generated classes so that they inherit from your base class.
I'm sure there are also other ways to do this, but none that I know of that are native to the designer.
I took the T4 template and modified it a little to automatically put inside the generated class the column information,
in the import I added
Imports System.Reflection
Imports System.Text.RegularExpressions
at line 228, added these line:
<# foreach(Column column in class1.Columns) {#>
Private _<#=column.Member#>Column As ExtraColumnInfo
Public ReadOnly Property <#=column.Member#>Column() As ExtraColumnInfo
Get
Return _<#=column.Member#>Column
End Get
End Property
<# }#>
at line 298 I added
Dim prop As PropertyInfo
<# foreach(Column column in class1.Columns) {#>
prop = Me.GetType.GetProperty("<#=column.Member#>")
_<#=column.Member#>Column = New ExtraColumnInfo(DirectCast(prop.GetCustomAttributes(GetType(ColumnAttribute), False)(0), ColumnAttribute))
<# }#>
at the end of the file, I added my own class