ASP.NetMVC3とEntityFramework4.1を使用して構築したアプリケーションを使用して稼働させようとしています。依存性注入には、Unity2.0IoCを使用しました。このチュートリアルは、UnityIoChttp : //weblogs.asp.net/shijuvarghese/archive/2011/01/21/dependency-injection-in-asp-net-mvc-3-using-dependencyresolverのセットアップに役立つガイドとして使用しました。 -および-controlleractivator.aspx
今日、コードをチェックして直前のバグ修正を確認してい ましたが、 Global.asaxファイルでApplication_Start()メソッドに出くわしました。こんな感じ
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
IUnityContainer container = new UnityContainer();
container.RegisterType<IControllerActivator, CustomControllerActivator>(new HttpContextLifetimeManager<IControllerActivator>());
//container.RegisterType<IUnitOfWork, UnitOfWork>(new ContainerControlledLifetimeManager());
container.RegisterType<IUnitOfWork, UnitOfWork>(new HttpContextLifetimeManager<IUnitOfWork>());
container.RegisterType<IListService, ListService>(new HttpContextLifetimeManager<IListService>());
container.RegisterType<IShiftService, ShiftService>(new HttpContextLifetimeManager<IShiftService>());
}
アプリケーションは正常に動作していますが、コード行が欠落していることに気付きました
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
行の後に行く
IUnityContainer container = new UnityContainer();
私が言っているように、私のアプリケーションは、このコード行がなくても(とにかくローカルで)正常に動作していました。それ以来、私はそれを追加しました、そして、アプリケーションは再び期待通りに働いています。
ただし、UnityIoCが正しくセットアップされていないのではないかと少し心配しています。この追加のコード行がなくても、アプリケーションが機能したのはなぜですか?そして、私もそれを追加する必要がありますか?
ご協力いただきありがとうございます。
アップデート
以下は、私のコントローラーの1つのコンストラクターを示しています
public class AccountController : Controller
{
private IAccountService _accountService;
private IUserService _userService;
private IFormsAuthenticationService _formsService;
private INotificationService _notifyService;
private ILoggingService _logService;
public AccountController(ILoggingService logService, IAccountService accountService, IUserService userService, IFormsAuthenticationService formsService, INotificationService notifyService)
{
_accountService = accountService;
_userService = userService;
_formsService = formsService;
_notifyService = notifyService;
_logService = logService;
}