views:

419

answers:

1

I was just starting to get comfortable with MVC when somebody mentioned IoC containers to me, and now I feel like I've fallen a few thousand feet and need to climb back up again. I was tempted to just ignore them, but then I read up on the Component Lifestyle. This seems like a big deal to me, as explained, uncommited changes to database updates might leak across requests if my repositories Lifestyle is set to Singleton instead of PerWebRequest.

So my question...is there a way to create the Component Lifestyle affect without using IoC containers, or is that the only option?

A: 

You can always create an implementation as a Singleton. However when speaking about a Repository I don't think there is a need. This is not the real purpose to using an IoC though. Using the IoC decouples the user of your code from the implementation itself. It makes testing easier as you can swap things in and out as you need too. Also, in my case I find it very useful for other purposes as well. Take caching for example. In my local dev I use Lucene.NET to provide my cache implementation with a disk based cache. Then when I push to my development and staging platforms I use the standard .net cache implementation. Then when I push to production I might use a Velocity or MemCached implementation. I don't have to do anything fancy to get from point A to point B since I am using StructureMap (my preferred IoC container).

It seems to be that Component Lifestyle is more of a design decision than an IoC decision. All three of the methods mentioned here for managing the life of an object can be done without IoC...but that might be an interesting side effect of an IoC (in some cases).

Andrew Siemer
Thanks...decoupling my code is *nice* but not vital for me at this time. However, the integrity of the data across requests is *vital* - and my concern is that without IoC, I am somehow opening my app to potential corruption issues. (NOTE: I have yet to implement a real app using MVC - just four or five demo apps. I've just been reading two books on the subject - and the IoC container subject has thrown me for a loop)
dizzyguy
"Your *SqlProductsRepository" currently has this Singleton lifestyle, so you're keeping a single LINQ to SQL *DataContext* alive as long as your app runs, sharing it across all request. That might seem fine...because so far all data access is read-only, but it would lead to problems when you start editing data. Uncommitted changes would start leaking across requests." - Pro ASP.NET MVC Framework, pg. 101
dizzyguy
If you instantiate an instance of your data context in a controller or a service and "inject" that into your repository or repositories for that transaction...then call commit...and then kill that isntance of the data context you "should" be ok. I never leave my datacontext open through out the lifecycle of an application. It is not around outside of the scope of a transaction...which I found to be a safe way to do it.
Andrew Siemer