Search

Categories

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Send mail to the author(s) E-mail

# Friday, 24 January 2014

There are going to be in depth notes here from this article.  Please consider buying the whole pdf  from him (as I have) as it
http://imar.spaanjaars.com/573/aspnet-n-layered-applications-introduction-part-1

These are purely my notes Imar’s article.

My goals are:

  • Understand this architecture as it “can be used to build real-world, large scale web applications.”
  • Build up from scratch his sample
  • Build out my own Winter Project app with the concepts I like from here
  • Pragmatic
  • Simple

Why Use N-Layers

  • Testability
  • Separation – make it easier to find where things are in large projects eg dataaccess, business logic, UI
  • Reuse – eg a console app can use all the goodness of the main MVC app

image

Previous Versions

  • BLL/DAL – lots of pass through code
  • DAL tight coupling making hard to test
  • SP’s hard to maintain and test
  • Validation was custom – now have better ways

Sidenote: PDF reader being horrible.. trying: http://www.foxitsoftware.com/downloads/

Starting from Scratch

image
Updating all packages

Also have enabled NuGet Package Restore.

image
My DLayer version.

Put into source control – am using github.

| | # 
# Wednesday, 11 December 2013

Want to display the list of Departments on the home page:

image

// Only want HomeController to know about IDepartmentDataSource and not a concreate implementation
        //private IDepartmentDataSource _db = new ApplicationDbContext();

        // A private field.
        private IDepartmentDataSource _db;

        // Poor mans DI
        public HomeController() : this(new ApplicationDbContext())
        {}

        public HomeController(IDepartmentDataSource db)
        {
            _db = db;
        }

Use IoC – Ninject, Unity, StructureMap

image

StructureMap plugs into the MVC Runtime. so we don’t need poor mans DI.

namespace eManager.Web.DependencyResolution {
    public static class IoC {
        public static IContainer Initialize() {
            ObjectFactory.Initialize(x =>
                        {
                            x.Scan(scan =>
                                    {
                                        scan.TheCallingAssembly();
                                        scan.WithDefaultConventions();
                                    });
                            // When you see something that needs IDep... then the concrete type to use is ApplicationDbContext
                            // and scope to a particular Http request
                            x.For<IDepartmentDataSource>().HttpContextScoped().Use<ApplicationDbContext>();
                        });
            return ObjectFactory.Container;
        }
    }
}

Scaffold

Right click inside the controller..

public ActionResult Index()
        {
            var allDepartments = _db.Departments;

            return View(allDepartments);
        }

image

Then we rewrote the scaffold like this:

@model IEnumerable<eManager.Domain.Department>

@{
    ViewBag.Title = "All Departments";
}

<h1>@ViewBag.Title</h1>

<ul>
    @foreach (var department in Model)
    {
        <li>@department.Name</li>
        @*<a href="/department/detail/5"></a>*@
    }
</ul>
| | #