1 of 36

The Framework Features Anti-Pattern

examples in ASP.NET / Core

@seangwright

2 of 36

Hello!

About me:

Tweet at / follow me here

Who am I? I’m Sean G. Wright.

What do I do? I make marketing, business tools and ecommerce stuff with ASP.NET and Angular, consulting and devops/hosting duties.

Where do I work? I’m a dev lead at WiredViews, a web development/marketing agency in Akron.

What’s new in my life? I just installed a new storm-door in the front of my house. It has a retractable screen. It’s like being outside.

@seangwright

3 of 36

Please interrupt me :)

Comments, questions and digressions are welcomed!

@seangwright

4 of 36

What is a Pattern?

@seangwright

5 of 36

Pattern: some definitions

  • general, reusable solution to a commonly occurring problem within a given context��formalized best practiceshttps://en.wikipedia.org/wiki/Software_design_pattern

@seangwright

6 of 36

What is an Anti-Pattern?

@seangwright

7 of 36

Anti-Pattern: some definitions

  • There must be two key elements to distinguish an anti-pattern from a bad habit:��A commonly used process, structure, or pattern of action that, despite initially appearing to be an appropriate and effective response to a problem, has more bad consequences than good ones��Another solution exists that is documented, repeatable, and proven to be effectivehttps://en.wikipedia.org/wiki/Anti-pattern
  • sensible people will take the path - only once you've put a lot of effort into it will you know it's a bad result��the same solution can be a good pattern in some contexts and an antipattern in othershttps://www.martinfowler.com/bliki/AntiPattern.html

@seangwright

8 of 36

You’re a sensible person, but you probably thought this was a good idea at one point

@seangwright

9 of 36

What is a framework?

@seangwright

10 of 36

Framework: some definitions

  • software providing generic functionality can be selectively changed by additional user-written code��inversion of control, extensible, not modifiable (O, D in SOLID)�https://en.wikipedia.org/wiki/Software_framework
  • application registers with the framework (note these are separate)��framework calls into the application��A framework often exists to address a particular �general-purpose Domain �(such as web applications, mobile apps, workflows, etc.)�http://blog.ploeh.dk/2014/05/19/di-friendly-framework/

@seangwright

11 of 36

A framework calls you! Your application is features and business logic, not the framework.

@seangwright

12 of 36

What is a Feature?

@seangwright

13 of 36

Feature: some definitions

  • "A distinguishing characteristic of a software item (e.g., performance, portability, or functionality)."�https://en.wikipedia.org/wiki/Software_feature

@seangwright

14 of 36

Comments or questions?

@seangwright

15 of 36

Framework:��ASP.NET MVC 5

Features:

  • Models, Views, Controllers
  • Conventions over configuration

  • Razor templating engine
  • Service Locator
  • Model binding

  • Routing patterns
  • Models,Views,Controllers

@seangwright

16 of 36

Focusing on the Framework’s features was an Anti-Pattern

@seangwright

17 of 36

Some project layout examples

@seangwright

18 of 36

@seangwright

19 of 36

Some other examples of building by framework (or architectural) features

Building by Framework features

Building by Architectural features

@seangwright

20 of 36

File -> New Project -> ASP.NET Core -> MVC (with User Accounts)

Did they fix it in ASP.NET Core?

Nope!

… well kinda

@seangwright

21 of 36

New Razor Pages project (ASP.NET Core 2.1+)

File -> �New Project -> �ASP.NET Core ->�Web Application (Razor Pages)

Much better!

Want a Resuable/Composable UI?

Razor Class Library!

https://docs.microsoft.com/en-us/aspnet/core/mvc/razor-pages/ui-class?view=aspnetcore-2.1&tabs=visual-studio

@seangwright

22 of 36

Comments or questions?

@seangwright

23 of 36

Why focus on your business’s features?

  • Developers are usually tasked with building or working on a business feature (UserAccounts), not a layer or framework feature (Controllers).
    • What do your user stories look like?�
  • Namespaces begin to represent feature dependencies.
    • Company.Web.Features.UserAccount vs Company.Web.Controllers
  • Hiding unfinished features via feature toggling is easier to keep consistent when you are already thinking in terms of features.

@seangwright

24 of 36

Why focus on your business’s features?

  • When reviewing PRs, source control history it becomes easier to pinpoint unnecessary or incorrect changes.
    • Why is Company.Web.Features.ProductCoupons.ProductCouponController changed when this PR is for the UserAccount feature?
  • It’s easier for new developers to understand and explore your project.
    • Do you want to see all Controllers or all Authorization?
  • Even if you have teams working on layers (Frontend and Backend), they can learn how their part of the feature integrates with the other team’s.
    • Frontend devs can debug an operation moving through the application for their feature even if they don’t write C Pound.

@seangwright

25 of 36

Why focus on your business’s features?

  • It’s easier to tell when a feature is becoming too complex based on the number of files in a feature folder.
    • Far more useful than making decisions based on the number of Controllers in your application.
  • Architecture tends towards less coupling between layers (per feature) and more cohesion within features.
    • Easier to re-architect a feature in isolation or have multiple variations of a feature (A/B, perf testing).
    • Separate layers at the project level, not the folder level.

@seangwright

26 of 36

Comments or questions?

@seangwright

27 of 36

Now for some code ...

@seangwright

28 of 36

Some thoughts

  • The ‘Framework Features’ project structure is a little like Hungarian notation for variables and types.
  • The .NET framework has saddled us with some anti-pattern conventions we still haven’t escaped (Async suffix on Task returning methods, I prefix on Interface types).��Don’t be a victim of cargo cult programming. Identify anti-patterns through experience.
  • Framework architects + Stroustrup's Rule
  • What causes this kind of anti-pattern?

@seangwright

29 of 36

Some thoughts

  • Try bringing an ‘application feature’ first mindset to other areas of your work
    • Using a literal ‘Features’ folder is not the key, focusing on your application features is.�
    • Client side code or assets (.ts, .js. .css, .png, .woff).�
    • Unit Test projects structure App.Web.Tests/Features/UserAccounts/UserAccountTests.cs
    • Class libraries
      • When features get too large, move them to their own project.
  • When you first start using a framework, your view on it tends to be ‘What can this framework do for me?” The focus here is on the framework features.��After becoming experienced with it your view changes to “What can I do with this framework?” The focus here is on the application features you will build.

@seangwright

30 of 36

Links and references (.NET)

@seangwright

31 of 36

Links and references (.NET)

@seangwright

32 of 36

Links and references (.NET Core)

@seangwright

33 of 36

Links and references (Other)

@seangwright

34 of 36

In Summary

  • Beware of Anti-Patterns
    • Seem like good ideas
    • Context dependent
    • Avoided through experience
  • Use Frameworks to help you do more
    • They are tools
    • Use their features
    • Do not let the framework use you (like in Soviet Russia)
  • Focus on your business’s / application features
    • They are what actually make you money
    • This helps your team grow, understand and maintain your code

@seangwright

35 of 36

Comments or questions?

… you thought there’d be a funny gif here, didn’t you?

@seangwright

36 of 36

No more slides.

Thanks!

@seangwright