Classes that show up in every project

Just out of whimsy, here’s my list of classes or interfaces that seem to show up in every project I work on.  

  1. Bootstrapper – sets up StructureMap and whatever UI machinery.  I formalized this in StructureMap 2.5 for diagnostic purposes.
  2. Debugging – Just a test fixture marked as explicit for playing with little bits of code and troubleshooting
  3. ObjectMother and/or DataMother (Test Data Builder ala Nat Pryce)
  4. TestUtility – Usually just a quickie way to run Fit/StoryTeller tests inside of NUnit tests
  5. ICommand – Execute() and usually something else
  6. A console app called DatabaseGenerator to setup the development and testing databases.  Wraps the NHibernate hbm2ddl tool for us.
  7. A console app called CodeGen to kick off whatever code generation we’re using.  We codegen DTO’s and Fit Fixture classes.
  8. If it’s a desktop application, I’ll have an ApplicationController and ApplicationShell
  9. SpecificationExtensions – This is relatively new.  I love the SpecUnit stuff that Bellware did with extension methods for RSpec like unit test assertions.  We add our own extensions at will.
  10. XmlExtensions – Extension methods for Xml manipulation.  Just a little bit of effort makes Xml consumption so much easier
  11. IRepository.  We dumped IRepository<T> with Save(T) methods in favor of IRepository with Save<T>(T) and use Linq for NHibernate to express queries to avoid having to write one off repositories for each aggregate root.  It’s too early to say if it’s a better approach, but I’m hopeful.  Our current one looks like:

    public interface IRepository

    {

        T Find<T>(long id) where T : Entity;

        void Delete<T>(T target);

        T[] Query<T>(Expression<System.Func<T, bool>> where);

        T FindBy<T, U>(Expression<System.Func<T, U>> expression, U search) where T : class;

        T FindBy<T>(Expression<System.Func<T, bool>> where);

        void Save<T>(T target);

    }

 

 

What’s yours?  Or is this just a sign of being in a rut? 

About Jeremy Miller

