开发者

How to manage multiple tables with the same structure (redux)

I found this question, which is similar to a problem that I would like to solve:

How to manage multiple tables with the same structure

However, due to the craptastical nature of VB, the solution doesn't really work. It specifically doesn't work because VB.NET requires the implementation of each method/property in the interface to be explicitly declared.

As for the problem that I'm really trying to sol开发者_JAVA技巧ve, here it is:

  • I have many lookup/domain tables in the database that all have the same structure
  • The items in these tables are typically used for drop downs in the interface
  • I would like to avoid a bunch of boilerplate repository methods to retrieve the contents of these tables (one method per table really sucks when you have 40 tables)
  • I am not using the One True Lookup Table anti-pattern and this is not an option

Does anyone have another solution for this that work work in VB?


Generic Repository should work in this case. There are many available online or you can write a simpler one for just the lookup tables.


Here is the code that we ended up using:

Public Function GetDomainTableList(tableName As String) As IEnumerable(Of Object)

    Dim table = CType(GetType(FECEntities).GetProperty(tableName).GetValue(DB, Nothing), IEnumerable(Of Object))

    Dim dt = From r In table
             Select r

    Return dt.ToList()

End Function

I had originally thought that this wouldn't work for us since I was trying to project each object returned into a DomainTableItem class that I had written. But then I realized that the SelectList constructor didn't really care about the type of object that it takes in. You just pass in a String containing the property name and it uses reflection to pull out the value.

So everything works out peachy this way and I avoided writing one method per domain/lookup table.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