MVC Examples use of var - asp.net-mvc

Maybe I live in a bubble, or am just too new, but I was wondering if anyone else has noticed the heavy use of 'var' to declare variables instead of a specific type in many of the MVC examples by Microsoft? Is there a purpose for this, or is it the "in" thing to do now, or just personal style?

Jared Parsons talked about this in his blog I don't agree with his conclusions (as my comment there indicates), but this article should explain why it's used so much.

All MVC Videos are maded in speedy way(also not always they using Unit test or placing bussiness logic in controlers). This is not because they are bad programmers, or "var" is cool, but because they want to quickly show some cool new features, and typing full name twice is not what they want to show.

The answer I gave below (in italics) is incorrect but as I would assume it is an assumption that a lot of people will make I am leaving it in.
For a explanation on why it is wrong read:
Var is Not Object
WRONG ANSWER: Personally I think that var has a bad name due to it's poor use in languages like Visual Basic and Excel Basic.
At the end of the day everything in .NET is derived from object which is basically the same as declaring something as var.

All of the above.

Related

Creating the controller using MVC3

I'm trying to get a UI using MVC. I'm currently battling with MVC3. There does’nt seem to be much documentation.
Here’s what I’m thinking. I believe I need to create stored procs for each query. I’m creating the the LINQ to SQL for the model. I’m trying to figure out how to create the cotroller. Any suggestion on how to input data to the stored proc and return results to the view? The default controller classes dont seem to do anything
Am I doing this right? Or MVC2/MVC1 the easier way to go?
I'm new to MVC on .NET. Thanks in advance.
There is plenty of documentation, if you look for it... Also, http://www.asp.net/mvc is a good place to start.
What it seems like you really need documentation for is LINQ-to-SQL, not MVC, which is here: http://msdn.microsoft.com/en-us/library/bb425822.aspx
There's also a great ScottGu tutorial here: http://weblogs.asp.net/scottgu/archive/2007/05/19/using-linq-to-sql-part-1.aspx
The whole series is available here, including a PDF download: http://scottonwriting.net/sowblog/archive/2010/07/27/links-to-scott-guthrie-s-using-linq-to-sql-tutorials.aspx
I believe I need to create stored procs for each query.
You could do this but it might be an overkill. I would recommend you watching the tutorials here. Paragraph 5. covers tutorials about using Entity Framework for data access. Try them out and if you encounter some specific issues don't hesitate to ask a question by showing what you have tried and what didn't work for you.
If you're using Linq to SQL, you don't need to use Stored Procs at all.
If you want more information on MVC generally, try doing the NerdDinner example.

Missing Documentation: MVCContrib UI-Testing

Is there any documentation or example how to use MvcContrib.TestHelper.Ui and WatinDriver?
I haven't found any real documentation for it, but there are examples of its use in the source code for the CodeCampServer project.
You might also want to take a look at Chapter 20 "Full System Testing" in the book "ASP.Net MVC 2 in Action."
It's a really great look at a similar approach to what the WatinDriver is trying to accomplish.
Its discussion is also great in terms of designing your UI/HTML to be testable with tools like WatiN, and techniques to help make your browser testing less brittle.
EDIT: The WatiNDriver in use in the Code Camp Project source code isn't technically the one from MvcContrib.TestHelper, but I'd be surprised if it differs by more than 2 lines of code. Basically it looks like they borrowed the code directly from MvcContrib and added some of their own stuff on top of it.
The code in codecampserver is the test bed we used to create the MvcContrib source code. We have recently brought on a new committer to the MvcContrib project that is going to focus on this area of the codebase. Also, you are right on about the Mvc2 in Action book. We really followed that approach that is outlined in the book. Also, feel free to email the mvccontrib email list.. We do reply to that list, we do not follow the SO questions as much so keep that in mind.

Entity Framework -- How to set properties to special types

