Blog Archives

I used n-layer architecture for my MVC projects

Really I enjoyed who that thinks about architecture and patterns and some ways for organization own source codes before starting a project. After doing some patterns for organization coding in n-layers applications based on my knowledge I found a best practice for implement n-layers application:

N-layer architecture schema

N-layer architecture schema

In two recent posts I took about entity framework 4.1/2 features and then create a domain layer for using their layers in UI layer.
In this post I will talk about an independent UI (web) layer. So, for this independency in web layer I used a dependency injection tool for using direct service interfaces in all controllers. According to this link with some benchmarks seems structuremap is better than other dependency injection third-parties tools. So I used structuremap.
Also, you can create a utility project and import all common your extension classes and your general html helpers for using to this project or other projects (may be as a template project).for do this, you should add system.web and system.web.mvc references to utility project.

Note: Maybe you want using utility library to all your enterprise applications, for this case you can make a local nuget package from this utility project, and then add utility package to all projects very fast. I’ll talk about “How to organized local nuget packages” in the next posts, but now you can see hanselman post about ‘how to create nuget packages’ or some docs in official nuget site. or visit this weblog
Anyway, come back to our target! I want create a sample for using domain layer in MVC project, so I choose membership subject for my sample. I wrote a customized membership. For do that, we should dirty some Domain codes.

You can assume I want create a form-Authentication based on .NET and using native classes for authentication with cookies in .NET but In this sample I want show to you that how to using native .net framework for making session based authentication.
In above figure you see some new classes under “Site” namespace. Please for more information about project check out project from Github.
There is a Controller class in Site namespace that I called AuthenticationController.
This base class for all controllers can do authentication and authorization for all users with difference roles.

using System.Threading;
using System.Web.Mvc;

namespace Arash.Membership.Site
{
    public class AuthenticationController : Controller
    {
        protected override void OnAuthorization(AuthorizationContext filterContext)
        {
            if (SessionPersister.SitePrincipal != null)
            {
                Thread.CurrentPrincipal = SessionPersister.SitePrincipal;
                filterContext.HttpContext.User = SessionPersister.SitePrincipal;
            }

            base.OnAuthorization(filterContext);
        }
    }
}

Other classes such as SiteIndentity or SitePrincipal are only an implement of some native .NET interfaces.
And Now Web layer:
- Install structuremap:
I added this third-party with execute command in packet manager:

PM> Install-Package structuremap

For using authentication you can create a derived controller form Authentication controller.
Conclusions:
After recent three posts we could create a new organized model for creating n-layer applications. And then implement a session based authentication model for sample, I hope you use this model and say me disadvantages or advantages.

You can download this sample form github repository.