ioc-container

Why is MVC4 using the Service Locator Anti-Pattern?

不问归期 提交于 2019-12-18 10:15:22
问题 After reading "Dependency Injection in .NET" by Mark Seemann I stay away from the Service Locator which is an anti-pattern. Upon reading the release notes on MVC 4 I see: Improved Inversion of Control (IoC) via DependencyResolver: Web API now uses the service locator pattern implemented by MVC’s dependency resolver to obtain instances for many different facilities. Thus I'm left curious and confused why Microsoft would use a service locator in 2012. 回答1: That's an implementation detail that

using auth in laravel service provider

怎甘沉沦 提交于 2019-12-18 07:13:10
问题 whenever i tried to use \Auth::User() i am getting non object property because my Auth::guest() returns true whenever i use them in service provider use Illuminate\Contracts\View\View; use Illuminate\Support\ServiceProvider; use Illuminate\Contracts\View\Factory; use App\relations; use App\User; use DB; use Illuminate\Support\Facades\Auth; class RelationServiceProvider extends ServiceProvider { /** * Bootstrap the application services. * * @return void */ public function boot() { // \Auth:

Injecting a primitive property in a base class with Castle Windsor

放肆的年华 提交于 2019-12-18 07:04:38
问题 I have got the following interface definition: public interface ICommandHandler { ILogger Logger { get; set; } bool SendAsync { get; set; } } I have multiple implementations that implement ICommandHandler and need to be resolved. While Castle Windows automatically injects the Logger property, when an ILogger is injected, I can't find a way to configure the SendAsync property to be set to true by Windsor during the creation of new instances. UPDATE The command handlers implement a generic

Castle Windsor strange behaviour wth property injection and factory method

Deadly 提交于 2019-12-18 04:13:06
问题 I am using Castle Windsor 2.5.1 in an ASP.NET MVC project and using property injection to create an object which I expect to always be available on a base controller class. I am using a factory to create this object, however if there is an error in the constructor, I do not get a warning from Windsor at all and it just returns my object but without injecting the property. Is this the expected behaviour, and if so, how can I get an error raised when a factory fails to return anything? Here is

Castle Windsor: Auto-register types from one assembly that implement interfaces from another

痞子三分冷 提交于 2019-12-18 00:31:47
问题 I use Castle Windsor as my IoC container. I have an application that has a structure similar to the following: MyApp.Services.dll IEmployeeService IContractHoursService ... MyApp.ServicesImpl.dll EmployeeService : MyApp.Services.IEmployeeService ContractHoursService : MyApp.Services.IContractHoursService ... I use the XML configuration at the moment, and every time I add a new IService/Service pair, I have to add a new component to the XML configuration file. I want to switch all this over to

Castle Windsor: Auto-register types from one assembly that implement interfaces from another

只愿长相守 提交于 2019-12-18 00:31:40
问题 I use Castle Windsor as my IoC container. I have an application that has a structure similar to the following: MyApp.Services.dll IEmployeeService IContractHoursService ... MyApp.ServicesImpl.dll EmployeeService : MyApp.Services.IEmployeeService ContractHoursService : MyApp.Services.IContractHoursService ... I use the XML configuration at the moment, and every time I add a new IService/Service pair, I have to add a new component to the XML configuration file. I want to switch all this over to

Register shutDownHook in web application

喜你入骨 提交于 2019-12-17 23:31:00
问题 How we can registerShutdown hook in web application? Is there any whays to register it in web.xml or in applicationContext.xml? I know that if we are using application with main class it's simple. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml"); context.registerShutdownHook(); But what about web application? As it uses ContextListener 回答1: registerShutdownHook() in standalone (non-web) application: The @PreDestroy annotation is used on

'Autofac Circular Component Dependency Detected' Error

北慕城南 提交于 2019-12-17 20:24:08
问题 I am new to IoC and am using Autofac in my current project. I have the following 2 classes: public class UserService : IUserService { private readonly IUserRepository _repo; private readonly IMailService _mailService; public UserService(IUserRepository repo, IMailService mailService) { _repo = repo; _mailService = mailService; } } public class MailService : IMailService { private readonly IMailRepository _repo; private readonly IUserService _userService; public MailService(IMailRepository

IOC containers and IDisposable

梦想与她 提交于 2019-12-17 19:23:51
问题 It was recommended to me that, when using an IOC container, I should change this: class Foobar: IFoobar, IDisposable {}; Into this: interface IFoobar: IDisposable{}; class Foobar : IFoobar{}; I'm wondering if this is ok, or if it solves one problem and creates another. It certainly solves the problem where I badly want to do this: using( IFoobar = myContainer.Resolve<IFoobar>() ) { ... } And now I know that any substitute won't cause a run-time error. On the other hand, now all my mock

Unity: Implicit ResolvedParameter for unnamed registrations

有些话、适合烂在心里 提交于 2019-12-17 18:30:13
问题 The UserService constructor has two parameters, a IUnitOfWork and a IUserRepository : public UserService(IUnitOfWork unitofWork, IUserRepository userRepository) { ... } I am using named registrations to differentiate between multiple instances of IUnitOfWork , so when registering the UserService with the Unity container, I need to explicitly specify the parameters using an InjectionConstructor : container.RegisterType<IUserService, UserService>( new InjectionConstructor( new ResolvedParameter