background preloader

Inversion of Control Containers and the Dependency Injection pattern

Inversion of Control Containers and the Dependency Injection pattern
In the Java community there's been a rush of lightweight containers that help to assemble components from different projects into a cohesive application. Underlying these containers is a common pattern to how they perform the wiring, a concept they refer under the very generic name of "Inversion of Control". In this article I dig into how this pattern works, under the more specific name of "Dependency Injection", and contrast it with the Service Locator alternative. The choice between them is less important than the principle of separating configuration from use. One of the entertaining things about the enterprise Java world is the huge amount of activity in building alternatives to the mainstream J2EE technologies, much of it happening in open source. Underlying these containers are a number of interesting design principles, things that go beyond both these specific containers and indeed the Java platform. Components and Services A Naive Example class MovieLister... Inversion of Control

Understanding IoC Container - sfeldman.NET Thursday, February 14, 2008 10:58 PM Sean Feldman In a multi layered application architecture, loosely coupled code is more than a important. It's the basic which can either help the entire project progress, or drive it down the slope to the end (in the bad meaning of the word). One of the basics to keep coupling as low as possible is Inversion of Control (IoC) container. I will try to show how to put in place a simple version of IoC container to allow loosely coupled design. In our application we do basic logging at all layers. public interface ILogger { void Log(string message); } Lets assume that the initial version of logger is implemented as a simple Console logger: public class ConsoleLogger : ILogger { public void Log(string message) { System.Console.WriteLine(message); } } Now lets look what the layered structure looks like. public interface IGadget { void TurnOn(); void TurnOff(); } Now the implementation of the Gadget will be quiet simple: 1: using Core; 3: namespace AssemblyOne

flickrVIEWR – A flickr viewer in Silverlight – Part 1 I’m back! I’ve had my work up to my eyeballs the last couple of weeks. Apparently moving to the other side of the world and starting a new job takes a lot of time. So after that excuse, it is time to get started with my new project. My fiancée has this blog where she blogs about our new life in NZ. And this is interesting to you because…? The final result will look like below. The first step in creating a flickr viewer is obviously to have a look at the flickr API. flickr has several APIs, but I have decided to go for the very simple REST API. To support the 2 different selection modes, I had to get familiar with 3 different methods in the flickr API, flickr.photosets.getPhotos, flickr.photos.search and flickr.photos.getInfo. As you can see (if you didn’t just skip my url definition above), you need something called an API key. So after having had a little look around flickr and taking about 30 seconds to think I came up with the following plan. The service will be very simple.

3 ways to define a JavaScript class / Stoyan's phpied.com Introduction JavaScript is a very flexible object-oriented language when it comes to syntax. In this article you can find three ways of defining and instantiating an object. Even if you have already picked your favorite way of doing it, it helps to know some alternatives in order to read other people's code. It's important to note that there are no classes in JavaScript. 1. This is probably one of the most common ways. function Apple (type) { this.type = type; this.color = "red"; this.getInfo = getAppleInfo; } function getAppleInfo() { return this.color + ' ' + this.type + ' apple'; } To instantiate an object using the Apple constructor function, set some properties and call methods you can do the following: var apple = new Apple('macintosh'); apple.color = "reddish"; alert(apple.getInfo()); 1.1. In the example above you see that the method getInfo() of the Apple "class" was defined in a separate function getAppleInfo(). 1.2. 2. apple.color = "reddish"; alert(apple.getInfo()); 3. Summary

Inversion of Control Containers and the Dependency Injection pat In the Java community there's been a rush of lightweight containers that help to assemble components from different projects into a cohesive application. Underlying these containers is a common pattern to how they perform the wiring, a concept they refer under the very generic name of "Inversion of Control". In this article I dig into how this pattern works, under the more specific name of "Dependency Injection", and contrast it with the Service Locator alternative. One of the entertaining things about the enterprise Java world is the huge amount of activity in building alternatives to the mainstream J2EE technologies, much of it happening in open source. Underlying these containers are a number of interesting design principles, things that go beyond both these specific containers and indeed the Java platform. Components and Services The topic of wiring elements together drags me almost immediately into the knotty terminology problems that surround the terms service and component.

Dependency Injection and Inversion of Control with ASP.NET MVC As you delve more into ASP.NET MVC you start to come across a whole new way of doing things that Web Forms development didn't really expose you to. Inversion of Control (IoC) and Dependency Injection (DI) are two phrases that crop up a lot in the MVC space. So what are they all about? And should you care? I should start by stating the IoC and DI are not unique to ASP.NET MVC. They have been around a long time. Just to clear up any initial confusion, yes - the example above is more or less the same code twice. There is no separation of concerns here in that the data access layer is embedded in the Controller. "Oh, but that will never happen to me", you say. When you view Data Access as a "component" of your application, you should also start to see that there are other areas in an application which could be viewed as components. Another problem with the tightly coupled examples is with Unit Testing. Dependency Injection and this: Right. This is a lot better. IoC Containers Summary

