I’m not too familiar with generics (concept or the syntax) in general (short of using them in collections and what not), but I was wondering if the following is the best way of accomplishing what I want. Actually, I’m not entirely positive generics will solve my problem in this case.
I’ve modelled and mapped a few dozen objects in NHibernate, and need some sort of universal class for my CRUD operations instead of creating a seperate persister class for each type.. such as
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 different types. I know this isn’t the best way of doing this (or if it’ll even work) but I’m hoping someone can steer me in the right direction.
The key issue here is: What does session.Update take as a parameter? If session.Update allows a generic object, then I’d just use that:
That’ll flow the generic type T through to session.Update.
If session.Update just takes an object, then just pass in the object; no need to CType it. Additionally, if objectType (string) is just the type name of the current object, you’d be better off using someObject.GetType() in the first place.