SlideShare a Scribd company logo
Elements of DDD with
ASP.NET MVC &
Entity Framework Code First
Gabriel ENEA
Software developer / Maxcode.ro
Co-founder Joobs.ro – primul portal de joburi IT

07 may 2011
Updated:
Elements of software design


07 may 2011
Agenda Tags
# Unit Testing
# Enterprise Application Architecture
# Dependency Injection
# ASP.NET MVC 3
# Entity Framework 4.1 Code First
# Domain-Driven-Design
# Design Patterns
How do you start building an
application architecture?
Focus on?
 building an architecture from scratch
 thinking about how to achieve unit testing
 start with modeling the database schema and data relations
 using drag & drop programming
 modeling the domain entities, relations, business rules



 but, in the end, do you achieve 99,99% of unit testing?
Is unit testing realizable? 100%?
Yes or No? Who knows?

Maybe not! Possible answers:
 The customer doesn't understand this need
 Neither the management staff
 Instead, everyone expects you to write the perfect code

 As developers, every time we say: we need time to do it
  right!

 But, do we need time or we don't know how to achieve it?
Let's start thinking to architecture
design?
What? Right, now!?

Hey, we have only 1 hour to finish this
presentation! 



Indeed, but let's try to do something!
Some directions
Modeling approaches
  Database First Design
  Model First Design
Layers
  How do we design them?
Business rules
  Where and how do we implement?
Persistence
  Should we use an ORM?
Modeling approach - Pros/Cons
Database First Design
  doesn't focus on business rules, only on the way the
   data is represented


Model First Design
  Conceptual Design
   defines a conceptual model of the entities and relations
    (UML vs. Domain-Specific Languages)
  Code First Design
   starts writing code: classes, properties, associations,
    businesss rules
Layers

         Presentation


          Business


         Data Access
Presentation


Layers – any problems?                       Business


                                            Data Access



Layers Coupling!

A strong coupling conducts to a hard way to do:
   unit testing
   refactoring
   agile development
   or be opened for changes
Business rules
Where should these be located?
  Database
  Business layer
  User Interface (aka code behind!)


How do we test them?
  Running the application
  Automatically, maybe using unit tests
  Or we should let the customer test them!? 
And...what's inappropriate in this
code?
// somewhere in the business layer
...
public class Patient {
    public DateTime Birthdate { get; set; }

      public int Age { // computed value
        get {
           return DateTime.Now.Year - this.Birthdate.Year;
        }
      }                  Strong coupling!

      public bool IsAdult { // business rule
        get {
           return this.Age >= 18;
        }
      }
...
Let's start with a new approach...DDD
Domain-Driven-Design
 What is Domain?

A new default architecture where:
 the database is not the first focus
 the layers are loosely coupled
 the business rules are within the application Domain
 it is easier to achieve unit testing

 Why? Today we have the tools!
A new default architecture
Persistence
Requirements
 Persistence Ignorance (PI) / POCO
 Help Domain Model stay out of infrastructure stuff
 Decide where to store data
 Use code generation or an Object Relation (O/R) Mapper
   Metadata mapping
 Support for the Unit of Work pattern
Building blocks of Domain-Driven
Design
Today's tools
(from a web developer perspective)
ASP.NET MVC 3
  a mature web development platform based on
   MVC pattern
Entity Framework 4.1 Code First / NHibernate
  helps you focus on your domain
DI frameworks
  Manage dependencies
  Castle Windsor, StructureMap, Spring.NET, Unity, ...
Stop, What is DI?
 = DI.Equals(IoC); // true or false?

 IoC = Inversion of Control
 DI = Dependency Injection

 Helps you to decouple the application dependencies
   Logging mechanisms (log4net, Enterprise Library Logging
    Application Block, ...)
   Persistence mechanism (direct access to database, ORM)
   User Interface dependencies on Domain services
Dependency Injection

                   Log4netLogger


 PatientService



                   PatientRepositoy
Dependency Injection
                           1) creates
         Builder                         PatientService




                         Log4netLogger
