Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

General NEWBIE advice: How much C# on ASP.NET screens?

Posted on 2006-04-12
9
Medium Priority
?
295 Views
Last Modified: 2010-04-16
Dear Experts,

Hi.  My company is switching to C# and ASP.NET from Java and ASP (old).  I’m the Java guy, and I’m keenly interested in how much we can bring the many excellent features of an object-oriented language like C# onto a web page driven by ASP.NET.  (I’m basically a middle-ware guy with almost no GUI experience.)

I understand that ASP.NET allows both embedded C# and a separate layer of C#.  I’m trying to understand how far we could go with that.  I’m not so much interested in the mechanics at this point.  Instead, I’m just looking for general guidance.   If anyone wants to either answer directly, or point me to stuff I could read, that would be greatly appreciated…

So I have two examples.  

EXAMPLE #1:  A SIMPLE “CUSTOMER” DATA BEAN

I’m not sure if C# calls it this, but a data bean is a very simple class that exists basically just to hold data.  So a customer bean would store Name, Address, Phone Number.  It would look like this (simplified):

public class DataCust
{
  private String custName;
  private String custAddress;
  private int custPhone;
}

Currently, in ASP (old), our GUI needs to create three separate “local variables” to store Name, Address and Phone Number.  And then, when the user clicks elsewhere, three more “session variables” are needed to store that information—because the web site is “stateless”.

One great advantage of the bean is that you declare it once, and then anyone can pass it around and use it.  So ideally, the GUI would pass this bean to my C# middleware, with no translation needed.

So QUESTION #1:

If we could replace those ASP “local variables” and “session variables” with this customer bean, would it PERSIST as the user clicked elsewhere?  In other words, could this customer bean (which is an “object” from an OOP perspective) do what ASP session variables do: persist as long as the GUI user is “present”?


EXAMPLE #2:  A SOPHISTICATED “BEAN HANDLER”

Basically, if you can have C# running in ASP.NET, how sophisticated can it get?  

For example, I have existing code that I could C#-ize, which does a lot very handy things with beans, such as load them into various data structures automatically, translate rows from a table into beans, etc.  Basically, it’s a full-fledged class—not some little widget.  And it could easily and safely do a lot of things that take a lot longer to do in a language like VBscript.

So QUESTION #2:

In ASP.NET, could something like that “bean handler” exist more or less as is?  (Please note the “bean handler” is just an example of “some sophisticated C# class…”).  Would I be able to “instantiate” it with ASP.NET, tell it to go grab some data, etc.  More importantly, could that “bean handler” (which is ALSO an “object” from an OOP perspective) PERSIST as long as the GUI user is “present”?


Thanks a lot!
--BrianMc1958




0
Comment
Question by:BrianMc1958
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 600 total points
ID: 16436309
1) Sort of. Because the ASPX page only exists for the lifetime of the call then you need to store it somewhere. However, Session isn't the only option. You could store the data in the pages ViewState, or the Applications Cache. The advantage with C# of course is that you can store the object/stuct in the Session/Application/ViewState/Cache, as an object rather than several unrelated variables.

2) You can do this. You could create libraries that exist externally to your ASPX pages that could perform common tasks for you. You could, if you had the time, create your own Portal type framework, so that your application works as an Application rather than just a set of disperate pages.

If you wanted to you could create part of your app as either a WebService or windows service on the server that runs independently of the actual website, but provides services to your site.


Essentially C# is a full blown programming language, you can mix and combine anything you want, the only limit is your imagination.
0
 

Author Comment

by:BrianMc1958
ID: 16436613
Dear carl,

Thanks.  That's great.  Regarding this:

>>2) You can do this. You could create libraries that exist externally to your ASPX pages that could perform common tasks for you.

Would those "libraries" be essentially "static", or can they be full-blown OOP objects that also hold their own data?  Going back to my Example #2, I would typically have several "bean handlers" in existence at once.  Each would have identical logic, but would be storing DIFFERENT data.  Is that possible?

Thanks,
BrianMc1958

0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 16436857
Absolutely. But again, depending on where you are creating your objects will determine whether they can stay resident in memory (i.e. as part of an external service) or whether they would have to be stored the Application or Session objects.

Objects stored in the Application are global to the entire app and remain resident for the lifetime of the application.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 16438384
in C# this:

public class DataCust
{
  private String custName;
  private String custAddress;
  private int custPhone;
}


would look like:

public class DataCust
{
  private string custName;
  private string custAddress;
  private int custPhone;
 
  public string CustName
  {
        get
           {
                return custName;
            }
         set
            {
                custName = value;
             }
   }
  public string CustAddress
  {
        get
           {
                return custAddress;
            }
         set
            {
                custAddress = value;
             }
   }
  public int CustPhone
  {
        get
           {
                return custPhone;
            }
         set
            {
                custPhone = value;
             }
   }
}

though I question whether custPhone would ever be a int (more likely a string)


AW
0
 
LVL 12

Assisted Solution

