如何在 Web 上下文中正确配置 Spring.NET

发布于 2024-12-02 16:44:28 字数 3045 浏览 0 评论 0原文

我在阻止值出现在所有用户的范围内时遇到了一些问题。我不确定我到底做错了什么,但我已经无计可施了。我想要么我的容器对象范围错误,要么我沿着 WebContext 传递值。首先,这是我在代码中所做的事情。这是可行的,但它显示了登录站点的第一个用户的详细信息(注意:我的 PageStore 只是 IDictionary 类型的属性,它存储要共享给页面上所有用户控件的值):

using (BasePage page = (BasePage)(Spring.Web.UI.Page)this.Context.Handler) {
    if ((page.PageStore("LoginId") != null)) {
        this.Model.LoginId = Convert.ToString(page.PageStore("LoginId"));
    }
}

其次,我尝试了不同的范围为了我的物品,但我空空如也。以下是我的对象当前在容器中的配置方式:

我的业务层配置的片段

 <!-- Transaction Management Strategy - local database transactions -->
    <object id="transactionManager"
          type="Spring.Data.Core.AdoPlatformTransactionManager, Spring.Data" scope="request">
        <property name="DbProvider" ref="MyProvider"/>
    </object>
    <tx:attribute-driven transaction-manager="transactionManager" />

    <!-- Data Access Objects -->
    <object id="DataAccess" type="MyApp.Business.DataAccess.DataAccess, MyApp.Business.DataAccess" abstract="true"  scope="request">
        <property name="AdoTemplate" ref="MyTemplate" />
    </object>

    <object id="ManagerDAO" type="MyApp.Business.DataAccess.ManagerDAO, MyApp.Business.DataAccess" parent="DataAccess" scope="request">
        <property name="TableName" value="vw_managers" />
        <property name="IsAllowedDeletion" value="False" />
        <property name="IsAllowedUniqueIdRetrieval" value="False" />
    </object>

     <!-- Services -->            

    <object id="SearchService" type="MyApp.Business.Services.SearchService, MyApp.Business.Services" scope="request">
        <property name="ManagerDAO" ref="UserDAO" />
    </object>

我的表示层的片段:

<object id="BasePageModel" type="MyApp.Presentation.Models.BasePageModel, MyApp.Presentation" abstract="true"  scope="session"/>
<object id="ManagerModel" type="MyApp.Presentation.Models.ManagerModel, MyApp.Presentation" parent="BasePageModel"  scope="session"/>

<!-- Presenters-->    
<object name="ManagerPresenter" type="MyApp.Presentation.Presenters.ManagePresenter, MyApp.Presentation" singleton="false" scope="session">
    <property name="SearchSvc" ref="SearchService"/>
</object>

我的 UI 层对象的片段:

    <!-- Pages -->
    <object id="BasePage" type="MyApp.UI.BasePage, MyApp.UI" abstract="true" singleton="false" scope="session" />    

    <object id="StandardPage" parent="BasePage" abstract="true" singleton="false" scope="session">
        <property name="MasterPageFile" value="~/Layouts/Site.master"/>
    </object>  
    <object type="~/Pages/Manager.aspx" parent="StandardPage" singleton="false" scope="session"/>

我知道有 100 种方法可以给这只猫剥皮,但最快的解决方案就是正确配置并继续行驶。是我的配置不稳定还是我的做法全错了?

编辑:常识开始发挥作用......IDictionary 不是线程安全的。

I'm having a bit of a problem keeping values from appearing in scope for all users. I'm not sure what exactly I may be doing wrong, but I'm at wits end. I'm thinking either I have my container objects scoped wrong or I'm passing the values along the WebContext. First, here's what I'm doing in code. This works, but it's showing details for the first user who logs into the site (note: my PageStore is just a property of type IDictionary that stores values to share to all user controls on the page):

using (BasePage page = (BasePage)(Spring.Web.UI.Page)this.Context.Handler) {
    if ((page.PageStore("LoginId") != null)) {
        this.Model.LoginId = Convert.ToString(page.PageStore("LoginId"));
    }
}

Second, I've tried different scopes for my objects but I'm coming up empty. Here is how my objects are currently configured in the container:

Snippet of my business tier configuration

 <!-- Transaction Management Strategy - local database transactions -->
    <object id="transactionManager"
          type="Spring.Data.Core.AdoPlatformTransactionManager, Spring.Data" scope="request">
        <property name="DbProvider" ref="MyProvider"/>
    </object>
    <tx:attribute-driven transaction-manager="transactionManager" />

    <!-- Data Access Objects -->
    <object id="DataAccess" type="MyApp.Business.DataAccess.DataAccess, MyApp.Business.DataAccess" abstract="true"  scope="request">
        <property name="AdoTemplate" ref="MyTemplate" />
    </object>

    <object id="ManagerDAO" type="MyApp.Business.DataAccess.ManagerDAO, MyApp.Business.DataAccess" parent="DataAccess" scope="request">
        <property name="TableName" value="vw_managers" />
        <property name="IsAllowedDeletion" value="False" />
        <property name="IsAllowedUniqueIdRetrieval" value="False" />
    </object>

     <!-- Services -->            

    <object id="SearchService" type="MyApp.Business.Services.SearchService, MyApp.Business.Services" scope="request">
        <property name="ManagerDAO" ref="UserDAO" />
    </object>

Snippet of my presentation layer:

<object id="BasePageModel" type="MyApp.Presentation.Models.BasePageModel, MyApp.Presentation" abstract="true"  scope="session"/>
<object id="ManagerModel" type="MyApp.Presentation.Models.ManagerModel, MyApp.Presentation" parent="BasePageModel"  scope="session"/>

<!-- Presenters-->    
<object name="ManagerPresenter" type="MyApp.Presentation.Presenters.ManagePresenter, MyApp.Presentation" singleton="false" scope="session">
    <property name="SearchSvc" ref="SearchService"/>
</object>

Snippet of my UI layer objects:

    <!-- Pages -->
    <object id="BasePage" type="MyApp.UI.BasePage, MyApp.UI" abstract="true" singleton="false" scope="session" />    

    <object id="StandardPage" parent="BasePage" abstract="true" singleton="false" scope="session">
        <property name="MasterPageFile" value="~/Layouts/Site.master"/>
    </object>  
    <object type="~/Pages/Manager.aspx" parent="StandardPage" singleton="false" scope="session"/>

I know there are 100 ways to skin this cat, but the fastest solution is to just get this configured properly and drive on. Is my configuration flaky or am I going about this all wrong?

Edit: Common sense is starting to kick in....IDictionary isn't thread safe.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

朱染 2024-12-09 16:44:28

解决了我的问题。将我的 Dictionary 对象切换为线程安全的 ConcurrentDictionary 对象并修复了我的范围问题。对于范围,我的业务层对象是单例,我的模型对象在会话范围内,我的视图(用户控件)和页面现在设置为请求范围,非单例。

Fixed my problem. Switched my Dictionary objects to thread safe ConcurrentDictionary objects and fixed my scoping issues. For the scopes, my business tier objects are singletons, my model objects are in the session scope, and my views(user controls) and pages are now set to the request scope, non-singletons.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文