Where Factory class should be placed on in a MVC application? - model-view-controller

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?

Related

How to read property from OSGI configuration in AEM 6.1 in model class which extends WCMUse class

I am extending my model class to WCMUse class to write my business logic. I have osgi configuration which has couple of properties. I want to read one of the property in my model class. I am not sure how to get the handle of osgi configuration in WCMUse class. Any pointers will be highly appreciated.
Adding the answer for future references.
You can use the #getSlingScriptHelper() method of the WCMUse class to get an handle of the SlingScriptHelper. This is the same sling object that is available through inclusion of global.jsp in traditional JSP scripts.
You can then call #getService() method of the SlingScriptHelper to look up the desired OSGi Service.
The following code snippet can be used in the model to get the service configuration
getSlingScriptHelper().getService(<<Configuration Service>>.class)

Issue in understanding the Spring Bean Scopes Vs Spring web flow Session Scopes

We Know the Spring Framework gives
singleton,
prototype,
request,
session,
global_session
bean Scopes.
Also we know that Spring web flow gives flowScope,viewScope,requestScope,flashScope,conversationScope.
So If i mentioned one Component, say Student, as #Component #Scope=singleton in a spring MVC project. For each request, will it create a new Student Object or Spring container will create only once?
You are confusing yourself with objects and beans.
For each request, will it create a new Student Object or Spring container will create only once?
The functioning of Spring is purely using beans. When you declare a something like a #Component, it's just an annotation that tells Spring that the part you've declared as a component is either Model or View or Controller i.e. a component of MVC. When you say something like #Scope=singleton, it tells Spring that only a single object instance can access the bean.
Let me make it more clear. Say you and I are objects and a strawberry candy is a bean. So if you have the candy. I cannot take it from you. Meaning only one of us can have that candy. It's the same thing with singleton scope.
Hope I made things simpler.. :)

Using Inheritance in Spring MVC 3.1

Researching the use of inheritance in Spring MVC.
Is it a good idea to have a base controller that other controllers can extend ?
The base controller would be to hold functionality common to all controllers.For E-g getting a handle to the logged-in user etc.
If using the base controller is not a good idea are there any other suggestions to implement something like what I have mentioned above.
It is perfectly acceptable to have a base controller that other controllers can extend. When Spring introduced #Controller annotations, they paved the way for you to use whatever class hierarchy you want.
Just be aware that as an object oriented design principle, it's good to favor composition over inheritance. As a rule of thumb (not a hard and fast rule) I would recommend moving your common controller code into a class whose interface can be injected into your controllers via #Inject or #Autowired.
Another suggestion for getting a handle to the logged-in user is a little more work but very nice once you have it. See the discussion here about using a current user annotation on your controller method arguments. This is what I do on my project and it works great!

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.

Where and when do Ninject Create a Controller in 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.

Resources