Changeset 5

User picture

Author: DamianMac

(2009/10/23 08:11) Over 2 years ago

Commenting for documentation

Affected files

Updated NHStructureMapMvc/NHStructureMapMvc/Controllers/HomeController.cs Download diff

45
13
    {
13
    {
14
        private readonly IRepository<Post> postRepository;
14
        private readonly IRepository<Post> postRepository;
15
15
16
17
        //This class will be created by our StructureMapControllerFactory.
18
        //StructureMap will see that this constructor has a dependency on the repository, and will instantiate that too
19
        //If you look at the Repository class, you'll see it has a dependency on ISession, which we know is created by our ISessionFactory
20
        //StructureMap will walk down and fill all those dependencies for you at runtime.
16
        public HomeController(IRepository<Post> postRepository)
21
        public HomeController(IRepository<Post> postRepository)
17
        {
22
        {
18
            this.postRepository = postRepository;
23
            this.postRepository = postRepository;

Updated NHStructureMapMvc/NHStructureMapMvc/Data/Repository.cs Download diff

45
9
    {
9
    {
10
        private readonly ISession session;
10
        private readonly ISession session;
11
11
12
        //This will get populated with a Session that StructureMap gives us if we allow StructureMap to instatiate the repository
13
        //Or you can give it a mocked ISession in your unit tests
12
        public Repository(ISession session)
14
        public Repository(ISession session)
13
        {
15
        {
14
            this.session = session;
16
            this.session = session;

Updated NHStructureMapMvc/NHStructureMapMvc/Global.asax.cs Download diff

45
28
        {
28
        {
29
            RegisterRoutes(RouteTable.Routes);
29
            RegisterRoutes(RouteTable.Routes);
30
30
31
32
            //Here we're calling a class that initialises StructreMap and configures the types it knows how to build
31
            ContainerConfiguration.Configure();
33
            ContainerConfiguration.Configure();
34
            
35
            //ASP.NET MVC allows us to specify our own ControllerFactory.
36
            //This one uses structuremap to build the controllers, and injects and required dependencies like Repositories.
32
            ControllerBuilder.Current.SetControllerFactory(new StructureMapControllerFactory());
37
            ControllerBuilder.Current.SetControllerFactory(new StructureMapControllerFactory());
33
        }
38
        }
34
    }
39
    }

Updated NHStructureMapMvc/NHStructureMapMvc/Infrastructure/ContainerConfiguration.cs Download diff

45
15
15
16
        private static void InitStructureMap(ConfigurationExpression obj)
16
        private static void InitStructureMap(ConfigurationExpression obj)
17
        {
17
        {
18
            //A registry is what StructureMap uses to manage how it builds objects.
19
            //You can do this all in one place, but in a big project splitting them up is a nice way to maintain them
18
            obj.AddRegistry(new NHibernateRegistry());
20
            obj.AddRegistry(new NHibernateRegistry());
19
            obj.AddRegistry(new RepositoryRegistry());
21
            obj.AddRegistry(new RepositoryRegistry());
20
        }
22
        }

Updated NHStructureMapMvc/NHStructureMapMvc/Infrastructure/NHibernateRegistry.cs Download diff

45
8
    {
8
    {
9
        public NHibernateRegistry()
9
        public NHibernateRegistry()
10
        {
10
        {
11
            //This is the interesting stuff
12
            //The session factory is slow to create, because it compiles all the mappings
13
            //We only want it done once, then use it to quickly create sessions.
14
            //You'll notice the Singleton scope here. 
15
            //It's created in the NHibernateSessionFactory class the first time it's requested
16
            //The StructureMap keeps it around
11
            ForRequestedType<ISessionFactory>()
17
            ForRequestedType<ISessionFactory>()
12
                .CacheBy(InstanceScope.Singleton)
18
                .CacheBy(InstanceScope.Singleton)
13
                .TheDefault.Is.ConstructedBy(() => new NHibernateSessionFactory().GetSessionFactory());
19
                .TheDefault.Is.ConstructedBy(() => new NHibernateSessionFactory().GetSessionFactory());
14
20
21
22
            //The Session is how we interact with NHibernate in our code.
23
            //You'll see it's created by taking the instance of our SessionFactory, and calling OpenSession on it
24
            //The InstanceScope.Hybrid means that in a web app, we keep the same session around for the life of a webrequest
25
            //When there is no HttpContext to store it against, it gets stored against the thread context
26
            //This lets us use the same configuration in integration tests.
15
            ForRequestedType<ISession>()
27
            ForRequestedType<ISession>()
16
                .CacheBy(InstanceScope.Hybrid)
28
                .CacheBy(InstanceScope.Hybrid)
17
                .TheDefault.Is.ConstructedBy(x => x.GetInstance<ISessionFactory>().OpenSession());
29
                .TheDefault.Is.ConstructedBy(x => x.GetInstance<ISessionFactory>().OpenSession());

Updated NHStructureMapMvc/NHStructureMapMvc/Infrastructure/NHibernateSessionFactory.cs Download diff

45
16
        {
16
        {
17
            var connectionString = ConfigurationManager.ConnectionStrings["Web"].ConnectionString;
17
            var connectionString = ConfigurationManager.ConnectionStrings["Web"].ConnectionString;
18
18
19
20
            //This is a nice example of how Fluent NHibernate replaces a whole bunch of ugly stuff inside web.config
21
            //with something you can pretty much copy into any project
22
23
            //Go look in the Mappings folder for how Fluent NHibernate maps your domain classes to the database
19
            var sessionFactory = Fluently.Configure()
24
            var sessionFactory = Fluently.Configure()
20
                .Database(MsSqlConfiguration.MsSql2005.ConnectionString(x => x.Is(connectionString)))
25
                .Database(MsSqlConfiguration.MsSql2005.ConnectionString(x => x.Is(connectionString)))
21
                .Mappings(x => x.FluentMappings.AddFromAssemblyOf<NHibernateSessionFactory>())
26
                .Mappings(x => x.FluentMappings.AddFromAssemblyOf<NHibernateSessionFactory>())

Updated NHStructureMapMvc/NHStructureMapMvc/Infrastructure/RepositoryRegistry.cs Download diff

45
8
    {
8
    {
9
        public RepositoryRegistry()
9
        public RepositoryRegistry()
10
        {
10
        {
11
12
            //Pretty simple, when StructureMap gets a request for this IRepository, it will instantiate the Repository class
11
            ForRequestedType<IRepository<Post>>().TheDefaultIsConcreteType<Repository<Post>>();
13
            ForRequestedType<IRepository<Post>>().TheDefaultIsConcreteType<Repository<Post>>();
12
        }
14
        }
13
    }
15
    }

Updated NHStructureMapMvc/NHStructureMapMvc/Infrastructure/StructureMapControllerFactory.asax.cs Download diff

45
20
            }
20
            }
21
            try
21
            try
22
            {
22
            {
23
                //The magic happens here.
24
                //When MVC tries to get an instance of the appropriate controller class, we ask StructureMap to create it
25
                //We don't actually have to register the controller with StructureMap, it will create anything we ask for.
26
                //What it will do though is populate the controller with dependencies if it knows about them.
27
                //See the HomeController for an example.
23
                controller = (IController)ObjectFactory.GetInstance(controllerType);
28
                controller = (IController)ObjectFactory.GetInstance(controllerType);
24
            }
29
            }
25
            catch (Exception exception)
30
            catch (Exception exception)