How to move models to separate project .NET MVC - asp.net-mvc

I created .NET MVC application and I want to move my models (and then Controllers) to a different project in my solution.
I'm a noob in MVC so I need simple explanation please.
Thank you very much

Add a project to your solution called "YourProject.Models".
Add references to System.Data, System.Data.Linq, System.Data.DataSetExtensions (if you're using Linq to Sql)
Add a reference from your original MVC project to the new "models"project.
Move your model classes from your MVC project to the new project.
Compile and run.

Create a new project with appropriate name and move the models to a new project. It is as simnple as that. Then add a reference to the project you are using. Next step is to Create interfaces and implement those interfaces for code separation. Next use IoC (Inversion of control or dependency injection). I suggest asp.net mvc structure map for Ioc. You can use NuGet tool to do the job for you.

Move your models wherever you want and then update the strongly typed views to match ViewPage<Namespace.For.Your.ModelClass> or maybe change the web.config namespace if you do it that way.

Related

How to create an ASP.NET MVC class library

I want to create a class library for an MVC 4 web application. Every search I've tried has returned plenty of references that merely mention creating one, or the importance of doing so, but not specifics of how.
My first assumption was a template would be under Web in the Visual Studio New Project dialog, but no. I was unsure if I was to use the Class Library template under Windows, but did.
I want to include things like some data access (e.g., DbContext), but while Intellisense sees the System.Data.Entity namespace, there are no classes available. I guess I need some additional references, but no idea which ones. Looking at the references in my main MVC project, at lot of them are pointing to the Packages folder. I'm unsure if I should be doing the same.
In short, I'm looking for instructions on how to create a class library for MVC in Visual Studio, including the necessary references for EF, Razor and whatever else.
you used the correct template - a simple class library is all you need.
then in the MVC web project just add a reference to the class library project
Use NuGet to add references to the pieces of functionality, like EF and System.Web.MVC, that you need in your class library or libraries.
A data access project to handle persistence and a class library to hold HTML Helpers that you might want to reuse both make some sense. Razor views if you're using the RazorEngine rendering stack can also be interesting to be able to test.
You are right to use the Class Library template in visual studio for your needs. You can add all of the references you need through NuGet (such as Razor, EF, and so on) and by Right clicking on references in the Solution Explorer and picking and choosing what you need.
Remember when using multiple projects that you add references between projects too! (for example your Web App project needs to know about your Data Repository Project)

Separate solution into different projects

I'm currently learning ASP.Net MVC; I'm using Visual Studio Express 2012 with MVC4 (which is the last version) and I'm totally new to this stuff. My goal is to rewrite a huge web application to MVC, so I was told to separate my main solution into 3 projects using the code-first method:
The core (models and controllers I guess)
The UI (views, scripts, and Content)
And the Database (Entity Framework 5.0 will be used)
I'm quite familiar with MVC, but not separating stuff into different projects. Now I'm a bit lost, I don't have a clue on how to do that, which should reference who, where, how, etc.
Your solution could be structured this way:
UI - ASP.NET MVC application project containing the controllers, views, view models, mapping logic between your domain models and view models, scripts, styles, ...
DAL (EF 5.0, EF autogenerated domain models, Data Contexts, ...) everything that is specific to the data retrieval
The UI layer will then reference the DAL layer.
Some people might also opt to externalize the controllers, view models and mapping logic into a third layer which in turn will reference the DAL layer. The UI layer in this case will reference both other layers.
There are tutorials available on here: http://www.asp.net/mvc
It really helped me out to get the basics of MVC, but be aware - sometimes there are parts missing in the video's, but you can find the code which isn't provided easily elsewhere.
Good luck :)
The tutorials are used to show code first.
create an empty solution using the Visual Studio Blank Solution template
add a solution folder (folder name will be your project name)
then right click that folder and select add project then select "class library" (for The c# classes domain logic)
same again right click the folder and select add project then select asp.net mvc3 template
then same way you create the test template as a new project.
For more information you can follow this book http://www.apress.com/9781430234043

Where do utility methods fit in ASP.NET MVC setup?

Where do utility methods go in MVC setup? They are not Models, Views or Controller. They don't belong in those folders. So do is the only right thing to do is to keep utility methods outside your ASP.NET MVC project and put them into a project of their own?
It really depends on how big your project is. If it's just a small project with a few utility methods, then what I normally do is put them in a folder called "Infrastructure" inside the MVC project.
The initial setup you get for an MVC project is only really a guide (with some exceptions), and you're free to add folders and put code in them if you want to.
If there's going to be a lot of supporting code, then a separate project might be cleaner and easier in the long run.

Where do Subsonic classes go in an ASP.NET MVC Project?

I've built ASP.NET webform projects in the past, and when generating Subsonic classes, the teams I have been on have put our Business Layer/DAL objects into a Project.Framework project.
Would that still be a recommended structure, or should the Subsonic classes go directly into the /Model folder within the MVC web project?
It shouldn't go in the Model folder (I think the Model folder should just be used for a Class that is made only for the view and wont be used in the rest of the app).
It should go into a separate assembly Maybe Project.Core or Project.Data
I would not put the Subsonic classes directly in the MVC project. Since you didn't with ASP.NET, there is no reason to change now.
I wouldn't even leave the Controllers in the MVC web project.
You can put them in Model, and move them to their own project later, but its not really any more work to just put them in their own project now, so I would just do that.

Where to put a simple Class in an MVC project?

Ok, this is possibly borderline-subjective, but I wonder where one would put a simple class that does something within an ASP.net MVC project? I mean a class like the Base36 De/Encoder, that is neither Model nor Controller. Should this always go into a separate Class Library Assembly, or do such Classes have a place within the MVC Web Application project?
And yes, I know that it probably does not really matter, I'm just possibly a bit over careful to learn a new technology the "right" way from the beginning.
I personally put such classes in some common "utils" assembly. Not only does it avoid junking up your MVC project, but such helper classes often find their way into other projects I work on.
You should still have your separate projects where you include your own libraries, helpers and frameworks. You should still see the ASP.NET MVC project as a web project. You don't have to put all your business logic or web helpers in the Model folder.
You should read the article Jeremy Miller wrote recently about separate assemblies. Using the IoC pattern gets you loose coupling in a way that creating a lot of assemblies cannot.

Resources