Using LINQPad with Entity Framework Using LINQPad with Entity Framework LINQPad lets you query Entity Framework models that you define in Visual Studio. Here's how to proceed: Create an ADO.NET Entity Data Model in a Visual Studio project, and build. From LINQPad, click Add Connection (top left) and choose Entity Framework in the bottom listbox. A few notes: You can return to Visual Studio at any time and rebuild - LINQPad will not lock your assembly. More Information View O'Reilly's detailed video tutorial on using LINQPad with Entity Framework - and LINQPad's other new features. Also, check out Stefan Cruysberghs's blog which provides further information on using LINQPad with Entity Framework and custom LINQ to SQL data context classes. Over 1 million downloads LINQPad Bookmark this on Delicious Follow LINQPad on Facebook More LINQPad Forum LINQPad + Mindscape LightSpeed LINQPad + OData LINQPad + DevForce LINQPad + DevArt AWS with LINQPad Method Chaining and Debugging LINQBugging WinForms/WPF Terry's LINQPad Extensions Videos LINQPad + EF

JSPatterns.com What is Dependency Injection? This article is part of a series on Dependency Injection in general and on a lightweight implementation of a Container in PHP in particular: This article is the first of a series on Dependency Injection in general and the implementation of a Dependency Injection Container in PHP. Today, I won't talk about the container yet as I first want to introduce the concept of Dependency Injection with some concrete examples that will hopefully demonstrate the problems it tries to solve and the benefits it gives to the developer. If you already knows the concept of Dependency Injection, you can safely skip this article and instead wait for the next one. Dependency Injection is probably one of the most dead simple design pattern I know. To overcome the statelessness of the HTTP protocol, web applications need a way to store user information between web requests. $_SESSION['language'] = 'fr'; The above code stores the user language in the language session variable. Enter Dependency Injection.

you've been HAACKED LINQ to SQL (Part 3 - Querying our Database) Last month I started a blog post series covering LINQ to SQL. LINQ to SQL is a built-in O/RM (object relational mapping) framework that ships in the .NET Framework 3.5 release, and which enables you to easily model relational databases using .NET classes. You can then use LINQ expressions to query the database with them, as well as update/insert/delete data from it. Below are the first two parts of my LINQ to SQL series: In today's blog post I'll be going into more detail on how to use the data model we created in the Part 2 post, and show how to use it to query data within an ASP.NET project. Northwind Database Modeled using LINQ to SQL In Part 2 of this series I walked through how to create a LINQ to SQL class model using the LINQ to SQL designer that is built-into VS 2008. Retrieving Products Once we have defined our data model classes above, we can easily query and retrieve data from our database. Visualizing LINQ to SQL Queries in the Debugger Shaping our Query Results Summary Scott

Reducing Resistence to Dependency Injection In one of my previous posts, I wrote about ways that you can make life difficult when injecting dependencies. I find that systems where it is difficult to inject dependencies will be harder to change, will be harder to test, and will often be intimately coupled together in all sorts of interesting ways. Some people (rightfully) commented that I should suggest some tips about what to do. I’d initially assumed it’d be easy to put a “Avoid if possible…” in front of each statement but I think it’d be more useful to describe some approaches to reducing the resistance. Here are a few tips: Tests in place – Find a seam, add some tests before starting to make changes. Hope this list was helpful.

Methods GET and POST in HTML forms - what's the difference? In HTML, one can specify two different submission methods for a form. The method is specified inside a FORM element, using the METHOD attribute. The difference between METHOD="GET" (the default) and METHOD="POST" is primarily defined in terms of form data encoding. Content: The fundamental differences between "GET" and "POST" The HTML specifications technically define the difference between "GET" and "POST" so that former means that form data is to be encoded (by a browser) into a URL while the latter means that the form data is to appear within a message body. The HTML 2.0 specification says, in section Form Submission (and the HTML 4.0 specification repeats this with minor stylistic changes): If the processing of a form is idempotent (i.e. it has no lasting observable effect on the state of the world), then the form method should be GET. In the HTTP specifications (specifically RFC 2616) the word idempotent is defined as follows: Why the distinction matters Differences in form submission

Expression Trees: Why LINQ to SQL is Better than NHibernate In my last post I described how the Where() function works for LINQ to Objects via extension methods and the yield statement. That was interesting. But where things get crazy is how the other LINQ technologies, like LINQ to SQL use extension methods. In particular it’s their use of a new C# 3 feature called expression trees that makes them extremely powerful. And it’s an advantage that more traditional technologies like NHibernate will never touch until they branch out from being a simple port of a Java technology. What’s so Magic about LINQ to SQL? LINQ to SQL (and it’s more powerful unreleased cousin LINQ to Entities) is a new Object Relational Mapping (ORM) technology from Microsoft. IEnumerable<Product> products = northwindDataContext.Products.Where( p => p.Category.CategoryName == "Beverages" ); Which as you’d expect returns products from the database whose category is Beverages. SELECT [t0]. In other words it’s pretty smart. Understanding the Magic How Deep Does The Rabbit Hole Go?

How to: Pass Values Between ASP.NET Web Pages The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location. If your application redirects (navigates) from one ASP.NET Web page to another, you will frequently want to pass information from the source page to the target page. A Visual Studio Web application project with source code is available to accompany this topic: Download. You can pass information between pages in various ways, some of which depend on how the redirection occurs. Use a query string.Get HTTP POST information from the source page. The following options are available only when the source and target pages are in the same ASP.NET Web application. Use session state.Create public properties in the source page and access the property values in the target page.Get control information in the target page from controls in the source page. To use a query string to pass information To use session state to pass information

Related: