Writing reusable c# code for ASP.NET websites
Asked Answered
U

4

9

I have been learning C# for the past few days for use with ASP.NET to create websites.

I am very new to C# but I have been thinking about how I should go about writing my code to make it as reusable as possible.

As a quick example, lets say I wanted to create a piece of code to check a users login details which I could just drop into another site at any time, and have it work with the data it gets given.

Remembering that I have no idea how I should layout my code to do this, this is the idea I came up with (I will keep it short with some kind of pseudo code):

First I create a class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Module {
    public class Login {
        public static bool check_login(string usernameCheck = "", string passwordCheck = "") {
            if(usernameCheck == "user" && passwordCheck == "password") {
                return true;
            }

            return false;
        }
    }
}

Then I would have an aspx page where the login form would go, for example:

<asp:Content ContentPlaceHolderID="column1" runat="server">
    <asp:TextBox ID="usernameInput" runat="server"></asp:TextBox>
    <asp:TextBox ID="passwordInput" runat="server"></asp:TextBox>
    <asp:Button OnClick="check_login" Text="Login" runat="server" />
</asp:Content>

And the code behind file would look like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Module {
    public partial class _default : System.Web.UI.Page {
        protected void Page_Load(object sender, EventArgs e) {

        }

        protected void check_login(object sender, EventArgs e) {
            if(Login.check_login(usernameInput.Text, passwordInput.Text)) {
                Response.Redirect("some other place");
            }
        }
    }
}

This works as expected, but what I want to know is:

  • Is there a better way to create reusable code?
  • How do you design your reusable code?

I'm sure there must be a better way for me to do this, but I just can't think of it on my own.

Unholy answered 18/11, 2010 at 15:39 Comment(0)
W
13

With regard to ASP.NET WebForms and reusability, it has to be said that the most common issue lies with putting too much logic into the code-behind files. Programmers, especially those new to ASP.NET WebForms, tend to put there parts of business logic and even database access code. After a while, it turns their application into a giant hard-to-maintain blurb.

Extract away your business logic into a separate layer. Access it from the code-behind. Just access, not implement it directly. Stay away from the database. Only the business layer should be allowed to talk to it.

You could even put your business logic and domain model entities into a separate class library. Then it's how it becomes reusable.

That's first simple considerations before you get your hands dirty with some serious coding.

Reusability techniques in .NET mostly revolve around shelving common code into class libraries to be used by a variety of applications.

Reusability as applied to web applications (ASP.NET) is usually achieved with UserControls (*.ascx) for the presentation part and exposing parts of the application via web services.

One could also mention moving the business logic into the database layer (putting it into stored procedures) but this one is commonly perceived as controversial these days.

Wiburg answered 18/11, 2010 at 15:46 Comment(1)
I agree! 3-tier or n-tiered development does a lot of the trick. The easy way for starters might be placing "modules" inside the "App_Code" and then just call the functions from there. Dont keep validation etc. in the code-behind. Only "call functions" from the actual page-codebehind.Amato
G
4

Maybe login is a bad example, because there are ASP.NET built-in ways to do this, but, in general, you are on the right track: Don't put the business logic that you might want to reuse into the Page itself, but rather into some classes -- this has the additional advantage of structuring your code in a more logical way: User-interface related stuff is in the ASPX page (and it's codebehind), program logic resides in separate classes. Eventually, you'll want to move some of those classes into a separate library project, which can be linked to by many different web applications.

If you want to reuse ASP.NET user interface elements, you have the possibility to write ASP.NET Server Controls, which you put into your library and use in your ASP.NET pages like this:

<asp:Content ContentPlaceHolderID="column1" runat="server">
    <custom:MyCustomLoginControl runat="server" 
                                 OnSuccessRedirectTo="myStartPage.aspx" />
</asp:Content>
Granvillegranvillebarker answered 18/11, 2010 at 15:47 Comment(0)
A
1

The best way I've found to re-use WebForms pages and code is to use the MVP pattern. We implemented MVP for some of our pages on our old website as described here, and when we moved to ASP.NET MVC, we found the process to be really simple.

Acanthus answered 18/11, 2010 at 15:48 Comment(1)
I should note that MVP just helps in what @Developer Art recommends: separate your business logic from the code behind code.Acanthus
C
0

There is a simple solution to code reuse in ASP.NET.

After researching, I found two solutions:

  • working with virtual directories, or
  • move the entire contents to the root directory.

Checking the two solutions, identified what really needs to be done: it is enough to just set the BIN folder of our project to target the root directory (C:\inetpub\wwwroot\bin).

Here's a simple example in the attached file: LibExemplo.zip.

Currently I use SharpDevelop which does not need lengthy installations or configurations.

Regards,

Reinaldo Fernando

Chemurgy answered 12/1, 2013 at 17:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.