jsf call managed bean method from another managed bean - jsf-2

I am using JSF 2.x in my project.
I have two pages and two managed beans (request scoped) for these two pages.
Page 1 is loaded after user clicks on a link on Home page. This link calls view() method of Bean1 (with request parameter ID=some value) in which we load some data from DB (based on ID) and then redirects to page 1 where this data is displayed.
Later, user navigates from page 1 to page 2 and here we pass the same ID to the page 2.
On page 2, user enters data and clicks on Save button. This will call saveDetails() method of Bean 2.
After the saveDetails() method I want to redirect to page 1 by calling Bean1's view() method and passing the ID as request parameter. I cannot redirect directly to page1 because then there will be no data to display as the bean1 is request scoped.
Hence, I want to call bean1.view() with request parameter ID. I.e. I want to achieve the same behavior as if user has clicked on the link on Home page.
How to achieve this?
Here is the code so far:
#ManagedBean
#Component
#RequestScoped
#Scope("request")
// bean for page1
public class ModifyCDSPageBean extends BasePageBean {
private DisplayTicket ticket;
private String selectedCDS;
...
...
// CDS List
private static Map<String, String> cdsList = new LinkedHashMap<String, String>();
#Autowired
TicketConsoleGTRDao ticketConsoleGTRDao;
private static final Logger LOGGER = Logger.getLogger(ModifyCDSPageBean.class);
public String viewTicketDetails() {
populateCDSList();
....
// Method updated to set DisplayInfoTravail
String id_incident = getRequestParameterValue(TicketConstants.ID_INCIDENT);
List<InfoTravail> travailsList =
ticketConsoleGTRDao.findMatchingTrvailInformation(id_incident);
....
return NavigationConstants.PAGE_MODIFY_CDS;
}
...
...
}
#ManagedBean
#Component
#RequestScoped
#Scope("request")
//Bean for page 2
public class CreateInfoTravailPageBean extends BasePageBean {
private String selectedTypeInfoTravail;
...
...
#Autowired
TicketConsoleGTRDao ticketConsoleGTRDao;
private static final Logger LOGGER = Logger.getLogger(CreateInfoTravailPageBean.class);
public String viewInfoTravail() {
populateTypeInfoTravailList();
...
...
return NavigationConstants.PAGE_CREATE_INFO_TRAVAIL;
}
public String saveInfoTravail() {
String idIncident = getRequestParameterValue(TicketConstants.ID_INCIDENT);
infoTravail.setTicketId(idIncident);
infoTravail.setDate_creation(formatter.format(new Date()));
// HERE I WANT TO CALL ModifyCDSPageBean.viewTicketDetails() method
// pass id_incident as request parameter while making this call
// because if you check ModifyCDSPageBean.viewTicketDetails above it
// looks for request parameter id_incident
}

Re-reading your requirements, it sounds like you want a page initialization code. That is, turn around the flow and don't let entry-points into your page1 call the bean's code, let the bean do that itself:
#ManagedBean #RequestScoped
public class ModifyCDSPageBean {
#Inject #Param(name = TicketConstants.ID_INCIDENT)
private ParamValue<Long> myParam;
#Autowired
private TicketConsoleGTRDao dao;
private List<InfoTravail> travailsList;
#PostConstruct
public void init() {
if (myParam.getValue() != null) {
// do stuff based on the param being set
travailsList = dao.findById(myParam.getValue());
}
}
// getter for travailsList
}
And then include the parameter in your navigation from bean2:
public class Bean2 {
public String save() {
String idIncident = getRequestParameterValue(TicketConstants.ID_INCIDENT);
// do stuff and then return to page1, passing parameter ID_INCIDENT
return String.format("page1?faces-redirect=true&%s=%s",
TicketConstants.ID_INCIDENT, idIncident);
}
If you don't need to execute your view preparation code every time you create ModifyCDSPageBean (ie, if it's used on other pages, too), look into calling it on your page. If you've got JSF-2.2, try <f:viewAction action="#{modifyCDSPageBean.init}"> or on older versions, <f:event listener="#{modifyCDSPageBean.init()}" type="preRenderView">.
Note that #PostConstruct with a #RequestScoped bean will re-create the bean with each AJAX-request, which is probably not what you want. In that case, try #ViewScoped.
My code example uses omnifaces' #Param for my lack of knowledge of spring. Maybe they have something similar in the toolkit already (or just call your getRequestParameterValue from the bean method).

Related

StackOverflowException in spring-data-jpa app with spring-security AuditorAware

I have a really nasty StackOverflowException in my spring backend, that I need help with. This is not going to be solved easily. I really hope to find some help here.
Most parts of my backend work. I can query my REST interface for models, they are nicely returned by spring-hateoas, GET, PUT and POST operations work. But one exception: When I try to update an existing DelegationModel, then I run into an endless StackOverflowException.
Here is my DelegetionModel.java class. Please mark, that delegation model actually doesn't have any property annotated with #CreatedBy!
#Entity
#Data
#NoArgsConstructor
#RequiredArgsConstructor(suppressConstructorProperties = true) //BUGFIX: https://jira.spring.io/browse/DATAREST-884
#EntityListeners(AuditingEntityListener.class) // this is necessary so that UpdatedAt and CreatedAt are handled.
#Table(name = "delegations")
public class DelegationModel {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
public Long id;
/** Area that this delegation is in */
#NonNull
#NotNull
#ManyToOne
public AreaModel area;
/** reference to delegee that delegated his vote */
#NonNull
#NotNull
#ManyToOne
public UserModel fromUser;
/** reference to proxy that receives the delegation */
#NonNull
#NotNull
#ManyToOne
public UserModel toProxy;
#CreatedDate
#NotNull
public Date createdAt = new Date();
#LastModifiedDate
#NotNull
public Date updatedAt = new Date();
}
As described in the Spring-data-jpa doc I implemented the necessary AuditorAware interface, which loads the UserModel from the SQL DB. I would have expected that this AuditorAware interface is only called for models that have a field annotated with #CreatedBy.
#Component
public class LiquidoAuditorAware implements AuditorAware<UserModel> {
Logger log = LoggerFactory.getLogger(this.getClass()); // Simple Logging Facade 4 Java
#Autowired
UserRepo userRepo;
#Override
public UserModel getCurrentAuditor() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication == null || !authentication.isAuthenticated()) {
log.warn("Cannot getCurrentAuditor. No one is currently authenticated");
return null;
}
User principal = (org.springframework.security.core.userdetails.User) authentication.getPrincipal();
UserModel currentlyLoggedInUser = userRepo.findByEmail(principal.getUsername()); // <<<<======= (!)
return currentlyLoggedInUser;
} catch (Exception e) {
log.error("Cannot getCurrentAuditor: "+e);
return null;
}
}
}
Now I update a DelegationModel in my UserRestController. The functional "Scrum User Story" here is:
As a user I want to be able to store a delegation so that I can forward my right to vote to my proxy.
#RestController
#RequestMapping("/liquido/v2/users")
public class UserRestController {
[...]
#RequestMapping(value = "/saveProxy", method = PUT, consumes="application/json")
#ResponseStatus(HttpStatus.CREATED)
public #ResponseBody String saveProxy(
#RequestBody Resource<DelegationModel> delegationResource,
//PersistentEntityResourceAssembler resourceAssembler,
Principal principal) throws BindException
{
[...]
DelegationModel result = delegationRepo.save(existingDelegation);
[...]
}
[...]
}
For some reason, that I cannot see, this actualy calls the AuditorAware implementation above. The problem is now, that my LqiuidoAuditorAware implementation is called again and again in and endless loop. It seems that the query for the UserModel inside LiquidoAuditorAware.java calls the LiquidoAuditorAware again. (Which is unusual, because that is only a read operation from the DB.)
Here is the full ThreadDump as a Gist
All the code can by found in this github repo
I'd really apriciate any help here. I am searching in the dark :-)
The reason for the behavior you see is that the AuditorAware implementation is called from within a JPA #PrePersist/#PreUpdate callback. You now issue a query by calling findByEmail(…), which triggers the dirty-detection again, which in turn causes the flushing to be triggered and thus the invocation of the callbacks.
The recommended workaround is to keep an instance of the UserModel inside the Spring Security User implementation (by looking it up when the UserDetailsService looks up the instance on authentication), so that you don't need an extra database query.
Another (less recommended) workaround could be to inject an EntityManager into the AuditorAware implementation, call setFlushMode(FlushModeType.COMMIT) before the query execution and reset it to FlushModeType.AUTO after that, so that the flush will not be triggered for the query execution.

