开发者

Default Constructors in Structure Map

开发者 https://www.devze.com 2023-01-20 01:49 出处:网络
I have this code for on ASP.NET MVC website: x.For<AccountController>().TheDefault.Is.ConstructedBy(() => new AccountController());

I have this code for on ASP.NET MVC website:

x.For<AccountController>().TheDefault.Is.ConstructedBy(() => new AccountController());

This code throws a warning which seems quite self explanatory but for some reason when I use the "Use" method it doesn't seem to work. I know I am doing something wrong and would appreciate some help.

开发者_C百科

The warning is:

Warning 1 'StructureMap.Configuration.DSL.Expressions.CreatePluginFamilyExpression.TheDefault' is obsolete: '"Prefer the Use() methods"

Thank You.


The direct equivalent of your existing code, but with the new API, is:

For<AccountController>().Use(() => new AccountController());

This will create a new instance of AccountController any time an instance of AccountController is requested.

Warning: If you pass an object instance (as RPM1984 suggests), instead of a lambda, to the Use() method you will get a singleton - the same instance will be returned every time an AccountController is requested. This is very different behavior from what your code was doing previously with the Is.ConstructedBy() syntax.


If you look at the XMLDoc for the third overload on the "Use" method, it says this:

Shorthand to say TheDefault.IsThis(@object)

Therefore, simply do it like this:

For<AccountController>().Use(() => new AccountController());

Although i'm not sure what the point of this is (injecting a concrete when something requests a concrete), unless this is just an example, and you're really doing something else.

Normally for the MVC Controllers, you create a ControllerFactory - i haven't seen a need to "explicitly" inject a concrete Controller.

0

精彩评论

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

关注公众号