I'm new to MVC and Entity Framework, so this may be a relatively simple answer, but I've tried searching around so far and no luck.
I'm using the most recent versions of both tools to my knowledge (MVC 3 Beta and Entity Framework 4.0)
I'm just trying to set up a quick example. Logically, I'm dealing with events -- in this case, an OngoingServiceEvent and a FiniteServiceEvent, which both inherit from ServiceEvent. I have a Locations Entity, which has LongLatLocation and AddressLocation that inherit from it.
I would like to specify a location in ServiceEvent, and have it be of type Location, but the types seem limited to those in the database.
What's the course of action in this scenario? Is this what I want to do, or am I confused on the implementation of what I want?
Thanks in advance for any help you can give!
You're looking for a feature called Complex Types. Best answer I can give is the MSDN documentation.

ASP.NET web forms as ASP.NET MVC

I am sorry for possible misleading about the title, but I have no idea for a proper title.
Feel free to edit.
Anyway, I am using ASP.NET Web Forms, and maybe this isn't how web forms is intended to be used, but I like to construct and populate HTML elements manually. It gives me more control.
I don't use DataBinding and that kind of stuff. I use SqlConnection, SqlCommand and SqlDataReader, set SQL string etc. and read the data from the DataReader.
Old school if you like. :)
I do create WebControls so that I don't have to copy-paste every time I need some control, but mostly, I need WebControls to render as HTML so I can append that HTML into some other function that renders the final output with the control inside.
I know I can render a control with control.RenderControl(writer), but this can only be done in (pre)Render or RenderContents overrides.
For example.
I have a dal.cs file where is stored all static functions and voids that communicate with the database.
Functions mostly return string so that it can be appended into some other function to render the final result.
The reason I am doing like this is that I want to separate the coding from the HTML as much as I can so that I don't do <% while (dataReader.Read()) %> in HTML and display the data. I moved this into a CodeBehind.
I also use this functions to render in the HttpHandler for AJAX response.
That works perfectly, but when I want to add a control (ASP.NET Server control (.cs extension, not .ascx)) I don't know how to do that, so I see my self writing the same control as function that returns string or another function inside that control that returns string and replaces a job that would RenderContents do, so that I can call that function when I need control to be appended into a another string.
I know this may not be a very good practice.
As I see all the tutorials/videos about the ASP.NET MVC, I think it suite my needs as with the MVC you have to construct everything (or most of it) by your self, which I am already doing right now with web forms.
After this long intro, I want to ask how can I build my controls so I can use them as I mentioned (return string) or I have to forget about server controls and build the controls as functions and used them that way?
Is that even possible with ASP.NET Server Controls (.cs extension) or am I right when I said that I am not using it right.
To be clear, I am talking about how to properly use a web forms, but to avoid data binders because I want to construct everything by my self (render HTML in Code Behind).
Someone might think that I am appending strings like "some " + "string", which I am not. I am using StringBuilder for that so there's no slowness.
Every opinion is welcome.
You need to stop thinking in terms of "controls" and webforms. MVC is a completely different way of constructing applications.
I also hate the automatic renderers in WebForms, they produce horrible html that never makes any sense. However, you dont want to be writing your html in your codebehind and passing it around as strings, thats just nasty. Your presentation code is mixed in with your logic, AND youre writing HTML in c# strings!!!
So, MVC... Instead of "widgets" that do interacty things with codebehinds and postbacks, yours view ONLY display data and contain forms to allow you to post to the controllers.
Because of this, you can strongly type your views to a Type, and then access the data you pass to it from a controller via the Model property. The equivalent to UserControls are Partial Views (ViewUserControl) which can be used to modularise your rendering code for types. For example, you might make an Address partial to which you pass your Person's Address property every time you need it rendered. This way you aren't repeating html all over the place.
P.S. A single file for all your DAL?
I hope I never ever have to work on an app you wrote in this manner. If your application is string-heavy then something is wrong.
Agree with #sliderhouserules, the way you are using MVC framework is awful. You must forgot all your "old school" techniques.
You should never use SqlCommands, SqlReaders, etc. in the code of the pages. You should pass to the view only a model (e.g. View(bar)) and it will be better if you avoid usage of
ViewData["some magic string"] = bar
Every time when you will use "old school" technique 2 mans and 2 cats will be killed :).
Also it's better to use some ORM (Object-Relational Mapper) like Linq2sql, NHibernate, SubSonic, etc.
If you need in samples of good application design please look at SharpArchitecture. It has a very good architecture and implementation and may help a lot. It has one sample (with Northwind db) and one more sample will be added soon.
Also look at CodeCampServer. It has very good architecture too.
It's better to look at the code of these projects instead of looking videos because existing videos can't demonstrate good sample of architecture, just a simple usage of functionality.
About server controls, you may use them if they can be used without 'runat="server"', like PlaceHolder. And you may create them too, but you shouldn't load any data in them directly. If you don't want to copy-paste html you should review your code and you should refactor it. Every duplicated code should be moved to MasterPages of UserControls (ascx ones).
And once more, please spend some time to look at these samples. You'll save your nerves and time in the future when you'll need to update the app or fix something. At the first look they can be hard to understand but this is only at the first look.
Hope this helps.
#lopkiju, I think that the MVC pattern would serve you much better than your current WebForms solution if you want that much control over the output HTML.
You can use Web Forms this way as you already do, but it is not designed to be used this way, so it will be a pain.
More detail
In my opinion, read some articles about the Separation of Concerns (also known as SoC) principle. If you apply it correctly, it can save you many-many headaches when you'll debug your app, and also for those people who you work with (eg. those who may have to read or modify your source code).
My other tip for you is this:
You are right that you shouldn't do things like <% while (dataReader.Read()) %> in your View code. But perhaps there are better ways to make it more elegant than your current way.
I think you should consider using some sort of ORM for these purposes. (LINQ to SQL, or even NHibernate.) If you get it, it will be much simpler. So much that you may not want to use DataReaders directly again. :-)
What I recommend for you
Simply, read the NerdDinner tutorial, and build the samle app by yourself step-by-step.
After that, try to build a similar app that serves a different purpose by yourself while applying the same rules and design that you applied to the tutorial.
I'm pretty sure you have the expertise to do it, and after actually doing something with it, you can pretty much get the feel of it.
The tutorial also explains and includes the principles I mentioned above, which will be very much use to you.
If you want the ASP.NET MVC path, you can set up controls as ASCX and they will simply be tags filled by the controller. It may not be old school enough for you, however.
You can create your full UI in code behind, if you so desire. This can include creating the HTML in routines.
But, I would recommend, if you are sticking with ASP.NET, reconsidering the bind model over the DataReader.Read() and output + loop. It is not only old style, it is highly inefficient and hard to maintain.
ASP.NET MVC does create a much looser ASPX, as it is just a view. And there is a much greater separation of code and UI. But it will not fit with the old school model either.
Have you considered using micro-templates? Dave Ward has a good example of of a client side data repeater that is uses micro-templates for layout after making a call to a page method. This sounds like this is more in the spirit of what you are trying to accomplish and can still be integrated nicely with WebForms.
The side effect will be that you will not rely on passing around HTML and can isolate your presentation from your logic.

Master page from external library

I want to put my master pages in a central library so I can use them in several projects without having the maintanance nightmare.
Everything is refactored in a generic and central way, they are all in a "shared" namespace.
But if put them in a seperate project, I can't reference them
I don't believe you can put that actual .master pages in an external library. You can place your own customer MasterPage-derived class in the library, and have your client apps derive from that, but I don't think that's what your intent is, since you're tagged with asp.net-mvc (implying very narrow views without much logic, and probably no code-behinds).
As far as I'm aware, there isn't a way to reference a master (nor, for that matter, an ASPX or ASCX) from an external library. Wish I had better news for you. (And I actually hope I'm wrong; I hope someone else here has figured out a technique for this, as I'd like to use that kind of thing myself on occasion.)
Just for reference's sake, I'll put Phil's mail here too:
Sorry for the late response. You could try using a VirtualPathProvider, but this would require full trust. Other than that, I don't know of any way to do this.
Phil
so there you go.

Resources