2) inject dependencies                                    3) uses




                            ILogger
Persistance with EF 4.1 CodeFirst
Benefits
 Mapping based on predefined conventions
 Support for Query Object pattern (LINQ - IQuerable
  interface)
 Fluent API for manual mapping entities to tables, no
  more .edmx files
 Entity Validation
ASP.NET MVC 3 and DI support
 Based on MVC pattern
 Provides better support for IoC
   Views/Controllers


 Check IDependencyResolver interface
   simplify service location and dependency resolution


TService GetService<TService>() { … }

IEnumerable<TService> GetServices<TService>() { … }
Demo



What? We have a demo? Finally!
Conclusions
Focus on
  Analyze application dependencies
  Business rules
  Do refactoring!
  Design your Domain
  Don’t forget to do Unit testing
Questions?
Resources
Books
 Domain-Driven Design, Tackling Complexity in the
  Heart of Software, by Eric Evans
 Applying Domain-Driven Design and Patterns, With
  Examples in C# and .NET, by Jimmy Nilsson

Online resources
 http://domaindrivendesign.org/
 http://www.infoq.com/minibooks/domain-driven-design-quickly
The end…Thank you!


Elements of DDD with
ASP.NET MVC &
Entity Framework Code First
Gabriel ENEA
Software developer / Maxcode.ro
Co-founder Joobs.ro – primul portal de joburi IT

07 may 2011

More Related Content

What's hot (20)

PDF
Introduction to-ddd
John Ferguson Smart Limited
 
PPTX
Implementing DDD Concepts in PHP
Steve Rhoades
 
PPTX
Decoupling with Domain Events
Steven Smith
 
PPTX
How to Implement Domain Driven Design in Real Life SDLC
Abdul Karim
 
PDF
Domain driven design and model driven development
Dmitry Geyzersky
 
PDF
Hexagonal architecture - message-oriented software design
Matthias Noback
 
KEY
ZendCon 2011 UnCon Domain-Driven Design
Bradley Holt
 
PDF
Domain Driven Design
Harsh Jegadeesan
 
PPTX
EF6 and DDD
Hrvoje Hudoletnjak
 
PPTX
Domain Driven Design Quickly
Mariam Hakobyan
 
PPTX
CQRS recipes or how to cook your architecture
Thomas Jaskula
 
PDF
Refactoring for Domain Driven Design
David Berliner
 
PDF
Hexagonal architecture - message-oriented software design (PHP Barcelona 2015)
Matthias Noback
 
PPT
Real-world Entity Framework
Lynn Langit
 
PDF
Hexagonal
jglobal
 
PPTX
Introduction To Domain Driven Design
Paul Rayner
 
PPTX
Domain-Driven Design: The "What" and the "Why"
bincangteknologi
 
PDF
Hexagonal architecture - message-oriented software design (PHP Benelux 2016)
Matthias Noback
 
PPTX
Domain driven design
jstack
 
PDF
Hexagonal architecture in PHP
Paulo Victor Gomes
 
Introduction to-ddd
John Ferguson Smart Limited
 
Implementing DDD Concepts in PHP
Steve Rhoades
 
Decoupling with Domain Events
Steven Smith
 
How to Implement Domain Driven Design in Real Life SDLC
Abdul Karim
 
Domain driven design and model driven development
Dmitry Geyzersky
 
Hexagonal architecture - message-oriented software design
Matthias Noback
 
ZendCon 2011 UnCon Domain-Driven Design
Bradley Holt
 
Domain Driven Design
Harsh Jegadeesan
 
EF6 and DDD
Hrvoje Hudoletnjak
 
Domain Driven Design Quickly
Mariam Hakobyan
 
CQRS recipes or how to cook your architecture
Thomas Jaskula
 
Refactoring for Domain Driven Design
David Berliner
 
Hexagonal architecture - message-oriented software design (PHP Barcelona 2015)
Matthias Noback
 
Real-world Entity Framework
Lynn Langit
 
