What is the best way to reuse pages from one site to another?

I am developing a new ASP.NET website, which is actually a subset of the pages on another site that we just released. Two or three pages will require minor adjustments, but nothing significant.

The obvious answer is to simply copy all the code and markup files into a new project, make the above settings and review the work done. However, I am not interested in this at all because of the amount of duplicated code that it will create.

My next idea was to move the code for the pages (i.e. the code file) into a separate assembly, which can then be referenced from both sites. This is a bit inconvenient, because if you do not take the file with the designer, you get a lot of build errors related to missing controls. I don't think moving the constructor file is a good idea, though, since it will need to be restored every time the markup changes.

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

+3
source share
4 answers

You might want to take a look at the MVP pattern. Since you are probably using WebForms, it would be difficult to port MVC to ASP.Net, but you could pretty easily implement MVP in existing applications.

At a basic level, you move all the business logic to the Presenter class, which has a view representing some kind 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 define your interface for storing the atomic types you want to display:

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

Once they are defined, you can 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); } } 

As you can see, your code behind is currently very simple, so duplicating the code doesn't really matter. Since the core business logic terminates in the DLL anyway, you don’t have to worry about the possibility of exiting synchronization. Presenters can be used in multiple views, so you have high reuse and you can change the interface without affecting the business logic while you stick to the contract.

The same template can be applied to user controls, so you can get as modular as you need. This template also gives you the ability to unit test your logic without having to launch a browser :)

templates and methods the group has a nice implementation: WCSF

However, you do not need to use your infrastructure to implement this template. I know that at first this may seem a bit complicated, but it will solve many of the problems (in my opinion) that you are working on.

+2
source

Create custom controls (widgets) or templates to customize what you want to achieve.

Perhaps you can also achieve the same as CSS or JavaScript styles.

+1
source

Why not create custom controls (or custom controls) from the pages you want to share? Then you can reuse them on both sites.

0
source

What we use in our project (JSP, not ASP, but when it comes to creating and files, is this of course not a problem?) Is to have a base folder for shared files and then another (“instance”) folder with additional files and overwrite, and our build script (in ANT, Maven should also be fine) will first copy the base folders, and then based on the provided option, select which instance files will also be copied.

Thus, we can change the file in the database and apply it in all instances.

The problem is that changing the base file will not update the instance file that overwrites it, but at least you can make a process for these updates. Presumably, you can also use the SVN version (etc.) to note the build error, since the instance file is older than the base file, but we have not yet implemented anything so smart.

In addition, your internal code (Struts actions in our case) will lead to the processing of all cases, and not just to cases of a specific instance. But at least all the code is in one place, and the logic should be clear ("if (instance == FooInstance)" {doFooInstanceStuff (...);} ").

0
source

All Articles