Code Cleanup - asp.net mvc - asp.net-mvc

This might be a weird question to ask.. but nevertheless this is my question.
I have a huge Asp.net MVC project with lots of model classes. As the requirement grew, we kept on adding some properties and deleting some. Eventually I ended up having properties which are not referenced anywhere in the project.
Is there any quick way to find out these orphan properties.?

ReSharper can do this. Right-click on your solution and selection "Find Code Issues".
One of the results is "Unused Symbols". This will show you classes, methods, properties etc., that aren't used.

I would use resharper to assist you with this task. In case you don't want to spend the $349 you can use it for 30days for free. Resharper allows you to analyse the whole solution and highlights obsolete/unused code.

Related

Which assemblies are required for MVC4?

In the MVC4 templates, many many (...many!) assemblies are added to a project, even for the "empty" template.
Which ones are really necessary, and what does each one actually do - I cannot find a list anywhere? I only want to reference the ones I need.
I created an "empty" project and started removing them one by one, but it gets tedious (and breaks) very quickly because of the associated web.config changes, etc.
Can't find anything official, but did manage to get a barebones project to run by using this answer.
Would still like to know what all the DLLs are for in the various templates.
If you are using ReSharper you can right-click the References folder and select either "Remove Unused References" or the safer option "Optimize References". The latter option gives you a report of which assemblies are and aren't used.
Be careful then using this in a mature solution where dependencies are injected as you might not have any direct code references to an assembly at compile time. But they could be required at runtime.
Sorry, I haven't answered your question directly, but hopefully I have shown you how to find out for yourself.

Visual Studio "Add Controller" list of model classes

The "Add Controller" dialog on an MVC project provides a dropdown list of model classes to choose from. These are usually the model classes in the project, and that's what I used to see. But now I'm getting this:
Any idea what's wrong and how to change it back to what I believe is the normal way?
I often run into a problem with that dialog box "forgetting" classes and showing odd choices. Closing and restarting VS has always cleared the problem up for me.
It seems there's no solution to this problem. That dialog shows the classes from all the classes in the solution, and that includes any 3rd party classes that were imported (using something like NuGet). Which kind of makes sense. So it's a feature, not a bug.
The workaround to sifting through that whole list is to take advantage of the autocomplete functionality built into that dialog, which makes it quite easy to find the required full class name.

Visual Studio: What approach do you use to 'template' plumbing for similar projects?

When building ASP.NET projects there is a certain amount of boilerplate, or plumbing that needs to be done, which is often identical across projects. This is especially the case with MVC and ALT.NET approaches. [I'm thinking of things such as: IoC, ORM, Solution structure (projects), Session Management, User Management, I18n etc.]
I would like to know what approach you find best for 'reusing' this plumbing across projects?
Have a 'master solution' which you duplicate and rename somehow? (I'm using a this to a degree at the moment, but it's fairly messy. Would be interested how people do this 'better')
Mainly rely on Shared Library projects? (I find this appropriate for some things, but too restrictive for things that have to be customised)
Code generation tools, such as T4? (Similar to the approach used by SharpArchitecture - have not tried this myself)
Something else?
Visual Studio supports Custom Templates.
I definitely (mostly!) go for T4 templates in conjunction with a modified version of SubSonic 3. I kind of use the database to model my domain and then use the T4 templates to generate the model and associated controllers and views. It takes about 50-60% of the effort out and keeps a consistency in place.
I then work on overrides (partials) of the classes along with filters and extension methods to 'make the app'. Now that I'm familiar with the environment and what I'm doing, I can have a basic model with good plumbing in place in a very short space of time. More importantly, because I create a set of partial class files, I can regenerate all I want without losing any of my 'custom' coding.
It works for me anyway :)
You could do it the bearded, t-shirted, agile style and create a nice template and put it in sourcecontrol. So when you need a new project, you just checkout the template?
For insanely fast MVC site setup, I use modified T4 templates (created with T4 Editor) and with ALOT of help from Oleg Sych's blogs for page generation (for your typical add/edit/index pages) combined with an awesome implementation of an automated create-update-delete called MVCCrud (if LINQ-to-SQL is your preferred data access method)
Using modified T4 templates and MVCCrud you can create fully functional entities (Create/Edit/List/Delete) with error handling and intuitive error messages in about 4 minutes for each.
I create a new project using the new project wizard so that I get unique project GUIDs assigned. Then I would use "Add Existing Item" to copy items from similar projects if it made sense to do so.
I sometimes use a file diff tool to copy references from one project to another, otherwise I just add the references by hand. A file diff tool can also be used to include similar source files, but the underlying files have to be copied anyway, so I prefer "Add Existing Item".
I've used T4 to generate solution and project files, but that definitely seems like an edge case and not something that would normally be necessary. In that case, I'd probably wrap the T4 in a PowerShell like script to create and populate the rest of the directory structure.
I use "shared libraries" pretty aggressively in general, but not specifically due to this scenario.
In general, I don't find myself reusing plumbing between projects much. It's probably more often that I hack away in one "prototype" project, then abandon it, and rebuild the project from scratch following the above approach and only bring over the "non-hacky" code.
I'm creating a MVC2 application template at http://erictopia.com. It will contain all the basic items I think should be in a MVC project. These include BDD specifications, an ORM (NHibernate and possibly Lightspeed), T4 templates, custom providers, ELMAH support, CSS/Javascript minifier, etc.

