Why Build Your System from the Business Layer First? A Five‑Year Architecture Story
This article chronicles a developer's five‑year journey of designing a system by starting with the business layer, exploring entities, repositories, services, view models, MVC, and tooling, while highlighting common pitfalls and practical solutions for backend architecture.
Writing technical documentation is hard, so I decided to adopt an agile “write first, refine later” approach and share my five‑year journey of building a system from scratch.
I start by questioning the common UI‑first approach and argue for beginning with the business layer, especially when supporting desktop, mobile web, and app versions.
Embarrassment
After defining a “forget‑the‑database” principle, I moved from UI‑driven development to a business‑layer‑centric design.
Entity
The traditional three‑tier business layer mixes logic with data access, making unit testing painful. Isolating the database operations and focusing the Entity project on state changes solves this problem.
Query (Repository)
Using NHibernate, I map objects to tables and perform CRUD via session.Save(), session.Delete(), session.Load(), and session.Query(). I treat the repository as a warehouse that returns collections of entities.
class BlogRepository
{
IList<Blog> GetBlogs(int pageIndex, int pageSize) { return new List<Blog>(); }
Blog Get(int Id) { return new Blog(); }
}Service
A Service layer sits between UI and BLL, enabling front‑end and back‑end developers to work against a common interface (UIDevService vs. ProdService) and simplifying unit testing.
ViewModel
ViewModel acts as a DTO for the presentation layer, keeping UI code independent from Entity and allowing custom data composition such as user‑specific information.
MVC
Controllers call Services to obtain ViewModels for Views. Topics like routing, Autofac, and Session‑Per‑Request are left for later deep dives.
Tool
The Tool project contains BuildDatabase, which creates development and integration test databases and supports automated schema updates.
Open Issues
CurrentUser handling, Get‑Post‑Redirect pattern, MVC routing, partial views, unit testing, performance optimizations.
Service performance (SessionPerRequest), UIDev/Prod switching, Query testing, AutoMapper for ViewModel mapping.
Entity collection performance, polymorphism, unit testing of entities and NHibernate mappings.
BuildDatabase complexity and other utility tools.
Despite the many pitfalls, iterating on this architecture has been rewarding and illustrates the continuous learning curve of software development.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
21CTO
21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
