VB.NET programming patterns for late binding of parsers for varying dataset file formats

synopsis

It’s been a long time since writing VB.NET code and since then .NET CORE has been created and grown. So quite some time. And programming practices and patterns have evolved without my keeping up, I’m sure.

So I’m feeling a bit ignorant. I believe I need late binding. But I’d like to know if there is a preferred pattern that achieves my goals. Or if there is something very wrong with the approach I finally chose.

details

I have decades of saved monthly datasets from NOAA and elsewhere. While the parameter values that I care about have persisted throughout almost a century, the formats have evolved over the years. (For example, there was a time when the information was manually entered from old sheets into ASCII, a time when FORTRAN formatting was used, a time when web pages existed, etc.)

I have a folder with many of these monthly datasets stored under Windows. And I have written parser functions for each kind of formatting in my possession. But I also need to anticipate the addition of still other formats, past, present, and future, which are laid out differently and yet still contain the information I’d like to process. So I expect to add more parsers as time passes.

As I uncover new formats and write and add new parsers to the project, I’d like to simply add another .vb file without modifying existing, working code. If anything is to break, it should only be the new parser class I’m writing.

I feel this means using some form of late binding, with a managing function that is able to track down the appropriate parser.

The approach I took (and works) is to create a MustInherit base class that imposes an interface, which allows over-loading. Derivative classes will be written, one for each type of formatting, implementing the interface regardless of the original formatting. Each derivative class will feature a Shared function which is its relevant parser and is able to indicate success or failure by either returning Nothing or else a fully constructed class (with interface.)

The MustInherit base class, ClimateMonthlyDataset, also provides this Shared function to identify and apply the appropriate parser to the indicated file (a “reader” is passed to it):

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Public Shared Function CreateClimateDataset(FileContents As IEnumerable(Of String)) As ClimateMonthlyDataset
Dim PossibleClimateDataDactoryClasses() As Type = GetType(MyApplicationContext).Assembly.GetTypes()
For Each q As Type In PossibleClimateDataDactoryClasses
If q.IsClass AndAlso (q.Attributes And TypeAttributes.Abstract) = 0 AndAlso q.GetInterface("IClimateDataset") IsNot Nothing Then
Dim TryMonthlyClimateDataFactory As MethodInfo = q.GetMethod("CreateNewDataset")
If TryMonthlyClimateDataFactory IsNot Nothing AndAlso (TryMonthlyClimateDataFactory.Attributes And MethodAttributes.Static) <> 0 Then
Dim MonthlyClimateData As Object = TryMonthlyClimateDataFactory.Invoke(Nothing, New Object() {FileContents})
If MonthlyClimateData IsNot Nothing Then
Return CType(MonthlyClimateData, ClimateMonthlyDataset)
End If
End If
End If
Next
Return Nothing
End Function
</code>
<code>Public Shared Function CreateClimateDataset(FileContents As IEnumerable(Of String)) As ClimateMonthlyDataset Dim PossibleClimateDataDactoryClasses() As Type = GetType(MyApplicationContext).Assembly.GetTypes() For Each q As Type In PossibleClimateDataDactoryClasses If q.IsClass AndAlso (q.Attributes And TypeAttributes.Abstract) = 0 AndAlso q.GetInterface("IClimateDataset") IsNot Nothing Then Dim TryMonthlyClimateDataFactory As MethodInfo = q.GetMethod("CreateNewDataset") If TryMonthlyClimateDataFactory IsNot Nothing AndAlso (TryMonthlyClimateDataFactory.Attributes And MethodAttributes.Static) <> 0 Then Dim MonthlyClimateData As Object = TryMonthlyClimateDataFactory.Invoke(Nothing, New Object() {FileContents}) If MonthlyClimateData IsNot Nothing Then Return CType(MonthlyClimateData, ClimateMonthlyDataset) End If End If End If Next Return Nothing End Function </code>
Public Shared Function CreateClimateDataset(FileContents As IEnumerable(Of String)) As ClimateMonthlyDataset
    Dim PossibleClimateDataDactoryClasses() As Type = GetType(MyApplicationContext).Assembly.GetTypes()
    For Each q As Type In PossibleClimateDataDactoryClasses
        If q.IsClass AndAlso (q.Attributes And TypeAttributes.Abstract) = 0 AndAlso q.GetInterface("IClimateDataset") IsNot Nothing Then
            Dim TryMonthlyClimateDataFactory As MethodInfo = q.GetMethod("CreateNewDataset")
            If TryMonthlyClimateDataFactory IsNot Nothing AndAlso (TryMonthlyClimateDataFactory.Attributes And MethodAttributes.Static) <> 0 Then
                Dim MonthlyClimateData As Object = TryMonthlyClimateDataFactory.Invoke(Nothing, New Object() {FileContents})
                If MonthlyClimateData IsNot Nothing Then
                    Return CType(MonthlyClimateData, ClimateMonthlyDataset)
                End If
            End If
        End If
    Next
    Return Nothing
End Function

I also considered the idea of using custom attributes to decorate and use reflection on those attributes, instead. But the interface is a requirement. So that already exists. It seemed to me that adding an attribute, as well, just unnecessarily duplicates knowledge and adds still more code to maintain.

I didn’t see added value there.

summary

Is there a better approach that I missed considering?

Is there something terribly wrong with the approach I did take?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật