从其他类访问页面/页面上的控件的最佳实践?

发布于 2024-11-08 08:03:05 字数 1388 浏览 1 评论 0原文

我正在重构一些代码,并且已经开始这样做的实践:

protected void Page_Init(object sender, EventArgs e)
{
    Logger.Info("Page Initialization.");
    //Provides highlighting/docking functionality at the start, but overshadows controls in more complex scenarios.
    RadDockZone1.Visible = (RadControlStates.SplitterStates.Count == 0);

    ControlRegeneration.RegenerateReportMenu(lstBxHistorical, lstBxCustom);
    ControlRegeneration.RegeneratePaneChildren(RadPane2);
    ControlRegeneration.RegenerateDockZones(Page);
    ControlRegeneration.RegenerateDocks(RadDockLayout1, RadDock_Command, UpdatePanel1);
}

我想知道将 Page 和 Page 控件传递给类似这样的其他函数是否是一个好习惯。

我正在考虑创建一个单例来保存对相关页面控件的引用,然后通过该实例访问控件。

就像...

public class DashboardPageControlsRepository
{
    private static readonly DashboardPageControlsRepository instance = new      DashboardPageControlsRepository();

    private DashboardPageControlsRepository() { }

    private Control myPanel;

    public static DashboardPageControlsRepository Instance
    {
        get { return instance; }
    }

    public void SetPageState(Page page)
    {
        myPanel = Utilities.FindControlRecursive(page, "UpdatePanel1")
    }

    public Control Panel
    {
        get { return myPanel; }
    }
}

然后,在页面初始化期间,在发生任何事情之前,我会去获取所有控件——允许我通过这里访问它们,而不是将它们传递下去。

关于如何处理这个问题有什么想法吗?

I'm refactoring some code and I've gotten into the practice of doing this:

protected void Page_Init(object sender, EventArgs e)
{
    Logger.Info("Page Initialization.");
    //Provides highlighting/docking functionality at the start, but overshadows controls in more complex scenarios.
    RadDockZone1.Visible = (RadControlStates.SplitterStates.Count == 0);

    ControlRegeneration.RegenerateReportMenu(lstBxHistorical, lstBxCustom);
    ControlRegeneration.RegeneratePaneChildren(RadPane2);
    ControlRegeneration.RegenerateDockZones(Page);
    ControlRegeneration.RegenerateDocks(RadDockLayout1, RadDock_Command, UpdatePanel1);
}

I'm wondering if it is good practice to pass Page and Page controls to other functions like this.

I was considering creating a singleton that will hold references to the relevant page controls, and then accessing the controls through that instance instead.

Something like...

public class DashboardPageControlsRepository
{
    private static readonly DashboardPageControlsRepository instance = new      DashboardPageControlsRepository();

    private DashboardPageControlsRepository() { }

    private Control myPanel;

    public static DashboardPageControlsRepository Instance
    {
        get { return instance; }
    }

    public void SetPageState(Page page)
    {
        myPanel = Utilities.FindControlRecursive(page, "UpdatePanel1")
    }

    public Control Panel
    {
        get { return myPanel; }
    }
}

Then, during page init before anything happens I would go and grab all my controls -- allowing me to access them through here rather than passing them down.

Any thoughts on how to handle this?

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

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

发布评论

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

评论(1

梦途 2024-11-15 08:03:05

以这种方式创建单例的问题是静态实例将在 AppDomain 的生命周期内存在(直到它被回收)。最重要的是,访问单例的多个请求将尝试独立改变单例的状态。

除了作为控制引用的容器之外,该存储库还提供哪些服务?

我要提到的另一件事是,不要过多地专门化您的方法,您应该考虑方法设计的最少需要类型方法,例如您当前拥有:

public void SetPageSize(Page page)

其中该方法仅真正感兴趣访问 System.Web.UI.Control 类型的 Controls 集合。您可以将该方法重新定义为:

public void SetPageSize(Control control)

The problem with creating singletons in this manner is that the static instance will exist for the lifetime of the AppDomain (until it is recycled). On top of that, multiple requests accessing the singleton will be attempting to mutate the singleton's state independently.

What services would this repository offer other than as a container for control references?

The other thing I would mention, is don't specialise your methods too much, you should consider the least required type approach to method design, e.g. you currently have:

public void SetPageSize(Page page)

In which the method is only really interested in accessing the Controls collection of the System.Web.UI.Control type. You could redefine the method as:

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