Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Learning ASP.NET Core Best Practices With An Opensource Project

Learning ASP.NET Core Best Practices With An Opensource Project

RawCms is an open source headless CMS in asp.net. Complete this project was a challenge due to the hard requirements. During the speech, I will introduce the architecture and the issues we found, explain how to solve them and how to use such knowledge in projects every day. By the technical part, what is behind this project is the best setup for all asp.net core projects keeping in mind modern requirements. Keeping the RawCms experience in the background, during the speech will be presented a best-practice architecture, covering:

- Authentication (Oauth2 with a build-in server or federated via introspection)
- Interaction between SQL and NO SQL database
- Plugin system in C#
- Hooking system
- AOP and code automation

Daniele Fontani

March 10, 2020
Tweet

More Decks by Daniele Fontani

Other Decks in Programming

Transcript

  1. SINTRA DIGITAL BUSINESS
    Learning ASP.NET Core Best Practices
    With An Opensource Project
    1

    View full-size slide

  2. SINTRA DIGITAL BUSINESS 2
    HELLO EVERYONE
    Until Covid-19 do us apart!

    View full-size slide

  3. SINTRA DIGITAL BUSINESS
    MYSELF IN THREE PICTURES
    A picture is worth a thousand words
    There is a world outside the internet!
    TREKKING
    The better sandbox for learning!
    OPENSOURCE
    The place where I do my best
    WORK
    3

    View full-size slide

  4. SINTRA DIGITAL BUSINESS
    MYSELF IN SOCIAL NETWORKS
    Building better tomorrow. Trekker inside. CTO interested in
    #CloudComputing, #AgileDevelopment and #Opensource
    4
    https://www.linkedin.com/in/daniele-fontani/
    https://medium.com/@daniele.fontani
    https://twitter.com/zeppaman
    https://github.com/zeppaman
    [email protected]

    View full-size slide

  5. SINTRA DIGITAL BUSINESS
    Why opensource is the best env for developing new skills
    From opensource to open mind
    All the bricks needed for an enterprise asp.net application
    Best practices
    What was my test case
    The gym
    5
    AGENDA

    View full-size slide

  6. SINTRA DIGITAL BUSINESS 6
    FROM OPENSOURCE
    TO
    OPEN MIND

    View full-size slide

  7. SINTRA DIGITAL BUSINESS
    You give you software for free, without patents
    Unsuccessful business model
    It’s just a loose of time
    Opensource is for losers
    Opensource? is simply Linux
    Ignorance
    7
    IN 2006

    View full-size slide

  8. SINTRA DIGITAL BUSINESS
    Learn new things, compare yourself with others
    CHALLENGING
    I give to the community for what I take
    ETHICAL
    Give me a lot, not money, but friends and knowledge
    REWARDING
    8
    THEN MY FIRST EXPERIENCE

    View full-size slide

  9. SINTRA DIGITAL BUSINESS
    I've never stopped.
    9
    AND THEN...
    I've never stopped

    View full-size slide

  10. SINTRA DIGITAL BUSINESS 10
    THE GYM

    View full-size slide

  11. SINTRA DIGITAL BUSINESS
    DEMO
    http://rawcms-demo.herokuapp.com/
    PROJECT PAGE
    https://github.com/arduosoft/RawCMS
    RAWCMS
    11
    11

    View full-size slide

  12. SINTRA DIGITAL BUSINESS 12
    BEST PRACTICES

    View full-size slide

  13. SINTRA DIGITAL BUSINESS
    Divide et impera
    MICROSERVICES
    One app, many customer
    MULTITENANCY
    One login many app
    IDENTITY
    World is Async
    BACKGROUND JOBS
    Get best from all
    MULTIPLE DATABASES
    Make things happens by
    design
    AOP
    BEST PRACTICES ROADMAP
    13
    For extension
    PLUGINs

    View full-size slide

  14. SINTRA DIGITAL BUSINESS
    PLUGINS
    Extend you application
    adding a deployable
    artifact.
    14

    View full-size slide

  15. SINTRA DIGITAL BUSINESS 15
    WHAT WE NEED

    View full-size slide

  16. SINTRA DIGITAL BUSINESS 16
    WHY PLUGIN IN THE MICROSERVICES ERA?
    There are some cases when microservices are hard to
    implement:
    ● Old codebase
    ● Background jobs
    ● Completely coupled business logic
    ● Very small application domains
    ● On-Prem Products WE ARE HERE

    View full-size slide

  17. SINTRA DIGITAL BUSINESS 17
    THE BASICS

    View full-size slide

  18. SINTRA DIGITAL BUSINESS
    Divide et impera
    MICROSERVICES
    One app, many customer
    MULTITENANCY
    One login many app
    IDENTITY
    World is Async
    BACKGROUND JOBS
    Get best from all
    MULTIPLE DATABASES
    Make things happens by
    design
    AOP
    BEST PRACTICES ROADMAP
    18
    For extension
    PLUGINs

    View full-size slide

  19. SINTRA DIGITAL BUSINESS
    AOP
    In computing, aspect-oriented programming (AOP) is a
    programming paradigm that aims to increase modularity by
    allowing the separation of cross-cutting concerns. It does so
    by adding additional behavior to existing code (an advice)
    without modifying the code itself […] This allows behaviors
    that are not central to the business logic (such as logging) to
    be added to a program without cluttering the code, core to
    the functionality.
    https://en.wikipedia.org/wiki/Aspect-oriented_programming
    19

    View full-size slide

  20. SINTRA DIGITAL BUSINESS 20
    IN SIMPLE WORDS
    Make things happen without writing
    code.

    View full-size slide

  21. SINTRA DIGITAL BUSINESS 21
    THE LOGGING EXAMPLE
    BEFORE AOP AFTER AOP

    View full-size slide

  22. SINTRA DIGITAL BUSINESS 22
    THE DATA EXAMPLE
    BEFORE AOP AFTER AOP

    View full-size slide

  23. SINTRA DIGITAL BUSINESS 23
    HOW IT IS POSSIBLE
    ProxyDispatcher

    View full-size slide

  24. SINTRA DIGITAL BUSINESS 24
    IN 34 LINES OF CODE
    FULL EXAMPLE ON https://github.com/zeppaman/csharp-aop

    View full-size slide

  25. SINTRA DIGITAL BUSINESS 25
    I CAN'T BELIEVE IT
    1 MINUTE DEMO

    View full-size slide

  26. SINTRA DIGITAL BUSINESS 26
    MY THOUGHT ABOUT AOP IN C#
    ● Its' ok for logging and measuring (but with a good control on what is
    logged or not). Pay attention to the C# limit: only virtual method or
    interface implementation can be intercepted.
    ● For the other application you cannot do yourself. You need a
    framework like Spring in Java, but we are on the other side.

    View full-size slide

  27. SINTRA DIGITAL BUSINESS
    Divide et impera
    MICROSERVICES
    One app, many customer
    MULTITENANCY
    One login many app
    IDENTITY
    World is Async
    BACKGROUND JOBS
    Get best from all
    MULTIPLE DATABASES
    Make things happens by
    design
    AOP
    BEST PRACTICES ROADMAP
    27
    For extension
    PLUGINs

    View full-size slide

  28. SINTRA DIGITAL BUSINESS
    BACKGROUND
    JOBS
    28
    Web can’t wait for you

    View full-size slide

  29. SINTRA DIGITAL BUSINESS 29
    SYNC IS DEAD LONG LIVE TO ASYNC!
    In 2020 most scenario need to be async, we have to live with it.
    This means every web application is also a background
    worker.
    Note:We may have a microservice application where you
    invoke by api the job on other server(container), but the
    application that host background job... is still a web application
    because have to be invoked by API!

    View full-size slide

  30. SINTRA DIGITAL BUSINESS 30
    WHY PUT IT INSIDE A WEB APPLICATION?

    View full-size slide

  31. SINTRA DIGITAL BUSINESS
    THE CHOICE
    Native, but just a process runner
    Based on service definition
    Native solution
    A complete scheduling solution
    Based on fluent code
    Hangfire
    31

    View full-size slide

  32. SINTRA DIGITAL BUSINESS
    HANGFIRE
    32
    •Persistency
    •Balancing on multiple
    server
    •Dashboard
    •Authentication
    •Possibility to run processes
    manually
    What else?

    View full-size slide

  33. SINTRA DIGITAL BUSINESS 33
    I CAN’T BELIEVE IT
    1 MINUTE DEMO

    View full-size slide

  34. SINTRA DIGITAL BUSINESS
    Divide et impera
    MICROSERVICES
    One app, many customer
    MULTITENANCY
    One login many app
    IDENTITY
    World is Async
    BACKGROUND JOBS
    Get best from all
    MULTIPLE DATABASES
    Make things happens by
    design
    AOP
    BEST PRACTICES ROADMAP
    34
    For extension
    PLUGINs

    View full-size slide

  35. SINTRA DIGITAL BUSINESS
    IDENTITY
    Cyclops, you asked about my famous name.
    I’ll tell you. Then you can offer me a gift,
    as your guest. My name is Nobody.
    My father and mother, all my other friends—
    they call me Nobody.
    Odysseus and Polyphemus - Odyssey
    35

    View full-size slide

  36. SINTRA DIGITAL BUSINESS 36
    WHAT IS NOT ACCEPTABLE ON 2020
    ● INSERT YOUR PASSWORD TWICE
    ● HAVE DIFFERENT ACCOUNT FOR DIFFERENT
    APPLICATION
    ● USE SERVICE ACCOUNTS THAT OPERATES ON
    BEHALF OF YOU
    ● IMPLEMENT IT BY YOURSELF

    View full-size slide

  37. SINTRA DIGITAL BUSINESS 37
    SCENARIOS
    WITH IDS START EASY AND EVOLVE LATER

    View full-size slide

  38. SINTRA DIGITAL BUSINESS 38
    SCENARIOS
    FEDERATION STANDALONE

    View full-size slide

  39. SINTRA DIGITAL BUSINESS
    Divide et impera
    MICROSERVICES
    One app, many customer
    MULTITENANCY
    One login many app
    IDENTITY
    World is Async
    BACKGROUND JOBS
    Get best from all
    MULTIPLE DATABASES
    Make things happens by
    design
    AOP
    BEST PRACTICES ROADMAP
    39
    For extension
    PLUGINs

    View full-size slide

  40. SINTRA DIGITAL BUSINESS
    MULTIPLE
    DATABASE
    Keeping same data in
    multiple places
    40

    View full-size slide

  41. SINTRA DIGITAL BUSINESS 41
    WHY
    ● Pick the best from each tool
    ● RDBMS may be not enough in 2020
    If you are fighting against tanks, you don’t need a swiss army.
    You need the army.

    View full-size slide

  42. SINTRA DIGITAL BUSINESS 42
    HOW
    You can use event listener on Hibernate, the OnSave method on
    Entity framework, or in case you are not using a orm.. Well, in thi
    case you need to rewrite the app and the full text search Is the
    last of your problems
    AUTOMATIC

    View full-size slide

  43. SINTRA DIGITAL BUSINESS 43
    IN CODE WORDS

    View full-size slide

  44. SINTRA DIGITAL BUSINESS
    Divide et impera
    MICROSERVICES
    One app, many customer
    MULTITENANCY
    One login many app
    IDENTITY
    World is Async
    BACKGROUND JOBS
    Get best from all
    MULTIPLE DATABASES
    Make things happens by
    design
    AOP
    BEST PRACTICES ROADMAP
    44
    For extension
    PLUGINs

    View full-size slide

  45. SINTRA DIGITAL BUSINESS
    MULTITENCANCY
    One application,
    multiple customers
    45

    View full-size slide

  46. SINTRA DIGITAL BUSINESS 46
    SCENARIOS
    Data isolation can be theoretically done at all levels
    (row, table, database, server) but only this make
    sense:
    ● Put all data in the same table, but separate
    filtering basing on user (not GDPR compliant,
    not performant).
    ● Use different database (with the options to have
    multiple servers)
    Options

    View full-size slide

  47. SINTRA DIGITAL BUSINESS 47
    WHY THE HELL I NEED MULTI TENANCY?
    It is not really needed in B2B application, but it is a common
    scenario when:
    ● You have multiple customer on same infrastructure
    (SaaS)
    ● Your are designing a product
    ● You may transform your custom application to a product
    or platform for many customer
    ● You hold sensitive data and GDPR ask you for security

    View full-size slide

  48. SINTRA DIGITAL BUSINESS 48
    HOW TO IMPLEMENT IT
    Dependency Injection is your friend

    View full-size slide

  49. SINTRA DIGITAL BUSINESS 49
    IN CODE WORDS
    CONFIGURATION USAGE

    View full-size slide

  50. SINTRA DIGITAL BUSINESS 50
    OR DO NOT REINVENT THE WHEEL AUTOFAC
    TENANT NAME DETECTION POLICY
    MANAGE PER TENANT DIFFERENCES

    View full-size slide

  51. SINTRA DIGITAL BUSINESS
    Divide et impera
    MICROSERVICES
    One app, many customer
    MULTITENANCY
    One login many app
    IDENTITY
    World is Async
    BACKGROUND JOBS
    Get best from all
    MULTIPLE DATABASES
    Make things happens by
    design
    AOP
    BEST PRACTICES ROADMAP
    51
    For extension
    PLUGINs

    View full-size slide

  52. SINTRA DIGITAL BUSINESS
    MICROSERVICES
    Divide et impera
    52

    View full-size slide

  53. SINTRA DIGITAL BUSINESS 53
    WHAT IS MICROSERVICES?
    The Chimera of Arezzo, Etruscan artwork.
    Now conserved in Florence.
    The Chimera
    Everybody try to implement microservices,
    but how micro they have to be is not said.
    A myth or reality?

    View full-size slide

  54. SINTRA DIGITAL BUSINESS 54
    WHY MICROSERVICES ARE A CHIMERA?
    ● The size of each microservices depends upon the business
    logic complexity and dependency. You microservice could be
    not so micro
    ● Reusing of common part is not easy on a distributed system. If
    the common layer changes quickly you finish for having a
    monolithic system splitted into a lot of part
    Problems
    ● A good analysis helps
    ● Use containers, always.
    ● Start with a good architecture (oauth2 with IDS, job server,...).
    Solutions

    View full-size slide

  55. SINTRA DIGITAL BUSINESS
    Divide et impera
    MICROSERVICES
    One app, many customer
    MULTITENANCY
    One login many app
    IDENTITY
    World is Async
    BACKGROUND JOBS
    Get best from all
    MULTIPLE DATABASES
    Make things happens by
    design
    AOP
    BEST PRACTICES ROADMAP
    55
    For extension
    PLUGINs
    ALL DONE!

    View full-size slide

  56. SINTRA DIGITAL BUSINESS 56
    WHAT TO TAKE HOME

    View full-size slide

  57. SINTRA DIGITAL BUSINESS 57
    POINT OF INTEREST
    ● Opensource is not for losers, but a real allied for become a
    better programmer.
    ● Working on challenging projects make you a better
    programmer.
    ● Lot of optional stuff are now included, don’t reinvent the
    wheel

    View full-size slide

  58. SINTRA DIGITAL BUSINESS 58
    REFERENCES
    ● SOURCE CODE https://github.com/zeppaman/aspnet-best-practices
    ● RAWCMS https://github.com/arduosoft/RawCMS
    ● AOP https://levelup.gitconnected.com/aop-dotnet-applications-67c6d94c08b0
    ● THIS PRESENTATION
    https://speakerdeck.com/zeppaman/learning-asp-dot-net-core-best-practices-w
    ith-an-opensource-project

    View full-size slide

  59. SINTRA DIGITAL BUSINESS 59
    THANK YOU

    View full-size slide