Jeremy is the Chief Software Architect at Dovetail Software, the coolest ISV in Austin. Jeremy began his IT career writing "Shadow IT" applications to automate his engineering documentation, then wandered into software development because it looked like more fun. Jeremy is the author of the open source StructureMap tool for Dependency Injection with .Net, StoryTeller for supercharged acceptance testing in .Net, and one of the principal developers behind FubuMVC. Jeremy's thoughts on all things software can be found at The Shade Tree Developer at http://codebetter.com/jeremymiller.
This entry was posted in Uncategorized. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • http://colinjack.blogspot.com Colin Jack

    “Keeps validation out of the domain and into a service object.”

    Do you mean that you aim to move all validation out of the domain, or just that you try to keep GUI focussed validation out?

  • http://weblogs.asp.net/bsimser Bil Simser

    Stuff we have that at some poinit will get harvested into a framework:

    * DependencyResolver. This is our wrapper for any IoC (used to be called IoC). Has methods like Initialize (which takes in something to use to resolve, like an IWindsorContainer) and Get to get a dependency

    * IBuilder Fluent fixture for building entities. Like ObjectMother but a little more flexible

    * ISpecification A generic for creating specification objects to filter out items from entitiy collections. Have a few blog posts on this that I need to get out

    * IValidationStrategy A strategy pattern implementation on creating various validations for entities. An example like ReadOnlyValidation would be used in the UI to set controls to non-editable. Keeps validation out of the domain and into a service object.

  • http://www.NDepend.com Patrick Smacchia

    Path handling library,
    considering that a path is a string is such a poor practice with all the path richness (file/folder, absolute/relative, operation…)

    http://www.codeplex.com/FileDirectoryPath

  • Guy

    1) ICommand

    2) IRule with the bool FulfiledBy(T candidate) method

    3) A Guard class for parameter validation.

    4) On.UIThread(Func operation)

    5) ApplicationShell

    6) EventBroker of some sort with the Subscribe and Publish methods

    7) EventHelper for raising events(hate repeating that eventName != null crap)

  • http://blog.troyd.net Troy DeMonbreun

    Try/catch blocks suck.

    Maybe we could start a new “Exception Ignorance” grassroots movement.

  • http://codebetter.com/blogs/jeremy.miller Jeremy D. Miller

    PROVISO: The source code is available, but it’s a naive implementation at the moment. Note the total absence of adequate try/catch blocks.

  • http://blog.troyd.net Troy Demonbreun

    Source code for Jeremy’s IRepository & Repository classes are available here:

    http://storyteller.tigris.org/source/browse/storyteller/trunk/src/ShadeTree.DomainModel/IRepository.cs?view=markup

  • http://sticklebackplastic.com Matt Ellis

    +1 for the linq/nhibernate.

    I’m a bit of a newbie with nhibernate, and realise it’s a different mindset to the traditional DB way I’ve been working previously. I’ve understood nhibernate to be a persistence layer to enable you to work in terms of a domain model, rather than in terms of your database model. In other words, to get at data, you navigate rather than query. And so it follows that having to perform a query is an indication that there’s something missing in your domain model.

    Assuming I’ve got the general idea, why do are you querying the database? What kind of data are you getting back?

    Cheers
    Matt

  • http://postback.be/blog vincent

    Good idea to list those. For me, it comes down to about this:

    DataProvider
    Logging
    Settings (or Config or whatever)
    APPNAMEContext (like WebshopContext, or CommunityContext)
    Utils
    Events
    MessagingEngine

    Thinks like that.

  • Dan

    Chris Brandsma,

    Why not just use KeyValuePair?
    http://msdn.microsoft.com/en-us/library/8e2wb99w.aspx

  • http://elegantcode.com Chris Brandsma

    a generic name value class.

    public class NameValue
    {
    public TName Name { get; set;}
    public TValue Value { get; set; }

    public override string ToString()
    {
    return Name.ToString();
    }
    }

  • http://mikehadlow.blogspot.com Mike Hadlow

    My IRepository looks very similar but instead of
    T[] Query(..)
    T FindBy
    (…) and
    T FindBy
    (…)

    I just have a single
    IQueryable GetAll()

    In my unit tests I have mock repository builder methods that return mock repositories that spew out lists of test data whenever GetAll() is called. Any Linq extension methods that I chain after GetAll() then simply work on that object graph rather than being translated into SQL.

    It’s nice to wrap up specific queries in extension methods, then you can write stuff like:

    var orders = orderRepository.GetAll().ThatMatch(criteria);

    or maybe

    var orders = orderRepository.GetAll().ThatHaveNotBeenBilled();

    Like you I’m still experimenting with this pattern.

  • Mike D

    Similar to your IRepository solution we added a Save() Extension method to IQueryable and the implementation wraps the datacontext / session.

    We then grab IQueryable from the IoC container.

    Still early, but time will tell.

  • http://sitecore.alexiasoft.nl Alex de Groot

    I think your approach is the start of your own software factory. You’re not already identifying a lot of abstract steps you take before you start developing. The next step is to become more concrete and combine this stuff in predefined templates, etc.

  • http://kentb.blogspot.com/ Kent Boogaart

    I always have some kind of abstraction over logging (whether I’m using log4net, System.Diagnostics or whatever) to do stuff like log performance in code blocks and log an exception easily.

    And – not really a class – but I always end up with a Build project, which contains any MSBuild tasks needed to support the build. And it also contains post-build steps for full builds, like running unit tests and FxCop etcetera.

  • Nat

    I always end up writing a clock interface. Something like (in Java):

    public interface Clock {
    Instant now();
    Date today();
    }

    Then having a SystemClock that delegates to the static time APIs, and a StoppedClock for fixing the time in unit tests.

  • http://www.codinginstinct.com Torkel

    I usually have a static IoC class that wraps Castle Windsor, a helper class named Db for bypassing nhibernate and using ADO.NET directly for ex. Db.Transaction(delegate(IDbCommand cmd) {})

    If it’s a WebService app I usually have a EntityTranslator service which translates domain objects to DTO (and back),.

  • http://angryhacker.com/blog/ Robert G

    Log (sometimes replaced by Log4Net)
    Utilities (everything miscallaneous)
    BusinessBase – business base class. I’ve inherited a couple of CSLA.NET based projects, which has a similar, if not overcomplicated business base classes.
    DataBase – my tried and true database access base class – sometimes replaced by something shiny like Subsonic or NHibernate or CodeSmith generated magic

    Here is my typical solution, broken down by projects

    PresentationLayer
    BusinessLayer
    CommonLayer
    DataLayer

    Yeah, it might be a sign of a rut.

  • http://codebetter.com/blogs/david_laribee/ Dave Laribee

    I’d also like to put in a request to talk about how you’re using Linq/NHibernate and maybe functional-style programming with your single repository.

  • mendicant

    One that comes to mind for me is some sort of an ILogger (we’ve got a bunch of legacy apps with their own ‘logging’ system) so we’ve ended up putting in a facade that takes whatever format that particular app’s Logging facility looks like and maps it to log4net.

  • http://mario-chavez.blogspot.com Mario A Chavez

    Jeremy;

    Will be great is you can post the code of some of those classes

    Thx