last day I implemented a simple class to serve as IOC container (not much like that) in one of my playground projects and some pro guy told me that is going to have some problem when testing the code (too vein to share what exactly is the problem !!)
although I always use IOC frameworks myself, I wanted to know why exactly this methodology is bad ?
I even wrote many tests (using mock frameworks) using this methodology and never had a problem.
I want your Idea about design problems of this:
public class IocContainer{
private static IocContainer instance;
private IocContainer(){
}
public static IocContainer getInstance()
{
if(instance == null){
instance = new IocContainer();
}
return instance;
}
public ChatPresenter getChatPresenter(ChatView chatView)
{
return new ChatPresenterImpl(chatView, getChatRepo() , getMessagingService());
}
private ChatRepo getChatRepo()
{
return new ChatRepoImpl(getDbHelper(), getRemoteService());
}
private MessagingService getMessagingService()
{
return new MessagingServiceImpl()
}
private DbHelper getDbHelper()
{
return new DbHelperImpl();
}
private RemoteService getRemoteService()
{
return new RemoteServiceImpl();
}
}
as you see I only made getChatPresenter accessible and I use this like below in my view and it works well.
ChatPresenter presenter = IocContainer.getInstance().getChatPresenter(this)
the code handles inversion of control without any tight coupling (using interfaces).
I wanna know any wrong thing with this approach ? (I want answers from technical point of view cuz I already know using a Ioc container library is easier has more features like scopes and so ...)
Actually I want to know any problem and limitation this approach would make in the future ?
be cruel and kill me :D