重用一个网站在另一个网站上的页面的最佳方法是什么? [英] What is the best way to reuse pages from one website in another?

查看:43
本文介绍了重用一个网站在另一个网站上的页面的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个新的ASP .NET网站,该网站实际上是我们刚刚发布的另一个网站中的页面的子集.其中两三个页面需要进行一些细微调整,但没有什么大碍.

I'm developing a new ASP .NET website which is effectively a subset of the pages in another site we've just released. Two or three of the pages will need minor tweaks but nothing significant.

显而易见的答案是将所有代码和标记文件简单地复制到新项目中,进行上述调整,然后考虑完成的工作.但是,由于它会创建大量重复的代码,我一点也不热衷.

The obvious answer is to simply copy all of the code and markup files into the new project, make the aforementioned tweaks, and consider the job done. However I'm not keen on this at all due to the amount of duplicated code it will create.

我的下一个想法是将页面的代码(即代码隐藏文件)移动到一个单独的程序集中,然后可以从两个站点引用该程序集.这有点尴尬,但是就好像您不带走设计器文件一样,您会遇到很多与缺少控件有关的构建错误.我认为移动设计器文件不是一个好主意,因为每次更改标记时都需要重新生成它.

My next idea was to move the code for the pages (i.e. the code-behind file) into a separate assembly which can then be referenced from both sites. This is a little awkward however as if you don't take the designer file with it, you get a lot of build errors relating to missing controls. I don't think moving the designer file is a good idea though as this will need to be regenerated each time the markup is altered.

有人对清洁解决此问题有任何建议吗?

Does anyone have any suggestions for a clean solution to this problem?

推荐答案

您可能想看一下MVP模式.由于您可能正在使用WebForms,因此很难迁移到ASP.Net MVC,但是可以很容易地在现有应用程序中实现MVP.

You might want to take a look at the MVP pattern. Since you are probably using WebForms it would be hard to migrate to ASP.Net MVC, but you could implement MVP pretty easily into existing apps.

在基本级别上,您需要将所有业务逻辑移到具有一个表示某种接口的View的Presenter类中:

On a basic level you would move all the business logic into a Presenter class that has a View that represents some sort of interface:

public class SomePresenter
{
    public ISomeView View{get; set;}

    public void InitializeView()
    {
        //Setup all the stuff on the view the first time

        View.Name = //Load from database
        View.Orders = //Load from database
    }

    public void LoadView()
    {
        //Handle all the stuff that happens each time the view loads
    }

    public Int32 AddOrder(Order newOrder)
    {
        //Code to update orders and then update the view
    }
}

您将定义您的界面以容纳要显示的原子类型:

You would define your interface to hold the atomic types you want to display:

public interface ISomeView
{
    String Name {get; set;}
    IList<Order> Orders{get; set;}
}

一旦定义了这些,您现在可以简单地以您的形式实现该接口:

Once those are defined you can now simply implement the interface in your form:

public partial class SomeConcreteView : System.Web.UI.Page, ISomeView
{
    public SomePresenter Presenter{get; set;}

    public SomeConcreteView()
    {
        Presenter = new SomePresenter();

        //Use the current page as the view instance
        Presenter.View = this; 
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            Presenter.InitializeView();     
        }

        Presenter.LoadView();
    }

    //Implement your members to bind to actual UI elements
    public String Name
    {
        get{ return lblName.Text; }
        set{ lblName.Text = value; }
    }

    public IList<Order> Orders
    {
        get{ return (IList<Order>)ordersGrid.DataSource; }
        set
        {
            ordersGrid.DataSource = value;
            ordersGrid.DataBind();
        }
    }

    //Respond to UI events and forward them to the presenter
    protected virtual void addOrderButton_OnClick(object sender, EventArgs e)
    {
        Order newOrder = //Get order from UI
        Presenter.AddOrder(newOrder);
    }
}

如您所见,您背后的代码现在非常简单,因此代码重复并不是什么大问题.由于核心业务逻辑全部封装在某个地方的DLL中,因此您不必担心功能不同步.演示者可以在多个视图中使用,因此您具有很高的重用性,并且只要遵守合同,就可以自由更改UI而不影响业务逻辑.

As you can see, your code behind is now extremely simple so code duplication is not a big deal. Since the core business logic is all wrapped up in a DLL somewhere, you don't have to worry about functionality getting out of sync. Presenters can be used in multiple views, so you have high reuse, and you are free to change the UI without affecting the business logic as long as you adhere to the contract.

相同的模式也可以应用于用户控件,因此您可以根据需要获得模块化的功能.这种模式还为您提供了无需运行浏览器即可对逻辑进行单元测试的可能性:)

This same pattern can apply to user controls as well, so you can get as modular as you need to. This pattern also opens up the possibility for you to unit test your logic without having to run a browser :)

模式和实践组对此具有很好的实现: WCSF

The patterns and practices group has a nice implementation of this: WCSF

但是,您不必使用他们的框架来实现此模式.我知道乍一看这可能有些令人生畏,但它可以解决您遇到的许多问题(在我看来).

However, you don't have to use their framework to implement this pattern. I know this may look a little daunting at first, but it will solve many of the problems (In my opinion) you are running into.

这篇关于重用一个网站在另一个网站上的页面的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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