Active Conventions with NDepend
June 28, 2008Some time ago Patrick Smacchia wrote a nice article on his blog about active conventions on your code base. I thought I tried this for myself and made up some interesting CQL queries.
Suppose we want to enforce a BDD naming convention for every SetUp method. Such methods should be named Establish_context. The following CQL constraint reports those methods that are not named consistently:
// <Name>
// NUnit SetUp methods that are not named consistently.
// </Name>
WARN IF Count > 0 IN
SELECT METHODS FROM ASSEMBLIES "MyProject.Domain.UnitTests",
"MyProject.Infrastructure.UnitTests"
WHERE HasAttribute "NUnit.Framework.SetUpAttribute"
AND !NameIs "Establish_context()"
Lets have another one. A while ago, I wrote this post about how WCF gave me this weird exception after I forgot to put a DataContract attribute on one of my DTO classes. Lets write another active convention for that:
// <Name>Data contracts without a DataContract attribute.</Name>
WARN IF Count > 0 IN
SELECT TYPES
FROM NAMESPACES "MyProject.MyServiceContracts.DataContracts"
WHERE !HasAttribute "System.Runtime.Serialization.DataContractAttribute"
Now here comes my personal favorite. Being the huge DDD adept that I am, I want to enforce that the domain is the core, the centerpiece, the kernel as you may of my application. I don't want it to have any dependencies to other assemblies in my application, especially not the infrastructure assembly.
// <Name>Domain is directly using the infrastructure</Name>
WARN IF Count > 0 IN SELECT METHODS FROM ASSEMBLIES "MyProject.Domain"
WHERE IsDirectlyUsing "ASSEMBLY:MyProject.Infrastructure"
It can't get any easier than this, now doesn't it? This is actively enforcing Separation of Concerns. Another way to have a convention about this is the following:
// <Name>Domain-Driven Design convention</Name>
WARN IF Count > 0 IN
SELECT ASSEMBLIES
WHERE AssemblyLevel > 1
AND NameIs "MyProject.Domain"
This means that my domain assembly can only have a reference to the .NET framework assemblies and nothing else. This gets my geek heart pounding.
Imagine the possibilities if you incorporate this with your daily and continuous integration builds. Pure software quality assurance if you ask me.
If you and your team want to learn more about how to write maintainable unit tests and get the most out of TDD practices, make sure to have look at our trainings and workshops or check out the books section. Feel free to reach out at info. @ principal-it .be
Jan Van Ryswyck
Thank you for visiting my blog. I’m a professional software developer since Y2K. A blogger since Y2K+5. Provider of training and coaching in XP practices. Curator of the Awesome Talks list. Past organizer of the European Virtual ALT.NET meetings. Thinking and learning about all kinds of technologies since forever.
Comments
Writing Maintainable
Unit Tests
Watch The Videos
Latest articles
-
Contract Tests - Parameterised Test Cases
June 28, 2023
-
Contract Tests - Abstract Test Cases
April 12, 2023
-
Contract Tests
February 1, 2023
-
The Testing Quadrant
June 15, 2022
-
Tales Of TDD: The Big Refactoring
February 2, 2022
Tags
- .NET
- ALT.NET
- ASP.NET
- Agile
- Announcement
- Architecture
- Behavior-Driven Development
- C++
- CQRS
- Clojure
- CoffeeScript
- Community
- Concurrent Programming
- Conferences
- Continuous Integration
- Core Skills
- CouchDB
- Database
- Design Patterns
- Domain-Driven Design
- Event Sourcing
- F#
- Fluent Interfaces
- Functional Programming
- Hacking
- Humor
- Java
- JavaScript
- Linux
- Microsoft
- NHibernate
- NoSQL
- Node.js
- Object-Relational Mapping
- Open Source
- Reading
- Ruby
- Software Design
- SourceControl
- Test-Driven Development
- Testing
- Tools
- Visual Studio
- Web
- Windows
Disclaimer
The opinions expressed on this blog are my own personal opinions. These do NOT represent anyone else’s view on the world in any way whatsoever.
About
Thank you for visiting my website. I’m a professional software developer since Y2K. A blogger since Y2K+5. Author of Writing Maintainable Unit Tests. Provider of training and coaching in XP practices. Curator of the Awesome Talks list. Thinking and learning about all kinds of technologies since forever.
Latest articles
Contract Tests - Parameterised Test Cases
Contract Tests - Abstract Test Cases
Contract Tests
The Testing Quadrant
Contact information
(+32) 496 38 00 82
info @ principal-it .be