Hexagonal
jglobal
 
Introduction To Domain Driven Design
Paul Rayner
 
Domain-Driven Design: The "What" and the "Why"
bincangteknologi
 
Hexagonal architecture - message-oriented software design (PHP Benelux 2016)
Matthias Noback
 
Domain driven design
jstack
 
Hexagonal architecture in PHP
Paulo Victor Gomes
 

Similar to Elements of DDD with ASP.NET MVC & Entity Framework Code First (20)

PPTX
Domain Driven Design
Ryan Riley
 
PDF
DDD with ASP.NET MVC - Steve Smith | FalafelCON 2014
FalafelSoftware
 
PPTX
Introduction to DDD
Eduards Sizovs
 
PDF
Using ADO.NET Entity Framework in Domain Driven Design: A Pattern Approach
Hoan Phuc
 
PDF
Domain Driven Design
Mojammel Haque
 
PPTX
Schibsted Spain - Day 1 - DDD Course
Kevin Mas Ruiz
 
PPTX
Domain Driven Design
Muhammad Ali
 
PDF
D2 domain driven-design
Arnaud Bouchez
 
PPTX
A Practical Guide to Domain Driven Design: Presentation Slides
thinkddd
 
PDF
Domain-Driven Design
Geeks Anonymes
 
PPT
Domain Driven Design (DDD)
Tom Kocjan
 
PPTX
ASP.NET 3.5 SP1
Dave Allen
 
PPTX
Domain Driven Design(DDD) Presentation
Oğuzhan Soykan
 
PDF
Domain Driven Design Development Spring Portfolio
Srini Penchikala
 
PPTX
Domain Driven Design
Up2 Technology
 
PPT
Domain Driven Design Demonstrated
Alan Christensen
 
PPTX
Seminar - Scalable Enterprise Application Development Using DDD and CQRS
Mizanur Sarker
 
PPTX
.NET Fest 2019. Halil Ibrahim Kalkan. Implementing Domain Driven Design
NETFest
 
PPTX
Crafted Design - Sandro Mancuso
JAXLondon2014
 
Domain Driven Design
Ryan Riley
 
DDD with ASP.NET MVC - Steve Smith | FalafelCON 2014
FalafelSoftware
 
Introduction to DDD
Eduards Sizovs
 
Using ADO.NET Entity Framework in Domain Driven Design: A Pattern Approach
Hoan Phuc
 
Domain Driven Design
Mojammel Haque
 
Schibsted Spain - Day 1 - DDD Course
Kevin Mas Ruiz
 
Domain Driven Design
Muhammad Ali
 
D2 domain driven-design
Arnaud Bouchez
 
A Practical Guide to Domain Driven Design: Presentation Slides
thinkddd
 
Domain-Driven Design
Geeks Anonymes
 
Domain Driven Design (DDD)
Tom Kocjan
 
ASP.NET 3.5 SP1
Dave Allen
 
Domain Driven Design(DDD) Presentation
Oğuzhan Soykan
 
Domain Driven Design Development Spring Portfolio
Srini Penchikala
 
Domain Driven Design
Up2 Technology
 
Domain Driven Design Demonstrated
Alan Christensen
 
Seminar - Scalable Enterprise Application Development Using DDD and CQRS
Mizanur Sarker
 
.NET Fest 2019. Halil Ibrahim Kalkan. Implementing Domain Driven Design
NETFest
 
Crafted Design - Sandro Mancuso
JAXLondon2014
 
Ad

More from Enea Gabriel (10)

PPTX
Moving forward with ASP.NET Core
Enea Gabriel
 
PPTX
The future of ASP.NET / CodeCamp/Iasi 25 Oct 2014
Enea Gabriel
 
PPSX
Improving the availability of your website
Enea Gabriel
 
PPTX
Applying EF Code First at Your Job
Enea Gabriel
 
PPTX
Noutăţi în ASP.NET MVC 2
Enea Gabriel
 
PPTX
FII absolvent!
Enea Gabriel
 
