Visual Studio 2013 solution containing two or more projects

This is a brief introduction to multi-project solutions in Visual Studio 2013 (with Update 4 or later).

This was updated on June 3, 2015.

.

Scenario and solution

After you create a number of ASP.NET MVC web apps, you become familiar with the project-creation process.

You also notice that a typical web app contains all the code for all the layers in the System Design Guidance diagram (which was recently described in the BTI420 notes page for January 27, 2015).

What if you want to separate the “data access” code?

And why would you want to do this?

Well, you may have two or more web apps that use the same data store. It would be nice to write the data access code once, and use the code in both web apps.

How is this done?

With a Visual Studio “solution” that has more than one “project”.

.

Code example

The techniques discussed in this post have been implemented in a code example named “Combo3”.

.

Getting started – create an empty solution

In Visual Studio 2013, open the New Project dialog.

On the left-side navigator, choose Installed > Templates > Other Project Types > Visual Studio Solutions.

In the center panel, choose the only item, “Blank Solution”. Choose a name for the overall solution, and a storage location.

In Solution Explorer, notice that there is only one item – a solution – and no projects.

Read the MSDN document that describes Visual Studio solutions and projects.

.

Adding a standard web app project to the solution

In Solution Explorer, right-click the solution item, and choose Add > New Project.

In the center panel, choose the familiar “ASP.NET Web Application”. (You may have to use the left-side navigator to choose Installed > Visual C# > Web.) Choose a name for the web app project. Notice that its storage location is a folder inside the solution folder created above. You can actually locate it anywhere.

.

Adding a “class library” project to hold your “data access” code

In Solution Explorer, right-click the solution item, and choose Add > New Project.

On the left-side navigator, choose Installed > Visual C#.

In the center panel, choose “Class Library”. Enter a name. (In this example, it will be “DAL”, for “data access layer”.) Notice that its storage location is a folder inside the solution folder created above. You can actually locate it anywhere.

Visual Studio will create the project. Delete the “Class1.cs” source code file, because you will not need it.

Study your Solution Explorer items. Notice that there are two (2) projects in the solution.

.

Adding a “class library” project to hold your “business (application) logic” code

The purpose of this library is to hold the problem domain’s common business or application logic. In the past, the Manager class was used to hold this code.

It will also hold the object conversion (or mapping) tasks. From the perspective of a web app (or web service) project, the business logic layer will accept only view model objects and collections, and will deliver only view model objects and collections. (And primitive value types, of course.)

In Solution Explorer, right-click the solution item, and choose Add > New Project.

On the left-side navigator, choose Installed > Visual C#.

In the center panel, choose “Class Library”. Enter a name. (In this example, it will be “BLL”, for “business logic layer”.) Notice that its storage location is a folder inside the solution folder created above. You can actually locate it anywhere.

Visual Studio will create the project. Delete the “Class1.cs” source code file, because you will not need it.

Study your Solution Explorer items. Notice that there are three (3) projects in the solution.

.

Add references

At this point in time, the code in each project cannot use code in another project. You will fix that now.

.

Enable the BLL project to use the code in the DAL project

In Solution Explorer, in the BLL project, select the References node. Right-click, and choose “Add Reference”. A dialog appears, similar to the excerpt shown below. (If not, use the left-side navigator to choose Solution > Projects.)

multi-project-solution-add-reference-to-dal

.

Check the box beside the “DAL” project, and click the dialog’s OK button.

.

Enable the web app project to use the code in the BLL project

In Solution Explorer, in the web app project, select the References node. Right-click, and choose “Add Reference”. A dialog appears, similar to the excerpt shown below. (If not, use the left-side navigator to choose Solution > Projects.)

multi-project-solution-add-reference-to-bll

.

Check the box beside the “BLL” project, and click the dialog’s OK button.

Stop and think for a moment. The web app project does NOT have direct access to the data access layer. That makes it easier, in the future, to change the data access scheme or design. Also, there is NO possibility that details about the design model will “leak” into the web app. Nice and clean.

.

Write data access code in the DAL project

A number of tasks…

.

Add Entity Framework

Right-click the data access class library project, and choose the NuGet package manager dialog.

Locate and install the EntityFramework package.

.

Add a local database

Right-click, Add > New Item.

On the left-side navigator, choose Installed > Visual C# Items > Data.

From the center panel, choose a “Service-based Database”. Name it. (In this example, it’s called “ABCStore”.)

.

Get its connection string

In Solution Explorer, double-click the “ABCStore.mdf”. Visual Studio will open the Server Explorer on the left side of the screen, and will highlight the just-created database. Right-click, and choose Properties.

Copy the value of the “Connection String” property. You will need it for your web app project’s Web.config file.

.

Add a connection string to the web app project

Edit the web app project’s Web.config file. Add a connection string inside the <configuration> element. In this example, it is located near the top, before the <appSettings> element. For example:

<connectionStrings>
  <add name="DataContext" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=&quot;d:\Users\peter\Documents\Visual Studio 2013\Projects\ICTAS\Combo3\DAL\ABCStore.mdf&quot;;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>

.

Add design model classes

Write some design model classes, using the familiar technique.

.

Add a database context class

Write a database context (named “DataContext”, to match the value in the Web.config connection string). Use the familiar technique. Remember to create a DbSet<TEntity> property for each design model class.

.

Add a “manager” class

This class will hold methods that perform data access tasks.

Similar to the manager class in past projects, it will have a private field to hold the reference to the database context. The manager class methods will perform operations on the data context. The methods will accept and deliver objects and collections that are based on design model classes. (And primitive value types, of course.)

.

Write code in the BLL project

A number of tasks…

.

Add AutoMapper

Right-click the business logic access class library project, and choose the NuGet package manager dialog.

Locate and install the AutoMapper package.

.

Add a reference to the assembly that has the data annotations

Right-click the project’s “References” node, and choose “Add Reference”.

On the left-side navigator, choose Assemblies > Framework. Locate and select (check/mark) the System.ComponentModel.DataAnnotations item.

multi-project-solution-add-reference-assembly

.

Add one or more source code files to hold view model classes

View model classes will live in this project. Following the familiar technique, create a source code file for each entity, or entity group, or workflow that you need.

The web app project has a reference to the BLL project, so these view model classes will be visible and usable in controllers. (A “using BLL” directive will be needed at the top of each source code file that needs it.)

.

Add a “manager” class

This class will work with the manager class in the DAL. It will have a private field to hold the reference.

This class can be used by the controllers, and other code modules in your project. The methods will accept and deliver objects and collections that are based on view model classes. (And primitive value types, of course.)

Write a constructor for this class. Add the AutoMapper “CreateMap” statements to the constructor code block.

.

Write code in the web app project

Using the familiar technique, create controllers as needed, and write code that will call the BLL manager class methods.

Have fun. Enjoy.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: