I am using dependency injection using Castle Windsor as IOC. But wherever I have injected few application services into controller class's constructor, and if the constructor of any of these application services has many items injected, then it takes too long (as long as 20 sec) to hit the controller action.
Could you please help and suggest some robust solution here?
Thanks in advance!
UPDATE We have 20 repositories and 15 application services injected in a constructor of one of the application service. Let me give you an example to make it clear:
I have 3 application services, say, A, B and C. Here, constructor of C has 25 items (20 repositories and 5 application services) injected in its constructor constructor of B has 35 items (25 repositories and 10 application services - one of them is C) injected in its constructor constructor of A has 35 items (20 repositories and 15 application services - two of them are B and C) injected in its constructor
So, the constructor of C is being initialized 3 times (well, 5 times in real case). This cascading effect is causing the problem. At least, that is what I think.
Any suggestions on this?
CodePudding user response:
Probably one of the reasons why controller initialization can get slower is when you have a lot of dependencies which in turn have dependencies injected in their constructor and so on and so forth.
I was having this issue with Simple Injector. So the solution that worked for me was changing the Lifestyle to Singleton in certain cases.
For Castle Windsor you may have to look into their documentation and find an equivalent.
CodePudding user response:
Probably some (all?) of the constructors do more than just assign the injected components to private fields.
Also, 20 repositories injected - looks overkill. Did you consider using a typed factory facility, so you inject only a repository factory, and resolve the individual repositories using it on demand?
public class MyService : IMyService
{
publiv coid MyService(IRepositoryFactory repoFactory)
{
_repoFactory = repoFactory;
}
public void SomeMethod(string id)
{
var imageRepository = _repoFactory.Get<IImageRepository>(); //or IRepository<Image>() - depends on how your repos are structured
var image = imageRepository.Get(id);
}
}
Same can be said for the services, if they can be unified in some way.