PPTX
Introducere în ASP.NET MVC prin exemple
Enea Gabriel
 
PPTX
Magia testelor automate cu ASP.NET MVC
Enea Gabriel
 
PPTX
Rute cu ASP.NET MVC
Enea Gabriel
 
PPTX
Model View Controller și ASP.NET MVC + AJAX
Enea Gabriel
 
Moving forward with ASP.NET Core
Enea Gabriel
 
The future of ASP.NET / CodeCamp/Iasi 25 Oct 2014
Enea Gabriel
 
Improving the availability of your website
Enea Gabriel
 
Applying EF Code First at Your Job
Enea Gabriel
 
Noutăţi în ASP.NET MVC 2
Enea Gabriel
 
FII absolvent!
Enea Gabriel
 
Introducere în ASP.NET MVC prin exemple
Enea Gabriel
 
Magia testelor automate cu ASP.NET MVC
Enea Gabriel
 
Rute cu ASP.NET MVC
Enea Gabriel
 
Model View Controller și ASP.NET MVC + AJAX
Enea Gabriel
 
Ad

Recently uploaded (20)

PDF
Staying Human in a Machine- Accelerated World
Catalin Jora
 
PDF
ICONIQ State of AI Report 2025 - The Builder's Playbook
Razin Mustafiz
 
PDF
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
PDF
UPDF - AI PDF Editor & Converter Key Features
DealFuel
 
PPTX
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
PPTX
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PPTX
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
PDF
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
PPT
Ericsson LTE presentation SEMINAR 2010.ppt
npat3
 
DOCX
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PDF
🚀 Let’s Build Our First Slack Workflow! 🔧.pdf
SanjeetMishra29
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PDF
NASA A Researcher’s Guide to International Space Station : Physical Sciences ...
Dr. PANKAJ DHUSSA
 
PDF
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 
PDF
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
PDF
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 
PDF
SIZING YOUR AIR CONDITIONER---A PRACTICAL GUIDE.pdf
Muhammad Rizwan Akram
 
Staying Human in a Machine- Accelerated World
Catalin Jora
 
ICONIQ State of AI Report 2025 - The Builder's Playbook
Razin Mustafiz
 
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
UPDF - AI PDF Editor & Converter Key Features
DealFuel
 
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
Ericsson LTE presentation SEMINAR 2010.ppt
npat3
 
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
🚀 Let’s Build Our First Slack Workflow! 🔧.pdf
SanjeetMishra29
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
NASA A Researcher’s Guide to International Space Station : Physical Sciences ...
Dr. PANKAJ DHUSSA
 
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 
SIZING YOUR AIR CONDITIONER---A PRACTICAL GUIDE.pdf
Muhammad Rizwan Akram
 

