elements of ddd with asp.net mvc & entity framework code first v2

33
@itcampro / #itcampro Premium conference on Microsoft’s Dev and ITPro technologies Elements of DDD with ASP.NET MVC & Entity Framework Code First Gabriel ENEA, Technical Director MAXCODE.nl Co-founder Joobs.ro – the first IT job portal in Romania CodeCamp member / Iași [email protected] / gabrielenea.blogspot.com / @dotnet18

Upload: enea-gabriel

Post on 21-Nov-2014

8.346 views

Category:

Technology


7 download

DESCRIPTION

This is a presentation about the new Domain Driven Design architecture in combination with ASP.NET MVC 3 and Entity Framework 4.1 CodeFirst tools.The session was part of the IT Camp 2011 event (http://www.itcamp.ro).

TRANSCRIPT

Page 1: Elements of DDD with ASP.NET MVC & Entity Framework Code First v2

@itcampro / #itcampro Premium conference on Microsoft’s Dev and ITPro technologies

Elements of DDD with ASP.NET MVC &

Entity Framework Code First Gabriel ENEA, Technical Director

MAXCODE.nl

Co-founder Joobs.ro – the first IT job portal in Romania

CodeCamp member / Iași [email protected] / gabrielenea.blogspot.com / @dotnet18

Page 2: Elements of DDD with ASP.NET MVC & Entity Framework Code First v2

@itcampro / #itcampro Premium conference on Microsoft’s Dev and ITPro technologies

IT Camp 2011

• Thanks for coming!

• ITCamp is made possible by our sponsors:

Page 3: Elements of DDD with ASP.NET MVC & Entity Framework Code First v2

@itcampro / #itcampro Premium conference on Microsoft’s Dev and ITPro technologies

Agenda

# Unit Testing challenges

# Today Architectural Design

# New approach: Domain-Driven-Design

# Today’s Tools (@web)

# Demo

• Q&A

Page 4: Elements of DDD with ASP.NET MVC & Entity Framework Code First v2

@itcampro / #itcampro Premium conference on Microsoft’s Dev and ITPro technologies

DEMO

Application requirements

Page 5: Elements of DDD with ASP.NET MVC & Entity Framework Code First v2

@itcampro / #itcampro Premium conference on Microsoft’s Dev and ITPro technologies

UNIT TESTING CHALLENGES

Page 6: Elements of DDD with ASP.NET MVC & Entity Framework Code First v2

@itcampro / #itcampro Premium conference on Microsoft’s Dev and ITPro technologies

How do you test it?

Page 7: Elements of DDD with ASP.NET MVC & Entity Framework Code First v2

@itcampro / #itcampro Premium conference on Microsoft’s Dev and ITPro technologies

Testing on components

Page 8: Elements of DDD with ASP.NET MVC & Entity Framework Code First v2

@itcampro / #itcampro Premium conference on Microsoft’s Dev and ITPro technologies

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

• reusing existing code?

• but, in the end, do you achieve 99,99% test code coverage?

Page 9: Elements of DDD with ASP.NET MVC & Entity Framework Code First v2

@itcampro / #itcampro Premium conference on Microsoft’s Dev and ITPro technologies

Is unit testing achievable? 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?

Page 10: Elements of DDD with ASP.NET MVC & Entity Framework Code First v2

@itcampro / #itcampro Premium conference on Microsoft’s Dev and ITPro technologies

TODAY ARCHITECTURAL DESIGN

Page 11: Elements of DDD with ASP.NET MVC & Entity Framework Code First v2

@itcampro / #itcampro Premium conference on Microsoft’s Dev and ITPro technologies

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!

Page 12: Elements of DDD with ASP.NET MVC & Entity Framework Code First v2

@itcampro / #itcampro Premium conference on Microsoft’s Dev and ITPro technologies

What stops 100% unit testing?

1. Layers

– How do we design them?

2. Business rules

– Where and how do we implement?

3. Persistence

– Should we use an ORM?

Page 13: Elements of DDD with ASP.NET MVC & Entity Framework Code First v2

@itcampro / #itcampro Premium conference on Microsoft’s Dev and ITPro technologies

1 - Layers

Data Access

Business

Presentation

Page 14: Elements of DDD with ASP.NET MVC & Entity Framework Code First v2

@itcampro / #itcampro Premium conference on Microsoft’s Dev and ITPro technologies

Data Access

Business

Presentation

1 - Layers – any problems?

Layers Coupling!

A strong coupling conducts to a hard way to do:

– unit testing

– refactoring

– agile development

– or be opened for changes

Page 15: Elements of DDD with ASP.NET MVC & Entity Framework Code First v2

@itcampro / #itcampro Premium conference on Microsoft’s Dev and ITPro technologies

2 - 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!?

Page 16: Elements of DDD with ASP.NET MVC & Entity Framework Code First v2

@itcampro / #itcampro Premium conference on Microsoft’s Dev and ITPro technologies

And...what's inappropriate here?

// 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;

}

}

public bool IsAdult { // business rule

get {

return this.Age >= 18;

}

}

