Program Class – Spring.Net

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spring.Context;
using Spring.Context.Support;

namespace MovieFinderTestSpring.Net
{
    class Program
    {
        static void Main(string[] args)
        {
            // Get one object from the Movie class configured with specific values
            IApplicationContext context = ContextRegistry.GetContext();
            Movie andreiRublev = (Movie)context.GetObject("AndreiRublevMovie");
            Console.WriteLine("Spring.Net XML: " + andreiRublev.ToString());

            // Finder injected into Lister by constructor injection.  Only one Lister
            // class here, configured to use two different Finders (SimpleMovieFinder
            // and ColonDelimitedMovie Finder.  Both of the Finders
            // implement the MovieFinder interface of course, and are injected
            // using that.
            MovieLister simpleLister = (MovieLister)context.GetObject("simpleMovieLister");
            Movie[] jamesCameronMovies = simpleLister.MoviesDirectedBy("James Cameron");
            foreach (Movie movie in jamesCameronMovies)
                Console.WriteLine(movie.ToString());

            // Finder injected by setter injection.  See comments above.
            MovieLister colonDelimitedLister = (MovieLister)context.GetObject("colonDelimitedMovieLister");
            Movie[] martinScorseseMovies = colonDelimitedLister.MoviesDirectedBy("Martin Scorsese");
            foreach (Movie movie in martinScorseseMovies)
                Console.WriteLine(movie.ToString());

            // Test scope by requesting a second lister of each type: 
            // the singleton scope SimpleMovieLister should return the same object
            // as above, the prototype scope ColonDelimitedMovieLister should
            // return a new object (which will have a different ID).
            Console.WriteLine("First requested simple lister (singleton) version = " + simpleLister.VersionID);
            MovieLister simpleLister2 = (MovieLister)context.GetObject("simpleMovieLister");
            Console.WriteLine("Second requested simple lister version = " + simpleLister2.VersionID);

            Console.WriteLine("First requested colon delimited lister (prototype) version = " + colonDelimitedLister.VersionID);
            MovieLister colonDelimitedLister2 = (MovieLister)context.GetObject("colonDelimitedMovieLister");
            Console.WriteLine("Second requested colon delimited lister version = " + colonDelimitedLister2.VersionID);

            // Need an example with more complex construction via a factory method
            MovieLister complexLister = (MovieLister)context.GetObject("complexMovieLister");
            Movie[] tarkovskyMovies = complexLister.MoviesDirectedBy("Andrei Tarkovsky");
            foreach (Movie movie in tarkovskyMovies)
                Console.WriteLine(movie.ToString());

            Console.ReadLine();
        }
    }
}
 
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s