caching per request in ASP.NET? - asp.net

I'm using both IBatis.NET and Spring.NET on a project at work and I'd like to figure out if I can leverage both/either frameworks to achieve "per-request caching" on all calls into my DAL layer. In other words, every time an HTTP request is served by ASP.NET, I would like first call into a DAL method to hit the remote DB, but all calls thereafter to be inflated from cache.
I've seen a few articles describe a way to achieve this using HttpContext.Current, but I can't stomach the idea of polluting my DAL layer with System.Web references. I'd also like to leverage these frameworks if at possible as I'm not fond of re-inventing the wheel.

I'm no expert on IBatis.NET and integration with Spring.NET most likely more thank lacking but here goes..
I would create a custom ICache implementation that uses HttpContext.Current.Items. Then I would make DAL layer objects proxied with Spring.NET AOP (they are behind interfaces, aren't they?). Then it's just a matter of applying the cache advice using the AOP framework.
You should be able to do this by following Spring.NET documentation about AOP caching and implement the ICache using Spring.NET's ASP.NET cache implementation as starting point.

Related

spring mvc and xforms - can it be done, and if so, with how much pain?

So after alot of fuss and bother I got my Spring MVC webclient to use XSLT to create a ModelAndView of some XML coming from my SOAP web service. Well, I suceeded in submitting a request with no parameters to the service and retrieving a XML document with several nodes which XSLT transmorgifies into a web page with a table with row=node.
But of course now I wish to insert rows into my database. And it sounds like this is a job for XForms. But how does one bake XForms into Spring MVC?
As always, hints, suggestions, links, constructive critism and examples are all welcome and appreciated.
TIA,
Still-learning Steve
This might depend on which XForms implementation you choose, and whether it is entirely client-side, or involves both the client and the server.
In general, it is difficult to mix together multiple technologies which are at some level designed to do the same thing. In my experience, for example, it doesn't make sense to combine XForms with JSF. It sounds like Spring MVC might be a similar case, although I don't know much about Spring MVC.
XForms itself follows an MVC architecture, so you would have to figure out whether that conflicts with Spring's MVC architecture. It may or may not be the case.
There are places where it makes sense to combine other technologies with XForms, however. For example:
You can use plain JSP (or similar server-side template technology) to generate XForms pages, and let the client (browser with JavaScript XForms implementation) or server/client (hybrid implementation which transforms XForms on the server) process the resulting XForms.
XForms proper doesn't include a controller (in the sense of routing, pages, etc.). So it makes sense to use a separate controller, client-side or server-side, around pages which use XForms.

ASP.NET Handler (ashx) vs MVC Controller Action for downloading files

We have an application that uses webforms for some older web app pages, but also contains MVC functionality for some of the newer features. We have some new requirements for downloading files that will have to be processed on the server (not direct links to static files on the web server).
I haven't seen anything to indicate if there is a reason one should use an ASHX handler over just using an MVC controller and operating on the response object and returning EmptyResult() at the end of the action method.
Is there a best-practice for this with MVC? Should ASHX handlers be left for WebForms or is there some benefit they provide over using MVC for this type of file download feature?
The performance of HttpHandler is better since it is more bare-metal than MVC actions (just a few extra steps, but still).
Besides that, I see no reason why you should choose one over the other for performance reasons. MVC has some nice features you might want to use, like caching and authorization attributes.
If you choose to use MVC, use results that are specifically built for file handling, like FileStreamResult or FileContentResult.
Well, an ASHX can be a bit more contained and specific... however, there is something to be said about having all your code and logic in your main application.
There is no technical reason to do one over the other, to my knowledge with MVC nowadays. With WebForms it was harder to stream large files, but with MVC you can do that pretty easily (so you don't have to load the whole file into memory first). Also, given modern Async methods you don't have to worry so much about tying up worker threads and what not for scalability.
It's really up to you. Even if you wanted to separate it into its own module, nowadays it might make more sense to make it an owin module rather than an ashx. It's more about how you want to design your app.

Servlet to JSF Transition

I have an application currently running based on servlets (app server glassfish). External applications make an HTTP call to that servlet and get response once successfully achieved what its designed for.
I need now to shift the whole application to JSF2.0 as it needs a front end and sticking to servlet/jsp is not an option.
My questions are:
What's the recommended way to achieve the scenario of external applications getting things done through http calls in my JSF2.0 web app (that is currently I am achieving with servlets)
I need to maintain some counters in Application scope; Would you advise me to keep using servlets in my jsf app too and are the application scoped beans callable in servlets?
The way how you described the old application sounds much like a web service.
JSF is a component based MVC framework, not a web service framework. So JSF is basically the wrong tool for the job. If you want to continue with the standard Java EE stack, look at JAX-WS (SOAP) or JAX-RS (RESTful) instead. The latter is "the" standard web service framework these days.
See also:
Servlet vs RESTful
What's the recommended way to achieve the scenario of external applications getting things done through http calls in my JSF2.0 web app (that is currently I am achieving with servlets)
Like BalusC said, JSF is not exactly what you need and is not designed for what you intend to use it for, webservices are. That being said, you could achieve your goals using JSF if developing an effective web service is not in the cards for you ,either because of a lack of technical expertise on your team or the time factor or other technical constraints require you to serve your customers via a webapp (I honestly can't think of any but hey, you never know)
That being said, I'm assuming you already know your way around JSF2: take a look at this answer to a question somewhat similar to yours. Ultimately, since you're in the Java EE 5-6 stack, I'd strongly suggest you begin work on developing webservices (for basic requirements,they're fairly straightforward to develop). You can download a tutorial here or use oracle's tutorial
I need to maintain some counters in Application scope; Would you advise me to keep using servlets in my jsf app too and are the application scoped beans callable in servlets?
Unless you give us your specific use case, there are very few and far between reasons maintain a servlet within a JSF app. Yes, application,session and request scoped beans are accessible from within a servlet. They're simply stored as objects within the scopes they're named for, application scoped beans within the applicationScope, session scoped beans within the session and request scoped beans within the request.
You may use your JSF webapp to serve low level http requests, but it's a square peg in a round hole chief.

How to convert struts2 to SpringMVC

I don't want to debate whether or not to undertake the conversion from struts2 -> SpringMVC. I have the best reason to do it. It is what the customer is asking/paying for.
I've done struts1 years back and I've done SpringMVC. I'm hoping to configure via xml and not use Annotations that much.
My plan is to:
Take each struts2 action class, understand what action is taking place and then create a clean API for delegating to the business layer. This step is not required. It is here so we can refactor whatever makes sense.
Create a SpringMVC Controller (roughly) for every struts 2 action class.
I know that is just touching the surface of what needs to be done. Anyone have anything else to add?
Thanks in advance,
Andrew
If the struts2 actions were created properly there would be a clean API to the service layer already. Hopefully the Struts2 application is already using Spring DI. Struts2 objects could be reused. But the web layer is relatively thin so it would be best to remove any trace of the struts2 actions to simplify further development, rather than leaving weird artifacts.
There are four main parts that a user of struts is responsible for.
The Action Class
Struts2 specific tags in the view layer
Validation
Interceptors
The action class as already mentioned should be quite thin, it's main purpose is to marshal objects on their way to the view. (To marshal them it will need to set form parameters and validate them). If the objects are acquired from the service layer, then you should be able to mostly cut and paste the logic over into Spring Controllers. If you find logic that should be in the service layer, it should be pushed up.
The tags in the view layer I'm not so sure about. Probably best to replace struts specific tags with jstl tags where possible. Struts2 does not have a huge tag set, and there should be a pretty much one to one mapping between the tags but you'll need to figure out what that mapping is (and which tag lib is best).
Validation - I don't know how Spring MVC does validation.
Interceptors address cross cutting concerns, I don't know how Spring interceptors work. Spring could address the issue of custom interceptors probably with AOP.
All in all, the web tier shouldn't be very invasive... and I'm not sure what you'll gain. Be aware that if your current S2 application is not using Spring DI it is quite easy to add, along with AOP and you can even delegate the creation of actions to Spring. As such I would expect very little return in such a conversion. As an alternative, it might just be easier to start building what you need with Spring MVC, and turn the struts2 actions into web services (json is ridiculously easy). Then dismantle them piece wise when you have the time to implement them in Spring MVC. A conversion means understanding two systems and mapping them, it is messy and error prone... this has the same end effect (the removal of the struts2 framework) but would allow for productivity as well.

What is the best way to establish communication between silverlight controls and ASP.NET

I thought I ask the more exprienced Silverlight users about what they think is the best way to embed Silverlight user controls into an ASP.NET page - in special regards to stablish an easy way of communication between the conrols.
I have heard about services, query strings, etc. but I'd like to find out what has worked for you the best so far...
Take a look at WCF RIA Services. It provides a way for ASP.NET to expose methods and data entities and expose them to Silverlight automatically. RIA Services creates proxy objects on the Silverlight side and wires them up with WCF services without you having to do any work. It is pretty slick.
Here is a starting point. Beware: the learning curve is kind of steep. But it is rewarding once you get there.
http://www.silverlight.net/getstarted/riaservices/
Also, another thing to watch out for is that some of those articles refer to beta/CTP versions and some of the namespaces changed before it went to production.
take a look at the below article which uses event aggregation to communicate.. i found this useful..
http://blog.weareon.net/how-to-communicate-between-two-user-controls-using-event-aggregator/

Resources