...

Strong coupling!

Page 17: Elements of DDD with ASP.NET MVC & Entity Framework Code First v2

@itcampro / #itcampro Premium conference on Microsoft’s Dev and ITPro technologies

3 - Persistence

Requirements

• Persistence Ignorance / POCO

• Help Domain Model stay out of infrastructure stuff

• Decide where to store data (NoSQL?)

• Use code generation or an Object Relation (O/R) Mapper – Metadata mapping

• Support for the Unit of Work pattern

Page 18: Elements of DDD with ASP.NET MVC & Entity Framework Code First v2

@itcampro / #itcampro Premium conference on Microsoft’s Dev and ITPro technologies

NEW APPROACH: DDD

Page 19: Elements of DDD with ASP.NET MVC & Entity Framework Code First v2

@itcampro / #itcampro Premium conference on Microsoft’s Dev and ITPro technologies

Let's start with a new approach... 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!

Page 20: Elements of DDD with ASP.NET MVC & Entity Framework Code First v2

@itcampro / #itcampro Premium conference on Microsoft’s Dev and ITPro technologies

A new default architecture - DDD

Page 21: Elements of DDD with ASP.NET MVC & Entity Framework Code First v2

@itcampro / #itcampro Premium conference on Microsoft’s Dev and ITPro technologies

Building blocks of DDD

Page 22: Elements of DDD with ASP.NET MVC & Entity Framework Code First v2

@itcampro / #itcampro Premium conference on Microsoft’s Dev and ITPro technologies

TODAY'S TOOLS

Page 23: Elements of DDD with ASP.NET MVC & Entity Framework Code First v2

@itcampro / #itcampro Premium conference on Microsoft’s Dev and ITPro technologies

Today's tools (from a web developer perspective)

Dependency Injection frameworks

– Manage dependencies

– Castle Windsor, StructureMap, Spring.NET, Unity, ...

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

Page 24: Elements of DDD with ASP.NET MVC & Entity Framework Code First v2

@itcampro / #itcampro Premium conference on Microsoft’s Dev and ITPro technologies

What is Dependency Injection?

• = 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

– Layers

Page 25: Elements of DDD with ASP.NET MVC & Entity Framework Code First v2

@itcampro / #itcampro Premium conference on Microsoft’s Dev and ITPro technologies

Dependency Injection

PatientService

Log4netLogger

PatientRepositoy

Page 26: Elements of DDD with ASP.NET MVC & Entity Framework Code First v2

@itcampro / #itcampro Premium conference on Microsoft’s Dev and ITPro technologies

Dependency Injection

Builder PatientService

Log4netLogger

ILogger

3) uses 2) inject dependencies

1) creates

Page 27: Elements of DDD with ASP.NET MVC & Entity Framework Code First v2

@itcampro / #itcampro Premium conference on Microsoft’s Dev and ITPro technologies

ASP.NET MVC 3 and DI support

• Based on MVC pattern

• Provides better support for IoC – Views/Controllers

• Model Validation support

• Check IDependencyResolver interface – simplify service location and dependency resolution

TService GetService<TService>() { … }

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

Page 28: Elements of DDD with ASP.NET MVC & Entity Framework Code First v2

@itcampro / #itcampro Premium conference on Microsoft’s Dev and ITPro technologies

Persistance with EF 4.1 CodeFirst

1st version benefits:

• CodeFirst development

• Better POCO support

• Mapping based on predefined conventions

(Convention over configuration)

• Fluent API for manual mapping entities to tables, no more .edmx files

• Built-in Model-Level Validation

• Dynamic database creation and dropping

Page 29: Elements of DDD with ASP.NET MVC & Entity Framework Code First v2

@itcampro / #itcampro Premium conference on Microsoft’s Dev and ITPro technologies

DEMO

DDD architecture with ASP.NET MVC 3, Unity, Entity Framework CodeFirst 4.1

Page 30: Elements of DDD with ASP.NET MVC & Entity Framework Code First v2

@itcampro / #itcampro Premium conference on Microsoft’s Dev and ITPro technologies

Conclusions

Focus on

– Analyze application dependencies

– Business rules

– Do refactoring!

– Design your Domain

– Don’t forget to do Unit testing

Page 31: Elements of DDD with ASP.NET MVC & Entity Framework Code First v2

@itcampro / #itcampro Premium conference on Microsoft’s Dev and ITPro technologies

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

Page 32: Elements of DDD with ASP.NET MVC & Entity Framework Code First v2

@itcampro / #itcampro Premium conference on Microsoft’s Dev and ITPro technologies

Q&A

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

Page 33: Elements of DDD with ASP.NET MVC & Entity Framework Code First v2

@itcampro / #itcampro Premium conference on Microsoft’s Dev and ITPro technologies

Don’t forget!

Get your free Azure pass!

• 30+15 days, no CC req’d

– http://bit.ly/ITCAMP11

– Promo code: ITCAMP11

We want your feedback!

• Win a WP7 smartphone

– Fill in your feedback forms

– Raffle: end of the day