Davita
Davita

Reputation: 9114

(Fluent) NHibernate initialize IUserType using Ninject?

I need to serialize one field to JSON and store it in database. For this, I implement IUserType interface. However, due to testing purposes, I'd like to pass a serializer interface in a constructor parameter. Ideally, I want to achieve that by telling NH to create an instance of my IUserType implementation using Ninject. Is this possible at all?

Upvotes: 0

Views: 141

Answers (1)

Firo
Firo

Reputation: 30813

Cfg.Environment.BytecodeProvider.ObjectsFactory is responsible for creating objects used by NHibernate. You can implement IBytecodeProvider to inject your own for example:

class MyBytecodeProvider : NHibernate.Bytecode.Lightweight.BytecodeProviderImpl, IObjectsFactory
{
    public override IObjectsFactory ObjectsFactory
    {
        get { return this; }
    }

    #region IObjectsFactory implementation
    public object CreateInstance(System.Type type)
    {
        // TODO:
    }

    public object CreateInstance(System.Type type, bool nonPublic)
    {
        // TODO:
    }

    public object CreateInstance(System.Type type, params object[] ctorArgs)
    {
        // TODO:
    }
    #endregion
}

Upvotes: 1

Related Questions