by:AGBrown
AGBrown earned 1400 total points
ID: 16438412
Possibly worth having a look at this Url to describe the 6, 7, 8, 9 .... (depending on what you are looking for) methods of state persistence in ASP.NET.
http://msdn.microsoft.com/msdnmag/issues/03/04/ASPNETUserState/
and (as an example of what MSDN offers)
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnpag/html/DIforWC-CH05.asp

I don't think you need to worry about C# being an OO language, I've watched a Java programmer recently start on C# with me and actually be "impressed" (might have had low expectations :-) ). It is a fully blown OO language. You have objects, and you can load instances of those objects into memory to exist side-by-side. As a trite example:

public class BeanHandler()
{
   public Bean(get;set;)
}

public void ButtonClickCode()
{
   // get beans from database
   BeanHandler bh1 = new BeanHandler(myBean);
   BeanHandler bh2 = new BeanHandler(myBean2);
   Session["bh1"] = bh1;  // stores the first one in the session for when the user's page comes back next time
   Session["bh2"] = bh2;  // stores the second one in the session for when the user's page comes back next time
}

But this doesn't really even begin to explain everything that C# does. You might be more inclined to ask "what does it not do", and given that you can implement static classes, partial classes, Interfaces, serialize and deserialize "beans" to send over the wire, do singleton patterns ... the answer is really that, just like Java, it does everything. If it doesn't, you can generally make it do it with your own objects.

And then we can continue, we have lists, arrays, lists that act like arrays and lists, generics so that you only have to write a specialist BeanHandler once and it will take any kind of Bean.

In terms of web development, I can't compare it to Java based development as I haven't done the latter, but it will do anything from a simple 2-tier application to service-based rich-client enterprise applications. Now you also have ClickOnce to simplify application deployment as well.

When you talk about "embedded" and "seperate layer" you are really touching on the RAD for my-first-site all the way to self-contained assemblies that you can load into your application and reuse in all your physical layers (define your beans in one assembly and use that assembly in a 2-tier website, or in the web service that drives the database and in the presentation layers).

Touching on the idea of storing "bean handlers" in the state bags, well you could also write classes to do asynchronous jobs with databases, external services etc and store them in the state to give long running jobs some kind of progress bar on the client, or just to improve performance in general. .NET 2.0 now supports AJAX controls (.NET 1.1 didn't) so it has now caught up on that one.

As carl_tawn says "the only limit is your imagination".

Andy
0
 

Author Comment

by:BrianMc1958
ID: 16439461
To AGBrown:

Yikes!  That was an ANSWER!  I didn't mean to imply that C# was less robust than Java.  (I understand they are nearly the same language...).  Instead, I'm asking if the .NET environment will allow us to leverage the many advantages of an OOP language like C# within a browser-based ASP.NET.  This is what our current combination of Java and (old) ASP does NOT do.  Nothing I develop in Java can be re-used or shared within (old) ASP at all.  They are two entirely separate worlds.  I'm trying to see how well C# and ASP.NET might bridge those worlds.

Specifically, regarding your example:

public void ButtonClickCode()
{
   // get beans from database
   BeanHandler bh1 = new BeanHandler(myBean);
   BeanHandler bh2 = new BeanHandler(myBean2);
   Session["bh1"] = bh1;  // stores the first one in the session for when the user's page comes back next time
   Session["bh2"] = bh2;  // stores the second one in the session for when the user's page comes back next time
}

Will the "ASP" part of ASP.NET be able to access that same object and use it's methods?  Is there something analogous in ASP.NET to OOP getters and setters like

myLocalCustName = myBean2.getCustName ?


To Everybody:

Although I've certainly got more than 500 points worth of excellent responses by now, I'd like to leave this question open a little longer, as it's very important to my little world...  I hope you don't mind.  If anyone else has any comments, I'd love to hear them...

--BrianMc1958  
0
 
LVL 12

Accepted Solution

by:
AGBrown earned 1400 total points
ID: 16446441
Not at all, I didn't take it to mean that. In my opinion C# is not necessarily _better_ than any other language, and ASP.NET is not necessarily better than any other web framework. Certainly in terms of OO as far as I can work out Java and C# are similar, but maybe come from slightly different viewpoints. I may also have got a little carried away :-) (and didn't really explain how ASP.NET _is_ OO)

Basically, almost _anything_ you can think of OOP wise, and C# wise, can be leveraged in ASP.NET due to the architecture of the framework. I think I'll struggle to give the perfect answer due my the lack of Java experience. The boundaries between rich clients, smart clients and thin (web) clients have really been blurred in ASP.NET, and all are completely OO.

As an example of how ASP.NET works, each control on a page is treated as an object, and so is the page itself. During a request from a client browser to the server:
1) Client requests page
2) IIS authenticates and determines the request belongs to ASP.NET, passes it to the ASP.NET worker process
3) aspnet_wp determines that the request is of a particular page type. It instantiates an instance of this page, and then all the controls within it in an object tree. This happens each time. If the request is as a result of a button click, or such, then the ViewState is used to rebuild the previous state of each object in the tree. I tend to talk about "disconnected instances" with this, but that is really just the same as serializing any object and passing it between service layers, reconstructing it at the other end, and then doing the same in reverse.
4) aspnet_wp determines the type of request (i.e. new request, event handler). So in the case of our button handler, we are in effect using a server-side method defined within the page object to react to the 'Click event'. Already we are seeing OOP in the form of the Observer pattern. Like normal OOP you can have multiple observers for one event. This extends to any and all objects and events on the page.

After that, other things happen depending on the code that you have written and the response gets posted back to the client. At all stages during the process you are dealing with instances of objects/types (without getting into any particular semantics). This foundation of pages and controls as objects in an object graph on the server-side means that it all acts as an OO framework, and not just ASP with some vbscript in it. This naturally extends to your code on top of the framework. It also extends to custom HttpHandlers which are not based on the Page object, but that you can use to generate and return, for example, images, or act as a service interface for Flash.

So with the button click code, that _is_ the ASP part of ASP.NET; ASP.NET has deliberately fuzzied the boundaries between the handlers for requests and the code behind it. Your code behind could just be script in effect without using any of the OO advantages, but you, as an OO programmer will be able to go in and implement whatever level of enterprise architecture you want. Importantly, you can program almost as if you are using an OO MVC-based windows application (with some fundamental state-persistence differences, and single-direction communication, but arguably on a broad level it is the same).

So when you think about ASP.NET don't think about it as anything other than C# (or VB.NET or VC++); you can use _all_ of any language to build your application. The ASP bit is just the wires that connect C# with IIS and build your objects from requests, and translate them back to http responses afterwards. Though, of course, that isn't the limit, there are also SOAP web services, and remoting.

This goes for your question about getters and setters as well. Getters and setters are not a property of ASP.NET, which is the framework. They are a property of C# (in your case). I think you probably meant this, but I would work hard to define and maintain the semantics when your team moves to C# as it will avoid a lot of the confusion I experienced talking with our, now multilingual, Java guy.

In fact, this property is actually called "Properties" in C#. There are a few fundamental Java-C# differences, and this question about "getters and setters" rings a bell; I think it's the same question our Java guy asked when he picked up C#. The C# "property" encapsulates those getters and setters, so if I write:

public class Bean
{
      private string _CustName;
      public string CustName
      {
            get
            {
                  return this._CustName;
            }
            set
            {
                  this._CustName = value;
            }
      }
}

then your code becomes:
      myLocalCustName = myBeanInsance.CustName;
and to set the CustName property in an instance you do:
      myBeanInstance1.CustName = "foo";
      myBeanInstance2.CustName = "bar";
      
With the .NET 2.0 language versions these getters and setters can also be assigned different visibility, so you can have a private setter and a public getter. You can also just have one or the other.

One other Java-C# difference that tripped up our Java guy was the web forms 1.1 DataGrid (now the GridView in .NET 2.0). The DataGrid he had used in a Java windows form stored the dataset that had been used to fill it. This was not quite the same in the DataGrid for ASP.NET, and it took a while for us to both realise that that was where he was getting stuck.

Therefore my major piece of advice when porting the application would be to not try and translate the code line-for-line but to look at the individual cases that led to the blocks of code and ask how they can be done in ASP.NET. You will also then be able to take advantage of the features of the very latest framework release, and (from what I understand) very different state-persistence methods.

MSDN has an absolute encyclopedia of code on how ASP.NET works, how to implement things like MVC, considerations for porting from J2EE and Structs, patterns and practices in web applications ... it is worth mining it for a day to get an idea of what you will be working with. I won't list them all as I will just end up listing most of the ASP.NET section of MSDN, but to start with, take a look at:

http://msdn.microsoft.com/asp.net/?pull=/library/en-us/dnaspp/html/aspnet-pageobjectmodel.asp#aspnet-pageobjectmodel_topic2
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconcontrolexecutionlifecycle.asp

Your primary reference should be MSDN, it comes with Visual Studio and has answers to pretty much everything. I would also recommend a good set of reference books. Firstly one which goes over every facet of C#, such as the OReilly Programming C# book by Jesse Liberty (4th edition for .NET 2.0); you should devour that pretty quickly. Then some others, maybe "Developing Microsoft ASP.NET Server Controls and Components" to help develop custom reusable controls for your application (although that might be .NET 1.1 specific it is pretty good, and there may be a newer version).

Answering a question like this is tough, its better done in person. I hope that helps anyway.

Andy
0
 

Author Comment

by:BrianMc1958
ID: 16449468
Dear AGBrown,

It seems silly to award points in this case.  What if I paid tuition?

This has been extremely helpful.  I can't thank you enough for giving so much time (not to cogent thought) to my questions.  Better yet, it seems like after all that, the answer is YES.

Sincerely,
BrianMc1958
0
 
LVL 12

Expert Comment

by:AGBrown
ID: 16450113
Lol, splitting the points does me fine. Good luck. Anyway, you have given me food for content for my website, which I was otherwise pretty stumped for.
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question