Home MVC Storefront

SubSonic: Taking a Look At The New Repository Base Class

Many people have asked over the last few years if it was possible to use SubSonic without Active Record. They didn't like the idea that every object could interact with the database and wanted a more "locked-down" approach. I offered some interim templates to get around this, but with version 2.1 we've advanced this idea further and included the ability to override the object base class entirely.

Services, Managers, Helpers, Repositories
There just really isn't a good word for this class but I think people have come to embrace the word "Repository" to refer to your database/data store. Hopefully we can agree on the concept here rather than the wording - if not let me know and I'll do my best to duck responsibility... If you like ActiveRecord - no worries, it's still our default. If you are curious and want to know more about this Repository thing and how it works - you can watch this screencast (download WMV here). A final note to say that we're still in beta - so your thoughts count!

tgmdbm avatar
tgmdbm says:
Friday, March 14, 2008
Hey Rob, Is there a direct link to the screencast? (I uninstalled silverlight since it crashed firefox regularly)

Brian avatar
Brian says:
Friday, March 14, 2008
I had that same problem but recently I updated silverlight and, at least for me, Firefox no longer crashes.

John S. avatar
John S. says:
Friday, March 14, 2008
At the end of SonicCast #3 (http://72.14.253.104/search?q=cache:http://subsonicproject.com/view/soniccast-3---all-about-mvc.aspx&rls=com.microsoft:en-us&ie=UTF-8&oe=UTF-8&startIndex=&startPage=1) you demonstrated extending the generated partial class and overriding some of the methods. How would you structure that now with Pakala?

Zack Owens avatar
Zack Owens says:
Saturday, March 15, 2008
Rob, What is your opinion on the public-facing DB class? As one of my own conventions, I just mark the DB class as internal and write my own DB-proxy "Logic" classes. What's your opinion? Zack

Rob Conery avatar
Rob Conery says:
Saturday, March 15, 2008
@tgm: I'll load it up to my FTP this weekend... @Zack - no opinion really. Sometimes you might need to run a query in your Controllers (if you're using MVC) but if you're being strict, yah internal's the way to go. It depends what you need - not every project needs to be bronzed. BTW nice to see you've removed your "VB Training Wheels" (/duck)

Peter Mendis avatar
Peter Mendis says:
Saturday, March 15, 2008
Hey Rob, Is there any comparison on this website that explains the clear difference and advantages between Active Record ( That works with Ruby on Rails ) and SubSonic ( That works for .Net 3.5 ) What i understand is SubSonic is an ActiveRecord version of Ruby for .Net supporting C# and Vb.Net with same advantages. What i am more interested is... Advantages of SubSonic over classic ActiveRecord. Can SubSonic be used in future with Dynamic languages like Vbx, IronPython, IronRuby etc... Is that all coming to us with SubSonic 3.0....? Whats the LInQ to SubSonic then...? Pl. blog on whats coming up with SubSonic in future and whats planned for future. I am stuck up with advantages and versions of SubSonic Guide me more please. Thanks Peter Mendis

Dotnetshadow avatar
Dotnetshadow says:
Saturday, March 15, 2008
Hey Rob, This is the post I've been waiting for... 1) You sort of answered with @Zack question (marking DB CRUD as internal), but what happens if you have different namespaces for your BLL and DAL? Marking internal won't help in this situation. 2) Basically I would like this answered: http://forums.subsonicproject.com/forums/p/2877/12032.aspx#12032 3) I've posted some possible fixes for Subsonic 2.1 Beta 2 in the 2.1 forum let me know what you think. Thanks once again for a great tool Regards Rob

Evan avatar
Evan says:
Sunday, March 16, 2008
So one thing you might think about down the road... When working with an aggregate (Order, LineItem, ShippingAddress, etc) and aggregate root (Order), it's often very useful to allow the Repository to fetch the entire object graph. This cleans up code such as the following: Order order = DB.Get(123); double total = order.CalculateTotal(); where CalculateTotal is implemented as follows: public double CalculateTotal() { long total = 0; foreach (LineItem item in _lineItems) total = (item.Quantity * item.UnitPrice); } You'll find that without the Aggregate / Aggregate Root feature in the data access code, it's very hard to write the above code while staying Persistant Ignorant. And yes, you could just store the Order Total as part of the order, but the same scenario applies to the ShoppingCart Aggregate (Cart, CartItem) where you may want to recalculate the cart's total on the fly every time (ie.."A change in product price should be reflected in all carts not in the Checkout process...") or ("From 5pm-6pm CST is happy hour, all products are 10% off during that time").. Just a thought..

