开发者

Microsoft Unity, parameters in constructor

开发者 https://www.devze.com 2022-12-26 17:24 出处:网络
I am using Unity with MVC and NHibernate. Unfortunately, our UnitOfWork resides in a different .dll and it doesn\'t have a default empty .ctor.

I am using Unity with MVC and NHibernate. Unfortunately, our UnitOfWork resides in a different .dll and it doesn't have a default empty .ctor. This is what I do to register NHibernate:

var connectionString = ConfigurationManager.ConnectionStrings
    ["jobManagerConnection"].ConnectionString;
var assemblyMap = ConfigurationManager.AppSettings["assemblyMap"];
container.RegisterType<IUnitOfWork, UnitOfWork>(
    new ContainerControlledLifetimeManager());

In my WebController I have this:

/// <summary>Gets or sets UnitOfWork.</summary>
[Dependency]
public IUnitOfWork UnitOfWork { get; set; }

The problem is that the constructor of UnitOfWork expects 2 mandatory strings. How I can setup the RegisterType for this Interface in order to pass the two开发者_如何学Python parameters retreived from the web.config? Is it possible?


Something like this should do it:

var connectionString = ConfigurationManager.ConnectionStrings
    ["jobManagerConnection"].ConnectionString;
var assemblyMap = ConfigurationManager.AppSettings["assemblyMap"];

container.RegisterType<IUnitOfWork, UnitOfWork>(
    new InjectionConstructor(connectionString, assemblyMap),
    new ContainerControlledLifetimeManager());


Easier than I though:

        var connectionString = ConfigurationManager.ConnectionStrings["jobManagerConnection"].ConnectionString;
        var assemblyMap = ConfigurationManager.AppSettings["assemblyMap"];
        container
            .RegisterType<IUnitOfWork, UnitOfWork>(new ContainerControlledLifetimeManager())
            .Configure<InjectedMembers>()
            .ConfigureInjectionFor<UnitOfWork>(new InjectionConstructor(connectionString, assemblyMap));
0

精彩评论

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