Let's say you’re building a web application which connects to a database. This is a very normal and consistent scenario for business developers on a day to day basis. Right off the bat you now have two projects to build within one solution; the UI Layer and the Data Access Layer. I will also add two more projects to this solution, UI Tests and Data Access Tests.
STEP 1
Create the solution and projects
IpswitchftWeb.sln
- IpswitchftWeb.csproj (Web Application)
- IpswitchftCore.csproj (Data Access)
- IpswitchftWebTests.csproj (Web Application Tests)
- IpswitchftCoreTests.csproj (Data Access Tests)
STEP 2
Update the assembly and default namespaces
Next you want to update assembly name and default namespace values the properties tab for all four projects within Visual Studio.
- Ipswitchft.IpswitchftWeb (Web Application)
- Ipswitchft.IpswitchftCore (Data Access)
- Ipswitchft.IpswitchftWebTests (Web Application Tests)
- Ipswitchft.IpswitchftCoreTests (Data Access Tests)
STEP 3
Add folders to your project that make sense
Open up your IpswitchftCore.csproj and six folders:
- Core (root)
- Core/DataAccess (interfaces)
- Core/DataAccess/Impl (concrete classes)
- Core/Domain (data model)
- Core/Service (interfaces)
- Core/Service/Impl (concrete classes)
STEP 4
Here is an example of requesting data from the data access layer from the service layer that can be called from the web application.
IpswitchftCore/Core/DataAccess/ITestRepository.cs
using StructureMap;
namespace Ipswitchft.IpswitchftCore.Core.DataAccess
{
[PluginFamily("Default")]
public interface ITestRepository
{
string GetData();
}
}
IpswitchftCore/Core/DataAccess/Impl/TestRepository.cs
using StructureMap;
namespace Ipswitchft.IpswitchftCore.Core.DataAccess.Impl
{
[Pluggable("Default")]
public class TestRepository : ITestRepository
{
public string GetData()
{
return "data from a linq object";
}
}
}
IpswitchftCore/Core/Service/ITestService.cs
using StructureMap;
namespace Ipswitchft.IpswitchftCore.Core.Service
{
[PluginFamily("Default")]
public interface ITestService
{
string GetTestDataAndValidate();
}
}
IpswitchftCore/Core/Service/Impl/TestService.cs
using Ipswitchft.IpswitchftCore.Core.DataAccess;
using StructureMap;
namespace Ipswitchft.IpswitchftCore.Core.Service.Impl
{
[Pluggable("Default")]
public class TestService : ITestService
{
private ITestRepository testRepository;
public TestService()
{
testRepository = ObjectFactory.GetInstance<ITestRepository>();
}
public string GetTestDataAndValidate()
{
return testRepository.GetData();
}
}
}
IpswitchftWeb/TestString.aspx.cs
using System;
using Ipswitchft.IpswitchftWeb.Interface;
using Ipswitchft.IpswitchftWeb.Presenter;
namespace Ipswitchft.IpswitchftWeb
{
public partial class TestString : System.Web.UI.Page, ITestString
{
private TestStringPresenter _presenter;
protected override void OnInit(EventArgs e)
{
_presenter = new TestStringPresenter();
_presenter.Init(this);
}
public void ShowString(string message)
{
lblString.Text = message;
}
}
}
IpswitchftWeb/Presenter/TestStringPresenter.cs
using Ipswitchft.IpswitchftCore.Core.Service;
using Ipswitchft.IpswitchftWeb.Interface;
using StructureMap;
namespace Ipswitchft.IpswitchftWeb.Presenter
{
public class TestStringPresenter
{
private ITestString _view;
private ITestService _testService;
public void Init(ITestString view)
{
_view = view;
_testService = ObjectFactory.GetInstance<ITestService>();
_view.ShowString(_testService.GetTestDataAndValidate());
}
}
}
IpswitchftWeb/Interface/ITestString.cs
namespace Ipswitchft.IpswitchftWeb.Interface
{
public interface ITestString
{
void ShowString(string message);
}
}