Author: DamianMac
(2009/10/23 08:11) Over 2 years ago
Commenting for documentation
13
{
14
private readonly IRepository<Post> postRepository;
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.
public HomeController(IRepository<Post> postRepository)
21
22
this.postRepository = postRepository;
23
9
10
private readonly ISession session;
11
12
//This will get populated with a Session that StructureMap gives us if we allow StructureMap to instatiate the repository
//Or you can give it a mocked ISession in your unit tests
public Repository(ISession session)
this.session = session;
28
29
RegisterRoutes(RouteTable.Routes);
30
31
32
//Here we're calling a class that initialises StructreMap and configures the types it knows how to build
ContainerConfiguration.Configure();
33
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.
ControllerBuilder.Current.SetControllerFactory(new StructureMapControllerFactory());
37
}
38
39
private static void InitStructureMap(ConfigurationExpression obj)
//A registry is what StructureMap uses to manage how it builds objects.
//You can do this all in one place, but in a big project splitting them up is a nice way to maintain them
obj.AddRegistry(new NHibernateRegistry());
obj.AddRegistry(new RepositoryRegistry());
8
public NHibernateRegistry()
//This is the interesting stuff
//The session factory is slow to create, because it compiles all the mappings
//We only want it done once, then use it to quickly create sessions.
//You'll notice the Singleton scope here.
//It's created in the NHibernateSessionFactory class the first time it's requested
//The StructureMap keeps it around
ForRequestedType<ISessionFactory>()
.CacheBy(InstanceScope.Singleton)
.TheDefault.Is.ConstructedBy(() => new NHibernateSessionFactory().GetSessionFactory());
//The Session is how we interact with NHibernate in our code.
//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.
ForRequestedType<ISession>()
27
.CacheBy(InstanceScope.Hybrid)
.TheDefault.Is.ConstructedBy(x => x.GetInstance<ISessionFactory>().OpenSession());
var connectionString = ConfigurationManager.ConnectionStrings["Web"].ConnectionString;
//This is a nice example of how Fluent NHibernate replaces a whole bunch of ugly stuff inside web.config
//with something you can pretty much copy into any project
//Go look in the Mappings folder for how Fluent NHibernate maps your domain classes to the database
var sessionFactory = Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2005.ConnectionString(x => x.Is(connectionString)))
.Mappings(x => x.FluentMappings.AddFromAssemblyOf<NHibernateSessionFactory>())
public RepositoryRegistry()
//Pretty simple, when StructureMap gets a request for this IRepository, it will instantiate the Repository class
ForRequestedType<IRepository<Post>>().TheDefaultIsConcreteType<Repository<Post>>();
try
//The magic happens here.
//When MVC tries to get an instance of the appropriate controller class, we ask StructureMap to create it
//We don't actually have to register the controller with StructureMap, it will create anything we ask for.
//What it will do though is populate the controller with dependencies if it knows about them.
//See the HomeController for an example.
controller = (IController)ObjectFactory.GetInstance(controllerType);
catch (Exception exception)
{{{{{{{{{{{{{{{{{{{{{{{{