views:

52

answers:

2

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.

The warning is:

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

Thank You.

+1  A: 

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.

RPM1984
+1 for "Although i'm not sure what the point of this is"
Steve Michelotti
Um, downvoter - care to explain?
RPM1984
Although I wasn't the downvoter, I considered it because your answer is wrong and will have a dramatically different result than what the original code did. Since it is marked as the answer, you should probably edit it to use a lambda, as in my answer.
Joshua Flanagan
Just realized I have edit rights, so I took the liberty of fixing your answer, since it is marked as correct.
Joshua Flanagan
Thanks. Yes, you're right, what i did was wrong. I didn't consider singletons, as a) i never use them and b) his original code used a singleton anyway, c) thought scoping was 'out of scope' (no pun intended) for this question. Nonetheless, you were more correct (which is why i upvoted you). Cheers.
RPM1984
+4  A: 

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.

Joshua Flanagan
+1 - good point on the singleton. Didn't even think of that! I always explicity use `.Singleton()` or `.HybridHttpOrThreadLocalScoped()` before the `.Use` statement. Much safer that way.
RPM1984
Thank you for the warning, I actually ran into this issue.
Lukasz