Common Functions in VB.NET

I am not very familiar with generics (concept or syntax) in general (I do not use them in collections, but what is not), but I was wondering if the following way is the best way to accomplish what I want. In fact, I'm not entirely sure that generics will solve my problem in this case.

I have modeled and matched several dozen objects in NHibernate and need some kind of universal class for my CRUD operations instead of creating a separate persister class for each type .. for example

Sub Update(someObject as Object, objectType as String) Dim session As ISession = NHibernateHelper.OpenSession Dim transaction As ITransaction = session.BeginTransaction session.Update(ctype(someObject, objectType)) transaction.Commit() End Sub 

where someObject can be of different types. I know that this is not the best way to do this (or even if it will work), but I hope that someone will be able to direct me in the right direction.

+1
source share
1 answer

The key issue here is: what does session.Update do as a parameter? If session.Update resolves a shared object, I would just use this:

  Sub Update(Of T)(ByVal someObject As T) Dim session As ISession = NHibernateHelper.OpenSession Dim transaction As ITransaction = session.BeginTransaction session.Update(someObject) transaction.Commit() End Sub 

This will pass the generic type T to session.Update.

If session.Update just takes an object, then just pass the object; no need to expose it. Also, if objectType (string) is just the type name of the current object, you'd better use someObject.GetType () in the first place.

+2
source

All Articles