Elements of DDD with ASP.NET MVC & Entity Framework Code First

  • 1. Elements of DDD with ASP.NET MVC & Entity Framework Code First Gabriel ENEA Software developer / Maxcode.ro Co-founder Joobs.ro – primul portal de joburi IT 07 may 2011
  • 2. Updated: Elements of software design 07 may 2011
  • 3. Agenda Tags # Unit Testing # Enterprise Application Architecture # Dependency Injection # ASP.NET MVC 3 # Entity Framework 4.1 Code First # Domain-Driven-Design # Design Patterns
  • 4. How do you start building an application architecture? Focus on?  building an architecture from scratch  thinking about how to achieve unit testing  start with modeling the database schema and data relations  using drag & drop programming  modeling the domain entities, relations, business rules  but, in the end, do you achieve 99,99% of unit testing?
  • 5. Is unit testing realizable? 100%? Yes or No? Who knows? Maybe not! Possible answers:  The customer doesn't understand this need  Neither the management staff  Instead, everyone expects you to write the perfect code  As developers, every time we say: we need time to do it right!  But, do we need time or we don't know how to achieve it?
  • 6. Let's start thinking to architecture design? What? Right, now!? Hey, we have only 1 hour to finish this presentation!  Indeed, but let's try to do something!
  • 7. Some directions Modeling approaches  Database First Design  Model First Design Layers  How do we design them? Business rules  Where and how do we implement? Persistence  Should we use an ORM?
  • 8. Modeling approach - Pros/Cons Database First Design  doesn't focus on business rules, only on the way the data is represented Model First Design  Conceptual Design  defines a conceptual model of the entities and relations (UML vs. Domain-Specific Languages)  Code First Design  starts writing code: classes, properties, associations, businesss rules
  • 9. Layers Presentation Business Data Access
  • 10. Presentation Layers – any problems? Business Data Access Layers Coupling! A strong coupling conducts to a hard way to do:  unit testing  refactoring  agile development  or be opened for changes
  • 11. Business rules Where should these be located?  Database  Business layer  User Interface (aka code behind!) How do we test them?  Running the application  Automatically, maybe using unit tests  Or we should let the customer test them!? 
  • 12. And...what's inappropriate in this code? // somewhere in the business layer ... public class Patient { public DateTime Birthdate { get; set; } public int Age { // computed value get { return DateTime.Now.Year - this.Birthdate.Year; } } Strong coupling! public bool IsAdult { // business rule get { return this.Age >= 18; } } ...
  • 13. Let's start with a new approach...DDD Domain-Driven-Design  What is Domain? A new default architecture where:  the database is not the first focus  the layers are loosely coupled  the business rules are within the application Domain  it is easier to achieve unit testing  Why? Today we have the tools!
  • 14. A new default architecture
  • 15. Persistence Requirements  Persistence Ignorance (PI) / POCO  Help Domain Model stay out of infrastructure stuff  Decide where to store data  Use code generation or an Object Relation (O/R) Mapper  Metadata mapping  Support for the Unit of Work pattern
  • 16. Building blocks of Domain-Driven Design
  • 17. Today's tools (from a web developer perspective) ASP.NET MVC 3  a mature web development platform based on MVC pattern Entity Framework 4.1 Code First / NHibernate  helps you focus on your domain DI frameworks  Manage dependencies  Castle Windsor, StructureMap, Spring.NET, Unity, ...
  • 18. Stop, What is DI?  = DI.Equals(IoC); // true or false?  IoC = Inversion of Control  DI = Dependency Injection  Helps you to decouple the application dependencies  Logging mechanisms (log4net, Enterprise Library Logging Application Block, ...)  Persistence mechanism (direct access to database, ORM)  User Interface dependencies on Domain services
  • 19. Dependency Injection Log4netLogger PatientService PatientRepositoy
  • 20. Dependency Injection 1) creates Builder PatientService Log4netLogger 2) inject dependencies 3) uses ILogger
  • 21. Persistance with EF 4.1 CodeFirst Benefits  Mapping based on predefined conventions  Support for Query Object pattern (LINQ - IQuerable interface)  Fluent API for manual mapping entities to tables, no more .edmx files  Entity Validation
  • 22. ASP.NET MVC 3 and DI support  Based on MVC pattern  Provides better support for IoC  Views/Controllers  Check IDependencyResolver interface  simplify service location and dependency resolution TService GetService<TService>() { … } IEnumerable<TService> GetServices<TService>() { … }
  • 23. Demo What? We have a demo? Finally!
  • 24. Conclusions Focus on  Analyze application dependencies  Business rules  Do refactoring!  Design your Domain  Don’t forget to do Unit testing
  • 26. Resources Books  Domain-Driven Design, Tackling Complexity in the Heart of Software, by Eric Evans  Applying Domain-Driven Design and Patterns, With Examples in C# and .NET, by Jimmy Nilsson Online resources  http://domaindrivendesign.org/  http://www.infoq.com/minibooks/domain-driven-design-quickly
  • 27. The end…Thank you! Elements of DDD with ASP.NET MVC & Entity Framework Code First Gabriel ENEA Software developer / Maxcode.ro Co-founder Joobs.ro – primul portal de joburi IT 07 may 2011