开发者

Injecting a service into an implementation of NHibernate's IUserType using Autofac

I'm using NHibernate to map the following class to an Oracle database in my ASP.NET MVC application:

public class User
{
    // Needs to be encrypted/decrypted when persisting
    public virtual string Question { get; set; }
}

Following several examples tha开发者_如何学JAVAt I've found, I would like to use an implementation of NHibernate's IUserType to transform my data when it is persisted to or retrieved from my database example.

Since I have already written an EncryptionService class to handle data encryption in other parts of my application, I would like to inject it into my user type. Is there a way to perform constructor injection on an IUserType using Autofac?


NHibernate is responsible for creating instances of IUserType implementations. If you want this configurable, you could query the service from the AutoFac container (obviously this isn't dependency injection, more of service locator). Or you could implement IConfigurable, which allows you to take in a Dictionary of parameters. One of those parameters could be a class name.


Use the Autofac's BytecodeProvider which you can find in Autofac contrib.

http://chadly.net/post/2009/05/28/Dependency-Injection-with-NHibernate-and-Autofac.aspx

http://code.google.com/p/autofac/downloads/list

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