patternMinor
Creating a lightweight strongly-typed parameter infrastructure
Viewed 0 times
creatinglightweightstronglyinfrastructuretypedparameter
Problem
I'm looking for some feedback on some code, designed to allow me to strongly-type parameters, which get passed to functions.
I'm only really looking for comments on making my parameters more strongly typed, as well as the existence of the factory method. The implementation of the
At the minute the code base has a number of places where many pieces of data are represented as
This can be awkward, so I am trying to consider a way in which this could be improved.
I have written the below code, as an attempt to address this problem, and allow for the parameters I pass around functions, to be more strongly typed.
Interface used to represent the values
Base class to provide functionality around construction (and maybe properties in future such as
This will then allow for the definition possible parameters to methods, for example:
Which could then be used like this
Some example ways BEFORE using this functionality (functions grouped into one class for brevity)
```
Public Class UserFinder
Public Function FindUserByEmailAddress(email
I'm only really looking for comments on making my parameters more strongly typed, as well as the existence of the factory method. The implementation of the
FindUser method is not relevant.At the minute the code base has a number of places where many pieces of data are represented as
strings, however in actual fact they may be EmailAddresses, Usernames etc.This can be awkward, so I am trying to consider a way in which this could be improved.
I have written the below code, as an attempt to address this problem, and allow for the parameters I pass around functions, to be more strongly typed.
Interface used to represent the values
Public Interface IStronglyTypedValue(Of T)
Property Value As T
// We could add IsValid, IsCorrectFormat, etc methods in future
End InterfaceBase class to provide functionality around construction (and maybe properties in future such as
IsValid, or ToString()).Public MustInherit Class StronglyTypedValueBase(Of T, TTypeOfThing As {New, IStronglyTypedValue(Of T)})
Implements IStronglyTypedValue(Of T)
Public Property Value As T Implements IStronglyTypedValue(Of T).Value
Public Shared Function Make(data As T) As TTypeOfThing
Return New TTypeOfThing() With {.Value = data}
End Function
End ClassThis will then allow for the definition possible parameters to methods, for example:
Public Class EmailAddress
Inherits StronglyTypedValueBase(Of String, EmailAddress)
End Class
Public Class UserName
Inherits StronglyTypedValueBase(Of String, UserName)
End ClassWhich could then be used like this
EmailAddress.Make("my@email.com")
UserName.Make("user123")Some example ways BEFORE using this functionality (functions grouped into one class for brevity)
```
Public Class UserFinder
Public Function FindUserByEmailAddress(email
Solution
Stop. This way madness lies.
It's great that you want to strongly type these things. They should be! The problem is in your approach. This isn't what generics/interfaces are for. This is what Structures and conversion operators are for.
Which lets you use it as a strongly typed object, without much fuss and using VB.Net idioms that any VB dev will be familiar with.
It's great that you want to strongly type these things. They should be! The problem is in your approach. This isn't what generics/interfaces are for. This is what Structures and conversion operators are for.
Public Structure EmailAddress
Private email As String
Public Sub New(ByVal s As String)
' validation code here
email = s
End Sub
Public Shared Narrowing Operator CType(ByVal s As String) As EmailAddress
Return New EmailAddress(s)
End Operator
Public OverRides Function ToString() As String
Return email
End Function
End StructureWhich lets you use it as a strongly typed object, without much fuss and using VB.Net idioms that any VB dev will be familiar with.
Dim email = new EmailAddress("JohnDoe@domain.net")
email = CType("JohnDoe@domain.net", EmailAddress)
Dim value As String = email.ToString()Code Snippets
Public Structure EmailAddress
Private email As String
Public Sub New(ByVal s As String)
' validation code here
email = s
End Sub
Public Shared Narrowing Operator CType(ByVal s As String) As EmailAddress
Return New EmailAddress(s)
End Operator
Public OverRides Function ToString() As String
Return email
End Function
End StructureDim email = new EmailAddress("JohnDoe@domain.net")
email = CType("JohnDoe@domain.net", EmailAddress)
Dim value As String = email.ToString()Context
StackExchange Code Review Q#118144, answer score: 2
Revisions (0)
No revisions yet.