Dec 15 2009

Setup your solution and project for success by proper naming conventions, namespace configuration and folder structure

Category: ASP.NETryancmartin1976 @ 20:56

First Strategy:

Second Strategy:

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

  1. IpswitchftWeb.csproj (Web Application)
  2. IpswitchftCore.csproj (Data Access)
  3. IpswitchftWebTests.csproj (Web Application Tests)
  4. 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.

  1. Ipswitchft.IpswitchftWeb (Web Application)
  2. Ipswitchft.IpswitchftCore (Data Access)
  3. Ipswitchft.IpswitchftWebTests (Web Application Tests)
  4. Ipswitchft.IpswitchftCoreTests (Data Access Tests)

STEP 3

Add folders to your project that make sense

Open up your IpswitchftCore.csproj and six folders:

  1. Core (root)
  2. Core/DataAccess (interfaces)
  3. Core/DataAccess/Impl (concrete classes)
  4. Core/Domain (data model)
  5. Core/Service (interfaces)
  6. 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);
    }
}

Tags: , , , , ,