解耦视图、表示和 ASP.NET Web 窗体

发布于 2024-08-27 12:40:08 字数 714 浏览 11 评论 0原文

我有一个 ASP.NET Web 窗体页面,演示者需要用控件填充该页面。这种交互对页面生命周期有些敏感,我想知道它是否有一个我不知道的技巧。

我想对整个事情保持实际,但不妥协可测试性。

目前我有这个:

public interface ISomeContract
{
    void InstantiateIn(System.Web.UI.Control container); 
}

此合同依赖于 System.Web.UI.Control,我需要它能够使用 ASP.NET Web Forms 编程模型执行操作。但视图和演示者都可能不了解 ASP.NET 服务器控件。

我该如何解决这个问题?如何在具体视图中使用 ASP.NET Web Forms 编程模型,而不在合同程序集中采用 System.Web.UI.Control 依赖项?

为了澄清一点,这种类型的界面都是关于 UI 组合(使用 MEF)的。它在整个框架中都是已知的,但实际上只是从具体视图中调用。具体的视图仍然是对 ASP.NET Web Forms 唯一了解的东西。然而,那些表示 InstantiateIn(System.Web.UI.Control) 的公共方法存在于我的合约程序集中,这意味着对 ASP.NET Web 窗体的依赖。

我一直在考虑一些双重调度机制甚至访问者模式来尝试解决这个问题,但我还不知道我想要朝哪个方向发展,我真的很希望对此事有一些意见。

I have an ASP.NET Web Forms page which the presenter needs to populate with controls. This interaction is somewhat sensitive to the page-life cycle and I was wondering if there's a trick to it, that I don't know about.

I wanna be practical about the whole thing but not compromise testability.

Currently I have this:

public interface ISomeContract
{
    void InstantiateIn(System.Web.UI.Control container); 
}

This contract has a dependency on System.Web.UI.Control and I need that to be able to do things with the ASP.NET Web Forms programming model. But neither the view nor the presenter may have knowledge about ASP.NET server controls.

How do I get around this? How can I work with the ASP.NET Web Forms programming model in my concrete views without taking a System.Web.UI.Control dependency in my contract assemblies?

To clarify things a bit, this type of interface is all about UI composition (using MEF). It's known through-out the framework but it's really only called from within the concrete view. The concrete view is still the only thing that knows about ASP.NET Web Forms. However those public methods that say InstantiateIn(System.Web.UI.Control) exists in my contract assemblies and that implies a dependency on ASP.NET Web Forms.

I've been thinking about some double dispatch mechanism or even visitor pattern to try and work around this but I don't yet know in which direction I want to go and I would really like some input on the matter.

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

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

发布评论

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

评论(4

稚气少女 2024-09-03 12:40:08

不知道访客会如何解决这个问题。但是为什么不让您的合同看起来像这样:

public interface ISomeContract
{
    void InstantiateIn(IControl container); 
}

使用 IControl 实现,可能在另一个程序集中以保持您的合同程序集干净,它包装了 ASP.NET System.Web.Control,例如:

public class AspnetControl : IControl
{
    public AspnetControl(System.Web.Control control) { }

    // IControl members that dispatch to control
}

尽管最终 IControl 很可能会最终看起来非常像 System.Web.Control(因此一开始就失去了抽象它的意义),它仍然非常易于测试,并且您的视图和演示者不必了解 ASP 。网。

Not sure how a visitor would solve the problem. But why not have your contracts look like this:

public interface ISomeContract
{
    void InstantiateIn(IControl container); 
}

with an IControl implementation, possibly in another assembly to keep your contract assembly clean, that wraps over the ASP.NET System.Web.Control, like:

public class AspnetControl : IControl
{
    public AspnetControl(System.Web.Control control) { }

    // IControl members that dispatch to control
}

Although there's a high likelihood that eventually IControl would end up looking very much like a System.Web.Control (and hence defeat the point of abstracting it in the first place), it'd still be very testable, and your view and presenters won't have to know a thing about ASP.NET.

倾`听者〃 2024-09-03 12:40:08

将合约与 Web 控件解耦的一种方法是使用一个单独的编写器来处理从 ISomeContract 获取信息并将其放置在 Control 容器中。它可以驻留在引用协定程序集和 System.Web 的程序集中。

One way you can decouple your contract from your web control is to have a separate writer that handles getting the information from ISomeContract and places it in your Control container. This could reside in an assembly that references both the contract assembly and System.Web.

|煩躁 2024-09-03 12:40:08

我一直在阅读有关敏捷技术、TDD、单元测试、实体、设计模式的内容,并且感到完全无力弥合从所有这些精彩理论到 ASP.NET WebForms 的差距。

今天早些时候,我再次尝试寻找此问题的解决方案,并找到了这篇文章:

这是一本书的摘录,我认为这本书可以解决我所有的问题,但本章实际上只是对在 Web 表单中实现 MVP 模式的可能性,最后说它并不实用。据我所知,本书的其余部分是关于测试 ASP.NET MVC 的。

还有一个新项目,旨在将人们的爱带回 webforms 平台:

I have been reading about agile techniques, tdd, unit testing, solid, design patterns and felt utterly powerless to bridge the gap from all this wonderful theory to asp.net webforms.

I had another go at trying to find a solution to this problem earlier today and found this article:

It is an excerpt from a book which I thought would solve all my problems but this chapter is really just an introduction to the possibilities of implementing the MVP pattern in webforms and it concludes with saying that its not really practical. The rest of the book is about testing asp.net MVC as far as I gathered.

There is also this new project which aims to bring the love back to the webforms platform:

美人骨 2024-09-03 12:40:08

在“普通”ASP.NET Web 表单中,您的页面/用户控件在技术上“负责”处理,并将其生命周期强加于代码。页面就是演示者和视图,无论您是否喜欢。

在这种环境中实现 MVP 模式的大多数尝试只是给已经过于复杂的环境增加了额外的复杂性!假装另一个类是 Presenter 只是......假装。 (在 MVC 网站中,控制器确实控制着代码,而视图则不然,所以这个论点不再适用。)

我的建议是让视图管理它自己的生命周期,并让它调用存储库和模型用于检索/保存数据和调用命令的类。

在这种方法中,模型类不需要了解有关 System.Web 的任何信息。这些相同的模型类可以在未来的 MVC 网站或 Silverlight 中使用,或作为 WPF 应用程序的 Web 服务,或嵌入到 WPF 应用程序中等。由视图决定如何实现(使用控件)从模型获得的响应/数据。

如果您正确地将模型类设置为支持依赖项注入,则可以根据需要对模型类进行测试。

希望有帮助!

In "normal" ASP.NET web forms, your page/user control is technically "in charge" of the processing, and imposes its life-cycle on the code. The page is the Presenter and the View, whether you like it or not.

Most attempts to implement the MVP pattern in this environment simply add excess complexity to an already overly complex environment! Pretending that another class is the Presenter is just that... pretending. (In MVC web sites, the Controller truly does control the code, and the View does not, so this argument no longer applies.)

My recommendation is to let the view look after its own life-cycle, and let it invoke Repositories and Model classes to retrieve/save data and invoke commands.

In this approach, the Model classes do not need to know anything about System.Web. These same Model classes could then be used in future MVC web sites, or Silverlight, or as a web service for WPF applications, or embedded in a WPF application, etc. It is up to the View to determine how to implement (using Controls) the response/data it gets from the Model.

The Model classes can be tested as much as you like if you correctly set them up to support dependency injection.

Hope that helps!

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