Create an identifier via COM interop

For now, we have an irreplaceable ball of code that offers an interface to a third party application. The third party application has a COM assembly that MUST be used to create new entries. This process includes two steps: generate a new object (mainly an identifier) ​​and update this object with new field values.
Because COM interop is so slow, we only use it to generate the ID (and related objects) in the database. The actual update is done using a regular SQL query.

What I'm trying to figure out is if NHibernate can be used to do some heavy lifting for us without going through the COM assembly. Here's the code to save something to the database as I imagine it:

using(var s = sessionFactory.OpenSession())
using(var t = s.BeginTransaction())
{
    MyEntity entity = new MyEntity();
    s.Save(entity);
    t.Commit();
}

      

Normal NH code I would say. Now this is becoming a challenge. I think I should provide my own implementation of NHibernate.Id.IIdentifierGenerator that calls the COM assembly in the Generate method. It's not a problem. The problem is that the COM assembly requires initialization, which takes a little time. For some reason, it also doesn't like multiple instances in the same process.
I would like to know if there is a way to correctly access the external service in the generator code.

I can use any technique I want, so if it contains something like an IoC container, that's not a problem. What I'm looking for is where exactly to hook up my code so that I can access the things I need in my generator without resorting to using singlets or other nasty things.

+2


a source to share


1 answer


Assuming you want a single COM component instance for all of your entities, this could be a service locator (implemented with any IoC container).

Your (trivial) implementation IIdentifierGenerator

should call the locator in its constructor to get the component instance and call the appropriate method for the component on Generate

.



One thing to consider: Is the COM thread thread safe? If not, you must sync the calls Generate

.

Using a generator is simple, just use the fully qualified name as the generator class.

0


a source







All Articles