Where and when do Ninject Create a Controller in MVC 3 - asp.net-mvc-3

I am using the NuGet add the Ninject and Ninject.Web.Mvc reference to my project.
but i don't add register code for the controller(with a parameter constructor) binding. but it seems the controller was created correctly, i wonder how Ninject create the controller with dependency parameter withou register the controller to the ninject.
in the Ninject.Web.Mvc, and i found the following sentense in the author (ninject) 's blog,
The controller itself will be found by Ninject even without adding a binding. Of course, you can still add a binding for the controller in case you need to specify more information for the binding (e.g. an additional constructor argument).
i found a similar questions,
How does Ninject create controller in ASP.NET MVC?
the answer saids:
Ninject finds constructor for this type, injects parameters, returns controller instance
my question is : if we don't register the controller how did the NinjectResolver resolve the controller?

non-abstract classes (such as controllers) are resolved by ninject by default as Bind<TheType>().ToSelf() if there is no binding.

Related

Where Factory class should be placed on in a MVC application?

I'm learning MVC pattern and building a simple java application without any framework, just building the package structure by myself in order to understand and practice the MVC concepts.
I have the packages: model, view and controller so far.
So, I have a Factory class to build some objects (part of the model) and I'm not sure in which of these packages place the factory class.
I'm sure is not part of view :), so should it be part of model or controller? Or may be is it correct to have other package to place it?

How do you pass parameters and navigate to Spring managed bean with custom scope - view?

I would like to give you some context first.
The Context
I am currently developing a web application using Spring framework, where I would like to provide users with displaying/editing different instances of the same entity type of model in separate tabs. For displaying and retrieving attributes of these entities, I am using Spring managed beans with matching Data Transfer Object instances and with #Autowired annotated instances of service layer interface implementations for persisting the data.
Because I have experienced, that Spring's session scoped beans are not suitable for multi-tab editing and request scoped beans cannot hold the submitted data "long enough" for redisplay, I have implemented a "custom" scoped view beans. That seems to work fine for displaying and editing multiple same type pages (and entities on them) and for holding the data as long as the bean's action methods return void/null.
Navigating between pages is currently done by the String type return value of the destination page on the bean action methods send to the components like h:commandLink and h:commandButton to be resolved by JSF or by using simple html links.
The Problem
How do you navigate with one h:link/h:button to another page and ALSO pass it parameters to display by a Spring view scope bean after page landing?
Is there a way, how to redirect to another page at first -- for example from a page with list of system users, and then displaying details of a user passed from the launching page?
Questions to be answered and possible solutions?
If I have understood and implemented it correctly, they are already managed and injected with Spring, so they cannot work exactly as JSF's ViewScoped beans, so I cannot easily use their attributes annotated as JSF managed and pass them values of parameters for displaying them on a details/edit page AFTER navigating to it. When I try it, it results in a "CDI #ViewScoped bean functionality unavailable" error message. I think, that for the same reason using #ManagedBean annotated JSF beans and it's f:viewParam on destination page did not work. Is that correct?
I have tried passing parameter to Spring view scoped bean's function with return value of a destination page, but obviously(?) the bean gets recreated after landing on it with it's parameters emptied. Trying to define navigation rule for a Spring view scoped bean's action with the void return type and a landing page name also did not work. It seems like the Spring bean was not recognised in faces-config.xml. Is it so?
Should I use some longer-lived Spring bean's method for calling the view scoped one's constructor with the right parameters? But how do I prevent the view scoped bean's recreation after redirecting?
Switching from view scope to session scope would solve my problem, but it would result in a bad user experience as I see it and given, how my application should work with multiple tabs of the same type instances for the same user.
Passing and retrieving the secured parameters by a view scoped bean from the url address seems to me as maybe not the best security practice. But I am also using Spring Security roles in UI, service layer and url intercept as well as AOP on my service methods for checking the owner of retrieved data. Would be using some data identifier in the url be the only solution possibly working? Is it a worse/better solution from the point of security than using session scoped beans?
I welcome and thank you for every advice you could give me, because this is my first Spring pet project and I am learning on the way.

Having some issues in InventoryController class?

These days i am learning spring by http://static.springsource.org.
I am facing some problem in this page http://static.springsource.org/docs/Spring-MVC-step-by-step/part4.html. i am not getting it clearly that when setProductManager method is called when InventoryController class is invoked. I know that this works as a front controller and when hello.jsp page is requested ,ModelAndView method is executed of InventoryController. but i want to know that when setProductManager method is called.
Any help would be appreciable.
Spring is an ioc container and in this particular example the dependency-injection is implemented using setters (setter injection). Basically the container takes care of supplying your bean (controller in this case) with necessary dependencies.
Back to your question: dependency injection is performed before your bean is ever used by the framework or any other beans requiring it. Furthermore, controllers are singletons. This means setProductManager is called before any request is handled by the controller - when the application is started. And because there is only one instance of the controller - it is called once.

Why Inject Controllers and not Actions

Why does all dependency injection in ASP.NET MVC3 happen at the controller level instead of at the action level. Controller creation is typically overridden in order to inject dependencies when the controller is instantiated. However, the controller is only instantiated as the result of a request for an action. Why not handle the dependency injection at the action level?
Because this is a known pattern and the hooks are in place in MVC to inject into a controller, not an action. There is a controller factor, but not an action factory. You create an instance of the controller, not the method, so thats where the injection needs to take place.
Plus theres a known pattern of constructor injection which would be more appropriate here than some other method (i.e. action method) injection and it also allows any other setup in your constructor that may be necessary.
You could use model binding as an action method injector of sorts. Here's a little introduction to model binders you can read:
http://www.singingeels.com/Articles/Model_Binders_in_ASPNET_MVC.aspx
You could instance and pass repositories, or anything you'd like to the action methods in this way, and it'd be reusable.
My guess is that you are starring at a GOD controllers with 10 dependecies and a lot of actions and wonder how this could be made better?
Remember! Controllers should be skinny and contain few actions. I prefer my controllers to only have one action per HTTP method.

JSF Controller Bean - Scoping

WI have a question about the "Best Practice" Design for controller beans.
I was reading this very good question and the linking article:
Question "JSF backing bean structure (best practices)"
Scoping Best Practice
Online Article
Distinctions between different kinds of JSF Managed-beans
My question is concerning the controller bean. I'm using JSF / Spring and was wondering why I would want to use request scope for Controller beans?
The controller logic being defined as "...execute some kind of business logic and return navigation outcome.." I would think doesn't require a request scope but either session/application scope. Why keep creating those controller objects on every request?
In my case I would create the controller bean in the faces-config obviously and inject it with my managed properties through spring.
Thoughts please around the scoping? Thanks.
Clarification:
Using JSF 1.2, Spring 3. Using faces-config.xml to declare my beans. Not through annotations.

Resources