Evan avatar
Evan says:
Sunday, March 16, 2008
And that's the buggy result I get when writing blog code blindly in a comment box at 1:30am on a Sunday morning.. ;-)

Duckie avatar
Duckie says:
Sunday, March 16, 2008
Is it correct, that the vb.net templates is not done yet? I would like to help, if possible.

dave thieben avatar
dave thieben says:
Sunday, March 16, 2008
Looks awesome, Rob. One thing, though - would it be possible to not use a static class for the DB class? I'm not sure what the right replacement would be, without knowing where you're going with it, but I'd prefer to see a data access class that can be mocked for unit tests. After all, that's most of the reason I like to have "dumb" model objects. On my projects so far I use some sort of IoC, but that is probably way outside the scope of SubSonic. Maybe there is a setting that defaults to an instance of the regular DB, but can be overridden at runtime in TestFixtureSetup.. ? just throwing it out there. thanks for the excellent work. dave.t

Jason Kealey avatar
Jason Kealey says:
Sunday, March 16, 2008
Rob, I like the way you've changed the base record class and removed the extra methods. This would make it easy (at compile time) to migrate existing code, should we decide to go down this lane. I'd like to see a UML diagram of your class hierarchy (for generated objects), because it changed since 2.0. Thanks, Jason

Rob Conery avatar
Rob Conery says:
Sunday, March 16, 2008
@Evan - yep, good point. You're referring to the old "eager load" and it's something Eric and I have been discussing. Need to figure that out - thanks for the reminder :). @Dave - I've been thinking about that too. Lots of code goes into IoC and I'm still not convinced it's viable for a DAL. That's me though - doesn't mean I can't make it easier for you guys. I need to noodle on this. @Jason - will do. @tgmgmdmdbmdsgmdgOMFG :) - Added a download link for ya...

Robsworld avatar
Robsworld says:
Sunday, March 16, 2008
Hey Rob, I know you must be extremely busy, but if possible could you please respond to the comments I made above (Dotnetshadow) Thanks in advance Regards Dotnetshadow

Quinten avatar
Quinten says:
Sunday, March 16, 2008
Hey Rob (and Team) One thing I would love to see in the next release would be the ability to add custom attributes to the .config for SubSonic. The logic behind this is that it would help greatly those that like to modify the templates. E.g. I have started work on a set of templates, I have tried to make them as generic as possible so that other may use them but I do have a proprietary requirement for the job I'm working on, this feature may be useful to other, most likely not though. If I could add an attribute then I could switch it off by default and then use attributes to manage what is generated. Maybe a place were the community can post their templates might be a useful idea, although I can see how this could get out of hand and would be a lot more work. Maybe codeplex is the place for these templates. Thanks for a wicked product, has made development a pleasure. As well as templates for SubSonic I am also planning on spending a bit of time working on the Scaffold, seeing how far it can go with code gen. I might look at using ASP.NET 3.5 for this though. Cheers Q

tgmdbm avatar
tgmdbm says:
Sunday, March 16, 2008
@rob, thanks for the direct link. although I think something freaky happened to your keyboard. J Also, what's the sound in the beginning of that video? My moniker comes from having such a common name. I wanted something that if i search for it I only find results relating to me. It doesn't mean anything, its just unique.

Rob Conery avatar
Rob Conery says:
Sunday, March 16, 2008
@Dotnetshadow.... >>>but what happens if you have different namespaces for your BLL and DAL? Marking internal won’t help in this situation.<<< I'm not sure what situation you're referring to. >>>I’ve posted some possible fixes for Subsonic 2.1 Beta 2 in the 2.1 forum let me know what you think<<< Much appreciated :)

