|Written by||Imar Spaanjaars|
Are you looking to hire an experienced software developer or .NET consultant? Then get in touch with me through my company's web site at devierkoeden.com
Found an interesting article on this site? Got inspired by something you read here? Then consider making a donation with PayPal.
Like this article? Or do you think it sucks? Make yourself heard by casting your vote below.Total number of ratings: 19
Note: this is part two in a series of ten. If you rather read this entire series off-line, you can buy the full series as a convenient PDF document that comes with the full source. Besides the convenience, buying the PDF will also make you feel good as it shows your appreciation for the articles and helps me pay the bills for my server and hosting so I can keep running imar.spaanjaars.com and continue to provide you with great content. For more details, check out this post that shows you how you can buy the entire series right now.
This is Part 2 in a series of 10 that show you how to build N-Layered applications using ASP.NET 4.5 and Entity Framework 5 Code First. The previous article provided some history of the architecture of the Contact Manager application and gave a broad overview of the new architecture. In this installment, things get a bit more concrete when you see how to setup a solution in Visual Studio 2012. The VS solution is going to contain three class libraries: one for the Infrastructure, one for the application’s Model and one to hold the Entity Framework (EF) Repository implementation. I’ll also add four frontend projects (an ASP.NET MVC 4, a Web Forms project, a WCF service project, and a windows command line application) which are discussed in detail in Part 6, 7, 8 and 9 of this series respectively. In the next article in this series I’ll extend the solution with four more projects for unit, integration, UI and service tests.
If you haven’t read Part 1 of this series yet, you’re encouraged to do so first. The following list has links to all articles in this series:
Note: as mentioned in Part 1, this article and the next article (Part 3) contain a lot of hands-on, step by step instructions that show you how to setup a solution like the Spaanjaars.ContactManager application. You can use these instructions pretty much as-is for your own applications. The remaining parts in the series analyze the working code for the Spaanjaars.ContactManager application. I’ll show a lot of the code in detail, and explain how it works, but you won’t find detailed step by step instructions on how to add the code and files to the various projects. Instead, you're encouraged to download the code at the end of each article to see how it works.
If you’ve read the previous article series, you’ll recognize the naming conventions I use for my projects and root namespaces. For the main projects I use the pattern: Company.Project.Layer where Company is your company’s or your client’s name, Project is the name of the application and Layer specifies the type of project in the stack. I can further divide the layer in specific project types. This gives me names such as Spaanjaars.ContactManager45.Model and Spaanjaars.ContactManager45.Web.Mvc.
My test projects follow the same pattern for the root name and are then further divided in the type of tests (Unit, Integration etc.) leading to names such as Spaanjaars.ContactManager45.Tests.Unit.
Note that the Infrastructure project I introduced in the preceding article is independent of the ContactManager application and can be reused across multiple applications. As such, it’s called Spaanjaars.Infrastrucure rather than Spaanjaars.ContactManager45.Infrastrucure.
In the following section you’ll find detailed instructions on setting up the solution. This is useful if you want to create your own project from scratch. If you just want to see the sample code or run the application, you can skip all these steps and simply open the solution that comes with this article series in Visual Studio 2012. You find a link to the source code at the end of each article.
Note: these steps are heavily borrowing from an earlier article I wrote about setting up a project for team development and TFS. I decided to repeat part of the steps here and make them more specific for the Spaanjaars.ContactManager application.
This Main folder is used later for Branching in TFS. This all feels a bit clumsy but I see no other way to create the solution with this name and store it in a folder called SolutionName/Main. Fortunately, this is a one-time operation.
Notice the inclusion of the Applications folder in the path. This way you can group all projects that make up the core application in a single folder. In the next article I’ll use a folder called Tests inside the Main folder to group all Test projects.
Make sure you don’t forget the \Applications part in the project’s location, as VS defaults to the solution’s folder instead.
Note that the Infrastructure project is generic enough to be placed in a namespace which is outside the ContactManager application. This makes it possible to reuse the classes defined in this project in other solutions based on the same model. For this sample application I put the project in the Applications folder, but if you’re reusing it across multiple projects you’ll want to store it elsewhere such as in C:\Projects directly.
In the next section you’ll see how to add four frontend projects to the solution. In your own projects you don’t need all four of them; just pick the one(s) you need. If you want to follow along with this series, just add all four projects.
Tip: if you forget to store one of the projects in the Applications folder, remove the project from the Solution Explorer (right-click the project and choose Remove). Move the project to the Applications folder using File Explorer and then add the project to the solution again using its Add Existing Project option.
Figure 2-5 The Solution Explorer with all Application Projects
Here are a few tips to get the Visual Studio solution in a good shape:
Now that the skeleton for the solution is ready, it’s a good time to add it to your source control system. If you don’t use such a system, you can skip this section (although you should seriously consider using one; you can get a free hosted TFS solution for up to five team members at http://tfs.visualstudio.com/). The following section uses TFS as the source control system, but similar concepts apply to other source control systems.
Figure 2-6 The Add Solution to Source Control Dialog
For more information about creating a branch, check out the original article on adding solutions to TFS (http://imar.spaanjaars.com/565/recommendations-for-setting-up-a-visual-studio-solution-for-tfs). In addition, check out the Branching and Merging guide at http://vsarbranchingguide.codeplex.com/.
Here are a few tips to keep your solution in great shape, and easy to manage:
In this article, you saw how to set up a Visual Studio solution and a number of projects. By following the recommendations from this article with regards to project naming and their location on disk your solution will be easy to manage for you and anybody else in your team that may need to have access to it. By storing all your projects in folders below the .sln file, opening the application from source control is as easy as double-clicking the .sln file in the Source Control Explorer.
In the next article you’ll see how to add various flavors of unit test projects to the solution. If you’re not interested in unit testing (yet), you can skip directly to Part 4. However, I still encourage you to read Part 3 as it might change your mind on whether unit testing is something for you or not.
Wonder where to go next?
You can read existing comments below
or you can post a comment yourself on this article
Consider making a donation
Please consider making a donation using PayPal. Your donation helps me to pay the bills so I can keep running Imar.Spaanjaars.Com, providing fresh content as often as possible.
I am interested in what you have to say about this article. Feel free to post any comments, remarks or questions you may have about this article. The Talk Back feature is not meant for technical questions that are not directly related to this article. So, a post like "Hey, can you tell me how I can upload files to a MySQL database in PHP?" is likely to be removed. Also spam and unrealistic job offers will be deleted immediately.
When you post a comment, you have to provide your name and the comment. Your e-mail address is optional and you only need to provide it if you want me to contact you. It will not be displayed along with your comment. I got sick and tired of the comment spam I was receiving, so I have protected this page with a simple calculation exercise. This means that if you want to leave a comment, you'll need to complete the calculation before you hit the Post Comment button.
For more information about the Talk Back feature, check out this news item.
Unfortunately, something went wrong and your message or comments have not been submitted successfully.
There's a fair chance things broke down because you tried to post something that looks like HTML. Things that look like HTML include (X)HTML, obviously, XML, ASP.NET markup and c# generics syntax as all of them use the < and > characters.
If that's the case, try altering your message and remove anything that looks like an angled bracket. You can replace them with [ and ] for example so you can still make it look like HTML to some extend.
If, on the other hand, you were trying to spam this web site, I am pretty glad I caught you in the act and stopped you from doing so ;-)
The number you entered is not correct. Please enter the sum of the two numbers again.