Create reusable 'base code' using MVC2-RC?

I am trying to write a simple library for MVC2 projects that takes care of user login, e-mail validation, password recovery, etc. Since some of these steps involve user interaction, I need to have Views and Controllers in that project.
In MVC2 Preview 2, one could do this by abusing areas (at least, it seems) since they were implemented in different projects. However, in RC a new area will merely create a subfolder. Since I want to share the base code between applications, that is not an option.
I tried to brutally hack the AfterBuild steps into my project files, but that doesn't 'feel right' (I wouldn't sleep another night :)
Could you share some thoughts on how to do this?
This question might be a duplicate of Areas over multiple projects - Views not found in child projects
The build task to copy the views have been moved to the Mvc.Futures project. Other options to solve the problem include defining the views in the application, even when they are being referred to by controllers in the library. Also, the MvcContrib project offers portable areas.
A thorough explanation by Phil Haack can be found at http://forums.asp.net/p/1494640/3540105.aspx

How can one use the "Add view" dialog in Visual Studio efficiently when working with ASP.NET MVC?

Does anyone else think that the add view dialog in VS is useless or is it just me?
Why is there no search/filtering in there? I can not even paste the type name (without namespace) there to speed up the view creation.
Plus, there are a lot of irrelevant classes there, including classes from all referenced libraries.
Am I missing something or Microsoft never did any usability testing with that??
Is it possible to filter out types/assemblies in the dialog and/or better navigation compared to manually scrolling the combobox with all the types listed? Is it possible to include only types from a particular directory/assembly/namespace? (the ViewModels namespace for example)
EDIT: As it seems from the answers that there is no usable workaround for this, I have filed a suggestion at Connect: https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=507784
Please vote for the suggestion if you find this is an important issue.
There is search/filter, but it's a "starts with" search. Start typing and the list will be filtered.
You can paste the type name, but you do need the namespace.
No, I don't think you can remove assemblies.
Could it be improved? Sure. Some of your suggestions are good, and you should consider filing Connect reports for them. Is it useless, IMHO, no.
I tried the dialog in VS2010. As of beta 2, it behaves the same as VS2008, AFAICS. So get your suggestions in soon!
I mainly agree with you. While it's not competely useless, tha fact that all the classes from the referenced libraries show up and that it doesn't have a search box make it slow to add a new view. Maybe you could file a wishlist bug report on the ASP.NET MVC site...
As it seems from that there is no usable workaround for this, I have filed a suggestion at Connect.
In case anyone else feels that this dialog should be improved, please vote for the suggestion in Connect.
Resharper 5.0 implements this dialog very efficiently, solving all the described problems.

Resources