3

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.

2
  • I can see wanting to move the data access to a separate project, but why do you need the razor stuff as well? Commented Oct 7, 2013 at 15:21
  • I friggin hate they way everything points to packages. We have a lot of projects so we GAC things like Asp.Net.Mvc and NewtonSoft.Json. I don't know who it helps to have individual projects specifyin their own version of core files Commented Oct 8, 2013 at 1:53

3 Answers 3

5

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

Sign up to request clarification or add additional context in comments.

Comments

3

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.

Comments

2

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.