Passing Func<T> to controller constructure when using Unity IoC with MVC, advantages?

Posted by user1361315 on Stack Overflow See other posts from Stack Overflow or by user1361315
Published on 2013-11-01T19:51:29Z Indexed on 2013/11/01 21:54 UTC
Read the original article Hit count: 141

I was looking at a sample of how to setup Unity IoC with MVC, and noticed someone who recommended the approach of having the parameters of Func. I believe the advantage is this is kind of like lazy loading the service, if it never gets called it will never get executed and not consume any resources.

 private readonly Func<IUserService> _userService;

 public CourseController(Func<IUserService> userService)
 {
         this._userService = userService;
 }

Versus a parameter without a Func:

 private readonly IUserService _userService;

 public CourseController(IUserService userService)
 {
         this._userService = userService;
 }

Can someone explain to me the differences, is it really more effecient?

© Stack Overflow or respective owner

Related posts about c#

Related posts about asp.net-mvc