Asp.Net inline (single file) and code-behind

When moving from classic ASP to Asp.Net, some developers took to place their server side code in a block at the top of their HTML al:

<%@ Import Namespace="MyDll" %>

<script runat="server">
    void Page_Load()
    {
    }
</script>

      

This one-page model has some advantages: Jeff Atwood describes , however in my experience I've seen almost all of the code placed in a separate code file lately (i.e. since VS 2008).

However, it turns out that a colleague strongly prefers a single file (inline) method over a separate code method.

What are the strengths and weaknesses of each approach? (I noticed that code collapse and #regions don't seem to be supported. Also the pages are getting quite long and there is no visual separation of the code on the client and server side now. Can you tell me which I prefer?)

I realize that variations on this question have been asked before, but I haven't seen anyone really disclose the specific pros and cons of each method.

EDIT

Thank you all for your thoughts provoking answers. I still hope for a list of the strengths and weaknesses of each method. What are the actual opportunities that everyone has (or doesn't have)?

+2


source to share


5 answers


Generally, when working in WebForms, the trend I've seen is to use code. A lot of * WebForms applications I've seen in the field have too much in their code entries, and separation is almost critical to understand all the logic.

However, in a well-designed application, where the UI does only the UI work and also transfers all the logic and heavy work to another layer of the application, a single file solution is often more elegant and simple enough to go through. In a sense, working with a single-file solution can - in the right hands - motivate better separation of concerns, because you don't want one file (which provides your interface) cluttering a bunch of business logic.

The ASP.NET MVC model uses one file by default. It also means emphasizing separation of concerns and good application design. (I don't know, from my point of view, if the ASP.NET MVC suite provides the concept of code. I haven't used it if it does.)



Ultimately, YMMV. Good developers tend to write good code, whether it uses the code or single file model. Bad developers tend to write bad code anyway.

*

Obviously not ALL!

+1


source


There is no question that code or mvc models are superior to almost anything you want to do. However, even today I am still using inline code for most of my pages. What for? Because there is one big scenario where inline scripts really shine.

If you have a lot of legacy ASP Classic code that you don't want to just throw away, including the deep nesting structure, it all lives in one big app, and you want to share that app with your asp.NET code, you can just embed asp pages. net inside your existing web filesystem and they just work.



This is similar to where your other developer comes from.

+5


source


Since codebehind is just a class, it has all the benefits like inheritance and interfaces. It also improves readability.

One page has mostly been replaced with mvc for applications that focus on outputting data instead of injecting business logic.

+4


source


Have you considered viewing ASP.NET MVC? This will enable you to overcome this dilemma in a very clean department.

+2


source


Inline code is procedural and there is no separation of concerns ...

One of the selling points of ASP.Net was code and server. Having inline code was considered bad. This changed when ASP.NET Mvc hit the scene - inline code is now "thighs" again.

If I had a choice and everything is equal using code-behind, this is the best approach. I try to keep as much logic / code out of the UI as possible.

Even using the code behind, even though it's a class, it can become a confusing mess. I found that using MVP or some variant of MVC with web forms makes development more maintainable in the long run.

-1


source







All Articles