Presentation for El Paso County IT on the concepts of Object-Relational Mapping and Dependency injection with a particular focus on the Microsoft Unity DI Container and the Microsoft Entity Framework. Delivered on October 2, 2012.
What is ORM?• Object Relational Mapping is a technique for converting between incompatible type systems in Object Oriented (OO) programming.• Primary use is to map between business objects in the application layer and a relational database.• Also can be used to map from business object models to data transfer objects (DTO) or a view model.
ORM Advantages and Disadvantages• Compared to traditional techniques of exchange between an object-oriented language and a relational database, ORM often reduces the amount of code that needs to be written.• Compared to traditional techniques, ORM greatly improves the speed of application development.• ORM tools add complexity in areas where proprietary, or database-specific techniques have been highly optimized.• Most ORM tools do not perform well during bulk deletions of data or joins.• Stored procedures may have better performance, but are not portable.• Object-relational impedance mismatch can result if the business object model does not closely match the relational data structure.
What is the Entity Framework (EF)?• EF is Microsoft’s recommended data access technology for new applications.• EF is an object-relational mapper that enables .NET developers to work with relational data using domain-specific objects.• Supported Databases • SQL Server • MySQL • Oracle • Sybase • Any other database that provides an ADO.NET data provider
EF Use Patterns• Database First• Model First• Code First • Code First Migrations
What is Inversion of Control (IoC)?• IoC is an object-oriented programming practice where the object coupling is bound at run time by an assembler object and is typically not known at compile time using static analysis.• Inversion of control as a design guideline serves the following purposes: • There is a decoupling of the execution of a certain task from implementation. • Every module can focus on what it is designed for. • Modules make no assumptions about what other systems do but rely on their contracts. • Replacing modules has no side effect on other modules.
What is Dependency Injection (DI)?• DI is a software design pattern that allows a choice of component to be made at run-time rather than compile time.• The binding of objects in the IoC pattern is accomplished through DI.• Dependency injection involves at least three elements: • A dependent consumer, • A declaration of a components dependencies, defined as interface contracts, • An injector (usually referred to as a container) that creates instances of classes that implement a given dependency interface on request.
Why use IoC and DI?• Encourages Separation of Concerns• Facilitates unit testing by allowing mock object with known behaviors to be injected• Promotes loose coupling between application layers, allowing implementations to be changed as necessary (i.e. mocks to SQL Server to Oracle)
What is Unity and Why Use It?• The Unity Application Block (Unity) is a lightweight extensible dependency injection container• Unity is developed and maintained by the Microsoft Patterns and Practices team• Unity is fully configurable using standard .NET config file patterns or through code initialization