|
|||||||
New to SitePoint Forums? Register here for free!
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
SitePoint Member
Join Date: Nov 2009
Posts: 17
|
I Know PHP. Who is an expert @ .NET?
To start, I know PHP. I can do some pretty remarkable things with it. CMS's, GD libaries, multi-level templating with real-time page compiling...I'm sharp and very efficient with it.
Out of necessity, for my current job, I've got to start doing programming in ASP. My first impressions of it are that I don't like it. The IIS is - worst nightmare - like WINDOWS. The DOTNET compiling program is feasting on 2 gigs of space on my hard drive. Ouch. PHP has some great features - whatever you include, inline or not, is accessible by your HTML page. In DOTNET, it looks like I have to take variables, assign them to labels, and only then can I use the label on the outputted aspx page. PHP also has associative arrays. For any DOTNET programmers that would consider themselves experts, what are some shortcuts and pointers on efficient coding? Remember back to when you were first learning it, slammed your head against a wall (repeatedly), and eventually went - OH!!!! - THIS IS THE WAY TO DO THAT! I know, in PHP, I would say things like, -save your database login file separately, link to it with an "include." -Learn associative arrays, they are the way to go. -Design your page in HTML and piece in small bits of php using <?php echo ?> - not design your page in PHP and piece in bits of HTML. -Use shorthand If statements. Something that just seems very inefficient to me is that I have to assign a variable to a label to be able to write it to an aspx page. Is this the correc thinking? I perform a query to this CRM system called Salesforce, and then I have to assign each field of this object to a label, and then I can output the label on the aspx page. Is there no way to make that object global and just <% Response.Write %> that value? But even so, if I had to do that (create labels for EVERYTHING), in PHP I would query the object, and then assign each field to it's own array key in an associative array. for($x = 0; $x < count($records); $x++) { foreach($records[$x]->fields as $field) { $labels[$field->name] = $field->value; } } It's not correct, but the logic's something you'd likely understand. Just any tips on how to go f/PHP to ASP DOTNET, and develop efficient programming habits, I would really appreciate. |
|
|
|
|
|
#2 |
|
Community Advisor
![]() ![]() Join Date: May 2003
Location: Washington, DC
Posts: 9,135
|
First, you might want to check out ASP.NET MVC as that is alot closer to the PHP model, especially if you have worked with any of the PHP MVC frameworks.
The big picture things to keep in mind: 1) Unlike PHP, .NET apps have a lifetime of far longer than a single Http request. So lots of stuff one does in php (like includes) don't make sense in .NET 2) Unlike PHP, .NET is strongly typed through and through. This means associative arrays are not necessarily the way to go, especially if you are working with one type of object. 3) Really an extension of #2, but NET is object oriented from the get go. There is no global namespace, and every method has to be a member of an object. Executive summary: get PHP out of your mind and learn to embrace the .NET way of doing things. Now, for your more tactical examples: 1) Configuration management: Look into the ConfigurationManager object for methods of accessing configuration information. 2) Again, there are no associative arrays. Honestly, they are a bad thing. Better off having strongly typed, managable objects than loose bags of random objects. Now, there are a number of collections, most of which can be used as generics. Check out the Systems.Collection.Generic namespace for a good list. 3) This is where UserControls (ASP.NET webforms) or Partial Views (ASP.NET MVC) step in. 4) Pretty much the same as PHP, at least in C#: [condition] ? [statement if true] : [statement if false] If you are going to be doing alot of .NET stuff, I'd highly advise taking a course on fundamental object oriented programming. It will help alot of the framework make sense. Much more so than attempting to translate PHP concepts into .NET. |
|
|
|
|
|
#3 | |
|
SitePoint Guru
![]() ![]() ![]() ![]() ![]() Join Date: Sep 2004
Location: NY, USA
Posts: 710
|
Good summnary... All I can really echo (no pun intended), is to 'unlearn' your PHP ways of thinking, and approach .NET as a completely different world. I dwell in both worlds a lot. There is not much that translates easily from one to the other IMHO.
and this is an example of thinking that you might want to unlearn: Quote:
The learning curve is a bit steep at first crack. You really need to educate yourself about how .NET apps tick. Get a good beginners book, or go to http://www.asp.net/ and just start diving in. In no particular order here, are some concepts that you should start learning in depth from the start - to get moving in the right direction...
I could go on and on... I'm just giving some basic overview stuff that I thought would get you on a good path. You gotta start somewhere, and getting a foundation to understand the framework is really a must in order to make progress. On the other hand, simply trying to apply your familiar PHP techniques to a .NET web app will only lead to poor .NET practices, and feeling like it's too much work to achieve simple things. (Which it will be) I disliked .NET when I started with it for those same reasons (coming from a classic ASP / PHP mind-set). Until I eventually got up the learning curve, then I changed my tune. hope that is helpful. good luck ![]() |
|
|
|
|
|
|
#4 |
|
SitePoint Wizard
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Dec 2002
Posts: 1,015
|
As somebody who left php behind for .NET abot ten months ago, with some things left to learn, might I offer a bit of insight?
First, read the above posts. They are the people who answer my own questions! Second, maybe we should ease him into it, instead of dropping a load on him right off. Try this... 1) Create a new "Web Application" using c#. 2) Create a test class of some sort, perhaps something simple like a one method object that simple returns a string: namespace TestProject { public class Foo { public string Bar() { return "Hello"; } } } 3) Add an aspx page to the project, then delete the code behind and designer files. Change the actual page content to: <%@ Page Language="c#" %> <%@ Import Namespace ="TestProject" %> <% Foo f = new Foo(); Response.Write(f.Bar()); %> 4) Run it and watch "Hello" appear. Anyway, the point is, there are more ways to do things than what would seem obvious. You have many options available to you: the MVC framework, the WebForms famework, home rolled stuff, the more "script like" stuff (demonstrsated above). The world is your oyster. Go for it. Oh, and assoc arrays: we have them, plus more. And they are typed! Dictionary<string, object> for example to hold anonymous objects. Dictionary<string, string> for example to hold strings. Dictionary<string, int> for example to hold ints. Hope this helps ease you into it. |
|
|
|
|
|
#5 |
|
SitePoint Wizard
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: Cape Town, South Africa
Posts: 1,587
|
Well, what can I say that has not been mentioned already?
1. Put your connection string in the web.config file 2. Use some ORM like Linq2Sql, Entity Framework, nHibernate, but first learn the old fashion ADO.NET so that you understand the fundermentals. 3. Use databound controls. asp:Repeater, asp:GridView and bind them. 3 will replace for loops writing html in a PHP. You will def learn to appreciate C#/asp.net over PHP. I know I have. I now cringe everytime I have to work in PHP again. lol. Unless I use a framework such as Zend or something. Do not create an aspx page and delete the .cs file and remove refs to it. If you want to go that route... when adding the page, there is a check back to Keep code in a seperate file, just uncheck that. But I recommend keeping the codebehind as it is far less messy then having everything inline. And this is a much better way of doing it and a way of forgetting PHP. Have no code behind is gona confuse you with PHP as that is the way PHP works. If you have a string variable on your cs page and it is public, yes you can write it to the page with no lable: <%=stringName %> Visual Studio is the BEST IDE you will ever use. Ok, thats all I can think of for now, hope it helped a bit |
|
|
|
|
|
#6 | |
|
SitePoint Member
Join Date: Nov 2009
Posts: 17
|
Quote:
The public variables, classes, and the custom objects are going to be what I need to start using. |
|
|
|
|
|
|
#7 | |
|
SitePoint Member
Join Date: Nov 2009
Posts: 17
|
Quote:
Now, what are the basic basics...such as - every page you create needs a "public default class", or, you ALWAYS have to define these variables @ the start: public string blah blah, etc. That would help me get good code and dissect what I need to dissect, not just what is required by the language itself. |
|
|
|
|
|
|
#8 |
|
SitePoint Member
Join Date: Nov 2009
Posts: 17
|
|
|
|
|
|
|
#9 | |
|
SitePoint Wizard
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Dec 2002
Posts: 1,015
|
Quote:
There's a thing called "viewstate". Nice in theory. It stores the state of the objects on the page so it can redisplay what you saw before when you submit the form. In reality though, it's a real hog. Disable it on everything you don't absolutely need it for. If you aren't sure, test it. The second thing is that if you find more then one page doing the same thing, make a base page class for it. You can do this in a normal *.cs file. Just create a new class based off of System.Web.UI.Page. Then in the code behind for your actual page, change the page class inheritance to the class you just made. That should be good for starters. |
|
|
|
|
|
|
#10 |
|
SitePoint Member
Join Date: Nov 2009
Posts: 17
|
Getting up my post count to reply.
|
|
|
|
|
|
#11 | |
|
SitePoint Member
Join Date: Nov 2009
Posts: 17
|
Quote:
This sounds simple enough, right? - 1. Create a survey page in ASPdotNET, and submit the values by logging into a CRM system and updating information in their database. Now this is my monstrous code: Code:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using salesforce;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string ContactID = Request.Url.Query.Contains("cid") ? Request.QueryString["cid"] : "0036000000nkuWI";
#region Login_Check_QuerySTring
SforceService sfdc = new SforceService();
LoginResult lr = sfdc.login("support@inlandinc.com", "PunchUInTheEyefTPgQGRGJKU8dK5LrQb6VVXj");
sfdc.SessionHeaderValue = new SessionHeader();
sfdc.SessionHeaderValue.sessionId = lr.sessionId;
sfdc.Url = lr.serverUrl;
GetUserInfoResult userInfo = lr.userInfo;
#endregion
QueryResult crecords = sfdc.query("SELECT LastName, FirstName, Email, Phone, MailingCity, MailingState, MailingPostalCode, Project_Site__r.Office__r.Name, Project_Site__r.Office__r.Id FROM Contact WHERE Id = '" + ContactID + "'");
sObject[] contactresults = crecords.records;
Contact contactrecord = (Contact)contactresults[0];
FirstName.Text = contactrecord.FirstName;
LastName.Text = contactrecord.LastName;
City.Text = contactrecord.MailingCity;
State.Text = contactrecord.MailingState;
PostalCode.Text = contactrecord.MailingPostalCode;
Email.Text = contactrecord.Email;
Office.Text = contactrecord.Project_Site__r.Office__r.Name;
}
protected void Submit_Click(object sender, EventArgs e)
{
#region Create_New_Appt_for_Contact
Customer_Survey__c survey1 = new Customer_Survey__c();
survey1.Contact__c = Request.Url.Query.Contains("cid") ? Request.QueryString["cid"] : "0036000000nkuWI";
survey1.Employment__c = Employment.Text;
survey1.Age__c = int.Parse(Age1.Text);
survey1.Age__cSpecified = true;
survey1.Closing_Process_Rating__c = ClosingProcess.Text;
survey1.Education__c = Education.Text;
survey1.Income__c = Income.Text;
survey1.Comments__c = Comments.Text;
SforceService sfdc = new SforceService();
LoginResult lr = sfdc.login("support@inlandinc.com", "PunchUInTheEyefTPgQGRGJKU8dK5LrQb6VVXj");
sfdc.SessionHeaderValue = new SessionHeader();
sfdc.SessionHeaderValue.sessionId = lr.sessionId;
sfdc.Url = lr.serverUrl;
GetUserInfoResult userInfo = lr.userInfo;
sObject[] sv1 = new sObject[] { survey1 };
sfdc.create(sv1);
//sforce.sObject[] reserved = new sforce.sObject[] { reservation };
//sforce.SaveResult[] sr = sfdc.update(new sforce.sObject[] { contactrecord });
//sfdc.create(reserved);
//Server.Transfer("thankyou.aspx", true);
#endregion
}
}
And last, what I would like to do is catch a hidden value (or something similar) to detect when a form's been submitted, and say something like "thank you for your successful submission" on the form page. I just don't know how to do this. I know you hate me saying this, but in PHP I would pass a hidden value, check for that, and display HTML accordingly. I'd then disable form fields, since the form's already submitted. And as far as logins and variables, they'd just be defined inline, and the login would be a function. It's really help w/the overall logic that would put me in the right direction. Same thing I'd do w/someone coming f/square 1 to a PHP environment. |
|
|
|
|
|
|
#12 |
|
Community Advisor
![]() ![]() Join Date: May 2003
Location: Washington, DC
Posts: 9,135
|
As I said, MVC will be a lot closer to the PHP model you have been working with in general. For "included" functions, you need to look at creating your own custom classes to handle things. Then you just instantiate your objects to make use of the code. Remember you are dealing in a compiled application environment, not a linear web scripting environment.
How you would handle the "user has submitted successfully" is to check, in your handler, if the user did successfully submit then either show an appropriate control or redirect the response to the appropriate page. |
|
|
|
|
|
#13 |
|
SitePoint Guru
![]() ![]() ![]() ![]() ![]() Join Date: Sep 2004
Location: NY, USA
Posts: 710
|
I have to come back to the fact that you should spend some time learning the very basics of a .NET web page/app. Really until you do that you'll be fighting against the tide - with little/no understanding of what's really going on - or how you can leverage the framework even on a basic level.
http://msdn.microsoft.com/en-us/library/ms178472.aspx http://www.asp.net/get-started/ http://www.asp.net/learn/ http://quickstarts.asp.net/QuickStar...t/Default.aspx You'll probably want to separate out your user authentication to a custom object (class) and re-use that wherever necessary, and/or use web.config to handle all the heavy lifting of authentication for you, and leverage all that built-in functionality. Forms authentication is just one solution. http://msdn.microsoft.com/en-us/library/7t6b43z4.aspx Chances are most of the mundane stuff that you've coded in PHP is built straight into .NET, saving you major time and effort. You need to start learning that from the get-go. Trust me, it's worth it. Learning .NET best practices will cut your dev. time significantly once you get your feet under you. Once you get more advanced, it only gets better and better. For one, postbacks. You don't need a hidden field to figure out if your form was submitted, which is typical in most PHP. Csharp Code:
Simple enough... but not necessary... why? because .NET is event driven. You have a handler defined already for the form submission (Submit_Click). That will fire when the form gets submitted... and that is where you should to handle the postback, not in Page_Load. Put another way, you don't even need to check if a postback was made because Submit_Click will automatically fire when it does. Make sense? Every page request will fire off a series of events, some based on user interaction (like Submit_Click)... some native to each and every page request (such as Page_Load, which fires on EVERY page load, postback or not). Look here: http://msdn.microsoft.com/en-us/library/ms178472.aspx Honestly, it's well-worth it for you to put in some time to understand how this all works, before writing code or diving into more advanced things. Read those intros, read sample code, get a beginner's book... Whatever it takes. poster above suggest MVC and he's a good one to listen to... however, if you're a complete newbie - you'd be getting ahead of yourself without a solid foundation of how .NET apps even flow in the first place. IMHO that is the biggest hurdle for those coming from PHP - they want to start producing, without first learning the basics. This is not PHP - and you'll make it very much harder on yourself to make progress in .NET by ignoring that little fact. (You're doing it already) Learn to leverage the power of .NET the correct way, instead of figuring out work-arounds to avoid it... just because that's how you did it in PHP. Otherwise what's the point? I hope that helps you somewhat. Ask questions here to fill in any "holes" as you learn. |
|
|
|
|
|
#14 |
|
SitePoint Member
Join Date: Nov 2009
Posts: 17
|
I follow, I agree. Getting into the C# syntax, workflow, application birthing and life of the application is priority right now. Thanks.
|
|
|
|
![]() |
| Bookmarks |
«
Previous Thread
|
Next Thread
»
| Thread Tools | |
| Display Modes | |
|
|
|
All times are GMT -7. The time now is 13:32.












Linear Mode