Robsworld avatar
Robsworld says:
Monday, March 17, 2008
@Rob The situation I'm referring to is that I would like to have Subsonic isolated from my web project (View Layer). What I would like is that I have a DAL layer that has all the subsonic generated code, and only the BLL has access to this DAL layer. The way subsonic works at the moment any layer can use the subsonic code, so there is a possibility that instead of a developer using the SaveCustomer() from the BLL they could use Customer.Save() from the DAL. Several people have suggested marking templates with internal so that this doesn't happen, but I also tried using this techinique and found that I would also have to use the InternalsVisibleTo attribute for subsonic. But I'm having a hard time trying to separate code out. I was hoping the repository pattern could of helped where the DB classes could be marked so that only certain layers can use it. I'm not sure how to achieve it what do u think? Maybe a sonicast on code separation with subsonic would be handy for those of us trying to do 3 tier applications demonstrating that the web layer won't have access to any CRUD methods? Regards DotnetShadow

dave thieben avatar
dave thieben says:
Monday, March 17, 2008
Rob, I didn't mean to insinuate that you should put an IoC into SubSonic. just a way to select & instantiate the "DB" class that does that actual work, and not use a static class. and have it overrideable at runtime, or by config, for unit testing. looks great so far.

thardy avatar
thardy says:
Tuesday, March 18, 2008
Just wondering about the progress/roadmap of the next version of SubSonic built on top of LINQ. I'm loving some things about LINQ currently, but could seriously use some goodness built on top of it. Of particular pain is the lack of good detached update support within LINQ. Also, decent eager-loading support is very weak. I'd be interested in helping out on the design and also the coding of a good library that built on top of LINQ.

D'Arcy from Winnipeg avatar
D'Arcy from Winnipeg says:
Tuesday, March 18, 2008
Ok, first off: I am not Robsworld/Dotnetshadow Secondly: SEE ROB, *others* think the same bat-$h1t-crazy stuff that I do! MUA HA HA HA HA Ok but seriously, before Dotnetshadow, I think what you might want to do is 1. Compile your SubSonic layer (Domain/Repository) into its own assembly 2. Have that assembly be referenced within another project, we'll call it "Service" 3. Have your BL assembly reference the "Service" assembly 4. Have your UI reference the BL The problem is that if you're just logically seperating your code within your project, its just that: a logical seperation. The only thing preventing the UI from getting to the DAL is the developer. Splitting it up into seperate assemblies helps a bit in that only projects referencing the Domain/Repository assembly will be able to call the code...but this doesn't stop said developer from referencing those pieces directly in the UI project. Still better then nothing. What Rob pointed out to me when I had a similar question was that in the scope of an ASP.NET web application you have to consider the applications context. Any code utilized within a project is governed by the project, even other assemblies. My issue was that I wanted my DAL assembly to own its own config file and not have to put it in as part of the overall project's config...but you just can't do that (or you can, but your web.config still needs to know where to look for the custom config file). So there's no way to totally seprate your DAL from your application unless you create it as a totally seperate boundary (webservice/remoting)...which is valid but may be overkill depending on your application. Anyway, hth. D

SDMauro avatar
SDMauro says:
Thursday, March 20, 2008
Hi Rob, When do you guys anticipate the final release of 2.1 that includes this new Repository Base Class? MSD


Search Me
Subscribe

Popular Posts
 
My Tweets
  • @codinghorror: I may be weird but I don't ask my cats to write my blog entries :p.
  • @blowdart sure! Wanna come on and do a webcast with me - plugging CardSpace in?
  • People are very, very weird. Skypecasts are creepy.
  • Is Rob Howard trying to tell us something? http://www.rob-howard.net/
  • New storefront posted- all about OpenID :) http://tinyurl.com/5rkgux
  About Me



Hi! My name is Rob Conery and I work at Microsoft on the ASP.NET team. I am the Creator of SubSonic and was the Chief Architect of the Commerce Starter Kit (a free, Open Source eCommerce platform for .NET)

I live in Kauai, HI with my family, and when my clients aren't looking, I sometimes write things on my blog (giving away secrets of incalculable value).