每个会话的生活方式城堡项目与ASP.NET MVC [英] Castle project per session lifestyle with ASP.NET MVC

查看:107
本文介绍了每个会话的生活方式城堡项目与ASP.NET MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是真正的新温莎城堡IoC容器。我想知道如果那里有一种方法来存储使用IoC容器会话变量。我在想的东西在这个行:

I'm really new to Castle Windsor IoC container. I wanted to know if theres a way to store session variables using the IoC container. I was thinking something in the line of this:

我想有一个类来存储搜索选项:

I want to have a class to store search options:

public interface ISearchOptions{
    public string Filter{get;set;}
    public string SortOrder{get;set;}
}

public class SearchOptions{
    public string Filter{get;set;}
    public string SortOrder{get;set;}
}

和再注入这为有能力使用它的类:

And then inject that into the class that has to use it:

public class SearchController{
    private ISearchOptions _searchOptions;
    public SearchController(ISearchOptions searchOptions){
        _searchOptions=searchOptions;
    }
    ...
}

然后我的web.config,在那里我配置的城堡我想有这样的:

then in my web.config, where I configure castle I want to have something like:

<castle>
    <components>
        <component id="searchOptions" service="Web.Models.ISearchOptions, Web" type="Web.Models.SearchOptions, Web" lifestyle="PerSession" />
    </components>
</castle>

和有IoC容器处理会话对象,而无需显式访问它自己。

And have the IoC container handle the session object without having to explicitly access it myself.

我怎样才能做到这一点?

How can I do this?

感谢。

编辑:在做一些研究。基本上,我要的是有一个会话范围的组件。我来自Java和Spring框架,并在那里我有会话范围豆,我认为是存储会话数据非常有用的。

Been doing some research. Basically, what I want is to have the a session Scoped component. I come from Java and Spring Framework and there I have session scoped beans which I think are very useful to store session data.

推荐答案

这可能是你在找什么。

public class PerSessionLifestyleManager : AbstractLifestyleManager
    {
    private readonly string PerSessionObjectID = "PerSessionLifestyleManager_" + Guid.NewGuid().ToString();

    public override object Resolve(CreationContext context)
    {
        if (HttpContext.Current.Session[PerSessionObjectID] == null)
        {
            // Create the actual object
            HttpContext.Current.Session[PerSessionObjectID] = base.Resolve(context);
        }

        return HttpContext.Current.Session[PerSessionObjectID];
    }

    public override void Dispose()
    {
    }
}

然后添加

<component
        id="billingManager"  
        lifestyle="custom"  
        customLifestyleType="Namespace.PerSessionLifestyleManager, Namespace"  
        service="IInterface, Namespace"
        type="Type, Namespace">
</component>

这篇关于每个会话的生活方式城堡项目与ASP.NET MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