NHibernate session is closed when refreshing page

This is another strange problem I've encountered this days!!! I've created and MVC 4 app using nhibernate. and added a filter attribute named [LoggingNHibernateSessionAttribute] on my HomeController which manages session for each action. I've followed 'ASP.NET MVC4 and the Web API published by Apress'.
public class LoggingNHibernateSessionAttribute : ActionFilterAttribute
{
private readonly IActionLogHelper _actionLogHelper;
private readonly IActionExceptionHandler _actionExceptionHandler;
private readonly IActionTransactionHelper _actionTransactionHelper;
public LoggingNHibernateSessionAttribute()
: this(WebContainerManager.Get<IActionLogHelper>(),
WebContainerManager.Get<IActionExceptionHandler>(),
WebContainerManager.Get<IActionTransactionHelper>())
{
}
public LoggingNHibernateSessionAttribute(
IActionLogHelper actionLogHelper,
IActionExceptionHandler actionExceptionHandler,
IActionTransactionHelper actionTransactionHelper)
{
_actionLogHelper = actionLogHelper;
_actionExceptionHandler = actionExceptionHandler;
_actionTransactionHelper = actionTransactionHelper;
}
public override void OnActionExecuting(ActionExecutingContext actionExectingContext)
{
_actionLogHelper.LogEntry(actionExectingContext.ActionDescriptor);
_actionTransactionHelper.BeginTransaction();
}
public override void OnActionExecuted(ActionExecutedContext actionExecutedContext)
{
_actionTransactionHelper.EndTransaction(actionExecutedContext);
_actionTransactionHelper.CloseSession();
_actionExceptionHandler.HandleException(actionExecutedContext);
_actionLogHelper.LogExit(actionExecutedContext.ActionDescriptor);
}
}
ActionTransactionHelper
public class ActionTransactionHelper : IActionTransactionHelper
{
private readonly ISessionFactory _sessionFactory;
private readonly ICurrentSessionContextAdapter _currentSessionContextAdapter;
public ActionTransactionHelper(
ISessionFactory sessionFactory,
ICurrentSessionContextAdapter currentSessionContextAdapter)
{
_sessionFactory = sessionFactory;
_currentSessionContextAdapter = currentSessionContextAdapter;
}
public void BeginTransaction()
{
var session = _sessionFactory.GetCurrentSession();
if (session != null)
{
session.BeginTransaction();
}
}
public bool TransactionHandled { get; private set; }
public void EndTransaction(ActionExecutedContext filterContext)
{
var session = _sessionFactory.GetCurrentSession();
if (session == null) return;
if (!session.Transaction.IsActive) return;
if (filterContext.Exception == null)
{
session.Flush();
session.Transaction.Commit();
}
else
{
session.Transaction.Rollback();
}
TransactionHandled = true;
}
public bool SessionClosed { get; private set; }
public void CloseSession()
{
if (_currentSessionContextAdapter.HasBind(_sessionFactory))
{
var session = _sessionFactory.GetCurrentSession();
session.Close();
session.Dispose();
_currentSessionContextAdapter.Unbind(_sessionFactory);
SessionClosed = true;
}
}
}
when run the app, I can save an entity in the dataBase. but when I hit refresh button and exception thrown indication session is closed.
I don't know why this happens. (I searched and find this NHibernate throwing Session is closed but couldn't solve my problem).
in my NinjectConfigurator I added inRequestScope() to all of injections but no answer. I checked when I refresh the page session will be opened. but I donnow why it say session is closed?!
UPDATE:
when I first run the app. I can create a new member. but when I hit the refresh button, the session will be closed unexpectedly!!
first run:
everything works well
after hitting refresh button:
a new session bind to the current context.
the new session will be injected the repository (session is open)
the ActionTransactionHelper calls beginTransaction()
4- customMembership createUser (....) called
5- but when the _userRepositoy.save(user)called in the repository session is closed!!!!
note:but when still endTransaction and closeSession isn't called. but how session is closed?
if I comment closeSession() in onActionExecute(). session alway is open and everything woks well if refresh the page.
I checked a lot and tried different way I knew. it only happens when for the second time I want to do CRUD operations with my customMembership.
for other entities it works like a charm!
I have upoaded my sample code. for testing just create and empty database and change connection string. then go to localHost:*****/api/categories (user and pass doesn't required)
Download sample project:
Size: 47 MB
https://www.dropbox.com/s/o63wjng5f799fii/Hashem-MVC4ServicesBook.rar
size: 54 MB
Zip Format: https://www.dropbox.com/s/smrsbz4cbtznx1y/Hashem-MVC4ServicesBook2.zip
A very important thing here, could be the nature of the NHibernate. The NHibernate and its Session are in the ASP.NET MVC living longer, then could be expected. I mean not only inside of the
ActionExecuting (Controller Action starts)
ActionExecuted (the View or Redirect is called)
Session in fact must live also through the phase of rendering. Because, we could load some proxy in the "Action()" but its collection, could be lazily loaded only during the View rendering. So even in these phases Session must be opened (the same Session from the request begining)
ResultExecuting (the proxy could start to be loaded only here)
ResultExecuted (almost all is done, let's close the session)
Other words... keep the session opened throught the complete Request. From authorization untill the content is rendered.
NOTE: Anohter hint, just to be sure that all is ok, I am using this scenario (maybe you do as well):
Client FORM is about to send the data to server. The method is POST, the Action is Update()
Sent FORM is coming to server, Action Update() is triggerred - all the transactions stuff is in place (as described above)
Once NHibernate persists the data into DB, the Update() action ends, and is redirected to action
Detail() if all is ok or
Edit() if something goes wrong
The users Browser was redirected to action Detail or Edit. So if user does REFRESH, the Detail or Edit is refreshed. The Update() is not called at all (it is a POST method)
In fact, the step 1. was one of the Actions Detail or Edit. In this case, we would face this issue already...
You have this error since Asp.Net MVC does not create a new instance of LoggingNHibernateSessionAttribute every request. It creates a new instance when you request an action first time and then uses this instance in the future.
The behaviour is the following:
First invocation of Post -> new instance of 'LoggingNHibernateSession' is created
First invocation of Put -> another one instance of 'LoggingNHibernateSession' is created
Second invocation of Put -> instance of 'LoggingNHibernateSession' from previous step is used
First invocation of Delete -> another one instance of 'LoggingNHibernateSession' is created
[LoggingNHibernateSession]
public JsonResult Post(Dto data)
{
/* ... */
}
[LoggingNHibernateSession]
public JsonResult Put(int id, Dto data)
{
/* ... */
}
[LoggingNHibernateSession]
public JsonResult Delete(int id)
{
/* ... */
}
It can be solved using Func<IActionLogHelper> instead of IActionLogHelper in the constructor. An instance of IActionLogHelper can be initialised within OnActionExecuting method.
public class LoggingNHibernateSessionAttribute : ActionFilterAttribute
{
/* your code */
private readonly Func<IActionTransactionHelper> _getActionTransactionHelper;
private IActionTransactionHelper _actionTransactionHelper;
public LoggingNHibernateSessionAttribute()
: this(WebContainerManager.Get<IActionLogHelper>(),
WebContainerManager.Get<IActionExceptionHandler>(),
() => WebContainerManager.Get<IActionTransactionHelper>())
{
}
public LoggingNHibernateSessionAttribute(
IActionLogHelper actionLogHelper,
IActionExceptionHandler actionExceptionHandler,
Func<IActionTransactionHelper> getActionTransactionHelper)
{
_actionLogHelper = actionLogHelper;
_actionExceptionHandler = actionExceptionHandler;
_getActionTransactionHelper = getActionTransactionHelper;
_actionTransactionHelper = null;
}
public override void OnActionExecuting(ActionExecutingContext actionExectingContext)
{
_actionTransactionHelper = _getActionTransactionHelper();
_actionLogHelper.LogEntry(actionExectingContext.ActionDescriptor);
_actionTransactionHelper.BeginTransaction();
}
/* your code */
}

Setting bean property from validator

Is there a way to set a bean property from a Validator?
In my case, I have a validator which connects to the database and performs some validation.
Upon successful validation, I want to save the object received from database, inside a bean property.
Currently i'm doing this by setting a static property of my bean from the validator.
Here is my validator method
public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
//perform validation
if(isValidated) {
Referral ref = database.getReferral(value.toString()); //receive referral object from batabase
RegistrationBean.staticReferral = ref; // Set ref to RegistrationBean's static property
} else {
FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_FATAL, "Invalid Referral!", "Referral does not exist!");
throw new ValidatorException(msg);
}
}
and here is my RegistrationBean
#ManagedBean
#ViewScoped
public class RegistrationBean implements Serializable {
//other bean properties
private Referral referral;
public static Referral staticReferral;
public RegistrationBean() {
//default constructor
}
public Referral getReferral() {
this.staticReferral = referral;
return referral;
}
// other getters, setters and methods
}
So the questions in my mind are:
Is there a way to set a bean property directly from a bean? (without
using a static property)
Would there be any concurrency issues (one user may receive other user's selected referral object etc) using the existing approach?
Thanks
Static members in managed beans are shared among all instances (and users of your application). So think at least twice before making a member variable static.
If you make your validator a managed bean, you can inject your target managed bean into your validator. See this answer for details.
In the given example an EJB is injected, but you can inject a JSF managed bean via the #ManagedProperty annotation

How to redirect xhtml page to the same Request Scoped Bean?

Since the bean is in Request Scope, redirect creates a new instance.
If I try to put the bean in the View Scope, then I get the error of Property name is referenced to object narrower than the target View.
I'm displaying a tree, and when the user clicks on the tree node, say the 2nd node, the values corresponding to that node is displayed on the next page.
#ManagedBean
#RequestScoped
public class ThreeSixtyDegreeBean implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
#ManagedProperty(value="#{param.name1}")
private String name;
private String type;
private String typeName;
private List<AttributeDetails> attributeList;
private List<Entity> entityList;
private boolean rendered;
private TreeNode root;
private TreeNode selectedNode;
public void onNodeSelect() {
*selecting Data of the Node selected*
String a=fetchData();
try {
FacesContext.getCurrentInstance().getExternalContext()
.redirect(a);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Where a is name of the xhtml page.
You can stash your stuff in the new JSF 2 Flash Scope to stash attributes between requests. Your onNodeSelect() can now look like this :
public void onNodeSelect() {
*selecting Data of the Node selected*
FacesContext context = FacesContext.getCurrentInstance();
Flash flash = context.getCurrentInstance().getExternalContext().getFlash(); //prepare jsf flash scope, to store user data pojo for the next view
flash.putNow("myData", fetchData());
try {
FacesContext.getCurrentInstance().getExternalContext()
.redirect(a);
} catch (IOException e) {
e.printStackTrace();
}
}
}
On the destination page, you can then retrieve the data you stored in the flash scope using the #{flash} EL expression. It's essentially a Map so you just use the key of the value you stored ("myData" in the example I used above). to access it, use
#{flash.myData.someMemberVariable}
The view scope wouldn't have worked either. It lives as long as you're postbacking to the same view. A redirect basically creates a brand new GET request and would also have recreated the view scope.
In this particular case, you're better off performing data initialization in the redirected request, not in the postback request. You could do that by creating a normal GET link wherein you pass the necessary information as request parameters. Something like this:
<h:link value="#{node.name}" outcome="#{node.viewId}">
<f:param name="someId" value="#{node.someId}" />
<f:param name="name1" value="#{param.name1}" />
</h:link>
and then in the request scoped bean associated with the redirected page, you can just use #ManagedProperty or even the <f:viewParam> to set the request parameters as model values.
See also:
Communication in JSF 2.0 - Processing GET request parameters

StructureMap IOC/DI and object creation

I'm building small web shop with asp.net mvc and Structuremap ioc/di. My Basket class uses session object for persistence, and I want use SM to create my basket object through IBasket interface. My basket implementation need HttpSessionStateBase (session state wrapper from mvc) in constructor, which is available inside Controller/Action. How do I register my IBasket implementation for SM?
This is my basket interface:
public interface IBasketService {
BasketContent GetBasket();
void AddItem(Product productItem);
void RemoveItem(Guid guid);
}
And SM registration:
ForRequestedType(typeof (IBasketService)).TheDefaultIsConcreteType(typeof (StoreBasketService));
But my StoreBasketService implementation has constructor:
public StoreBasketService(HttpSessionStateBase sessionState)
How do I provide HttpSessionStateBase object to SM, which is available only in controller?
This is my first use of SM IOC/DI, and cann't find solution/example in official documentation and web site ;)
If you absolutely have to have your StoreBasketService use the session, I'd be tempted to define an interface and wrapper around HttpSessionState instead of using HttpSessionStateBase so that you can register it with StructureMap as well.The wrapper would get the session state from the current context. Register the wrapper with StructureMap and then have your StoreBasketService take the interface as the argument to the constructor. Structure map should then know how to create an instance of the interface wrapper and inject it into your StoreBasketService class.
Using an interface and wrapper will allow you to mock the wrapper in your unit tests, muc in the same way HttpSessionStateBase allows mocking the actual session.
public interface IHttpSessionStateWrapper
{
HttpSessionState GetSessionState();
}
public class HttpSessionStateWrapper : IHttpSessionStateWrapper
{
public virtual HttpSessionState GetSessionState()
{
return HttpContext.Current.Session;
}
}
ForRquestedType(typeof(IHttpSessionStateWrapper))
.TheDefaultIsConcreteType(typeof(IHttpSessionStateWrapper));
public class StoreBasketService
{
HttpSessionState session;
public StoreBasketService( IHttpSessionstateWrapper wrapper )
{
session = wrapper.GetSessionState();
}
// basket implementation ...
}
However, you can have StructureMap actually store your basket in the session using .CacheBy(InstanceScope.HttpContext) when registering it. It may actually be better to have your StoreBasketService implement internal storage instead of storing things in the session -- then you lose the dependency on the session state entirely (from the perspective of your class) and your solution could be simpler. Your internal storage could be a Dictionary<Guid,Product> since this is how you access them via your interface.
See also:
http://www.lostechies.com/blogs/chad_myers/archive/2008/07/15/structuremap-basic-scenario-usage.aspx
http://www.lostechies.com/blogs/chad_myers/archive/2008/07/17/structuremap-medium-level-usage-scenarios.aspx
ForRequestedType<IBasketService>()
.TheDefault.Is.OfConcreteType<StoreBasketService>()
.WithCtorArg("sessionState").EqualTo(HttpContext.Current.Session);
?? does that work?
I just started with StructureMap, and I do not get the results you are describing.
I performed a simple test using a simple class, configuring Structuremap to cacheby HttpContext, and from what I can see, CacheBy.HttpContext means within the same request you will get the same instance... not within the same Session
The constructor of my class, sets the date/time in a private field
I have a button which gets 2 instances of MyClass with one second interval...
It then display the time of both instances in a label.
Pressing the first time this button, object A and B are same instance, as their creation time is exactly the same, as expected.
Clicking the button a second time, you would expect the creation time to not have changed if instances would be cached in session... however, in my test I get a new creation time ...
Structuremap configuration:
ObjectFactory.Initialize(x=>x.ForRequestedType<MyClass>(). CacheBy(InstanceScope.HttpContext));
Button clicked event of test page
protected void btnTest_Click(object sender, EventArgs e)
{
MyClass c = ObjectFactory.GetInstance<MyClass>();
System.Threading.Thread.Sleep(1000);
MyClass b = ObjectFactory.GetInstance<MyClass>();
lblResult.Text = String.Format("cache by httpcontext First:{0} Second:{1} session id {2} ", c.GetTimeCreated(), b.GetTimeCreated(),Session.SessionID);
}
MyClass
public class MyClass
{
private DateTime _timeCreated;
public MyClass()
{
_timeCreated = DateTime.Now;
}
public string GetTimeCreated()
{
return _timeCreated.ToString("dd/MM/yyyy hh:mm:ss");
}
}
You could also use one of the ObjectFactory.Inject methods to inject the HttpSessionStateBase into StructureMap. It would then invoke the constructor with the injected HttpSessionStateBase.
I just made my first attempt at creating an custom scope... build a small web application with it, and as far as I can see, it seems to work. This will cache the object inside the current user session and will return the same object as long as you remain inside the same session:
public class HttpSessionBuilder : CacheInterceptor
{
private readonly string _prefix = Guid.NewGuid().ToString();
protected override CacheInterceptor clone()
{
return this;
}
private string getKey(string instanceKey, Type pluginType)
{
return string.Format("{0}:{1}:{2}", pluginType.AssemblyQualifiedName, instanceKey, this._prefix);
}
public static bool HasContext()
{
return (HttpContext.Current.Session != null);
}
protected override bool isCached(string instanceKey, Type pluginType)
{
return HttpContext.Current.Session[this.getKey(instanceKey, pluginType)] != null;
}
protected override object retrieveFromCache(string instanceKey, Type pluginType)
{
return HttpContext.Current.Session[this.getKey(instanceKey, pluginType)];
}
protected override void storeInCache(string instanceKey, Type pluginType, object instance)
{
HttpContext.Current.Session.Add(this.getKey(instanceKey, pluginType), instance);
}
}
You have to configure the ObjectFactory as follows in the global.asax Application_start
ObjectFactory.Initialize(x=>
x.ForRequestedType<MyClass>().InterceptConstructionWith(new HttpSessionBuilder()));

Resources