in

 

Sean Chambers

I am a Lead Developer from Palm Coast Florida. If I could describe my skillset I would include TDD, DDD, Alt.net, NHibernate, Castle Project and so on

November 2007 - Posts

  • Entities vs. Value Objects

    I haven't done a post in quite awhile. I have been taking care of my newborn boy Aidan of just about 2 months and needless to say I greatly underestimated the amount of time and effort it takes in the first couple of months for a new parent. We love him all the same and are getting enough sleep to not keel over and die. That being said, I hope everyone can forgive my absence.

    I recently purchased Domain Driven Design by Eric Evans (I know, it took me this long to buy it) and have been working through the book finding that a lot of the ideas I have been practicing from reading Applying DDD and Patterns by Jimmy Nilsson, but Eric's book definitely lays down a much better foundation to work off and wrap your head around. In hindsight, at Joe Ocampo's recommendation I would urge anyone else looking at a DDD book to start with Eric's and then read Jimmy's. Jimmy's book refers back to ALOT of the work outlined in Eric's book.

    One of the topics I have spent the most time reading over was Entities and Value Objects. These are two very important and ideas in a rich Domain Model (not to forget Aggregates and Repositories of course) and have to be identified very explicitly in the model. I don't mean to undermine any other topics in the book, but to just focus on these ideas for this post as I could write ALOT of posts of the knowledge I've absorbed thus far from Eric's book.

    I have been hanging out on the Yahoo DDD Group for some time now. If any of of you are looking for great information from very knowledgeable people, then subscribe to the rss feed and hold onto your seat! Almost all of the posts on this group are gems in their own right. This leads me to my main point.

    I started on a new project recently and thought very hard about the Entities/Value Objects in the first refactoring of the Domain. It was about a "State" object. State as in Florida or California. I thought about it and said to myself, A State is very clearly a value object since it is immutable and I can pass it to around to other objects in the model. Then once I thought some more, I figured, well maybe it is an entity because each instance of a "State" is obviously unique because you can only have one Florida or one California. After thinking about it for awhile I posted a question on the Yahoo group and got an amazing explanation from Daniel Gackle and just had to share it with everyone at LosTechies. At the same time, I also went back and read Joe's post from a little while ago and understood much more coming back to it this time around.

    You can read the post for yourself but I will paraphrase here because I loved this example and gave me real good insight into the idea of Entities vs. Value Objects. 

    Here is the example: Imagine a table in a classroom filled with a large number of unopened bottles of water. When the class starts, the teacher says, "Everyone please grab a bottle of water from the table". Everyone goes to the table and grabs any bottle, not caring which one to grab since they are all unopened bottles of water, thus they are all the same thing. They have no individual identity.

    Now, the same scenario except this time the teacher opens one of the bottles and drinks from it and places it back on the table. When he tells everyone to get a bottle this time the students will get a SPECIFIC bottle. This is because the opened bottle now has an identity and no one wants to drink the teachers spit. Thus, now the bottles can be considered Entities because they identities have to be kept track of so we don't get the one with spit.

    To add to this example let's think that for some crazy reason, everyone does not care about the teachers spit in the water bottle. If no one cares about the teachers spit then the bottles are value objects again because the spit is outside of the scope of the domain model.

    So, to summarize Daniel's response to my question. Look at the objects in the actual context of the domain. In my instance, I don't really care if the state of Florida is passed between multiple users and there is no attributes in the Florida object that sets it apart from other Florida object. Therefore, the State class can be considered a Value Object. Now, the moment I want to keep track of which states have electronic voting machines, then the State class could become an Entity because there is clearly a specific identity and attributes that go along with it.

    I hope that wasn't too confusing. I'm trying to present the idea in the way I understood it. Please do go read the whole thread though. There are other people that really add to the description and make the meaning much more thick then what I can present here. Good Luck!

  • It's written in AJAX!

    Had a very long webinar this morning about some new software we're looking at purchasing. The sales guy is showing us a bunch of features and blurts out the "AJAX" buzz word. I listen in more closely to see what he's going to say. It goes something like this:

    "This app was written completely with AJAX which is great!", I correct him with "You mean that it USES AJAX right?"

    He continues to insist that it is not .NET whatsoever and that AJAX is a family of programming languages which is what was used to make this great new application. Meanwhile I am staring at the page extensions of .aspx... I just rolled my eyes and bit my tongue. =)

    Later on in the presentation he jumps back on the AJAX bandwagon to beat the horse some more and while showing a GridView control in a page states: "See this scrollbar with grid, this is all ajax ...You can't do this with any other programming language", I correct him that the values he was changing within the grid was in fact AJAX because it was submitting behind the scenes, but the scrollbar was just a basic grid control that you can make with basic html. He cut me off and insisted again that the whole thing was AJAX. I just gave up at that point and listened to him ramble.

    I had no idea that the "AJAX programming language" had arrived, Nor was I aware that this is now a buzzword that Sales guys use to sell software. Anything to get the dollars =)

    After the meeting I was saying this to my boss and he says "That's why he's the sales guy"

  • LosTechies welcomes Chris Patterson!

    Let me take this moment to welcome Chris Patterson to the LosTechies crew. Many of you know him has PhatBoyG. His website can be found here: http://www.phatboyg.com. His blog on LosTechies can be found at this address: http://www.lostechies.com/blogs/chris_patterson/

    Chris is still getting moved in but expect a post soon.

    From everyone at LosTechies welcome Chris!! We're glad you joined the family!!

  • Testing Security Link Demands with R#

    This is more just a reference for me or anyone else that may stumble across this.

    I had a set of Controller tests that I needed to set a mock object to Thread.CurrentPrincipal so I could test PrincipalPermission attributes. Off the bat I would just happily set Thread.CurrentPrincipal to my mocked user and everything worked fine.

    When I ran the full suite of tests including all of my SqlCE Nhibernate tests I saw that NHibernate was throwing a TypeInitializationException from NHibernate.Cfg.Environment. After quite awhile of debugging I found that the data tests would only fail when I ran my Controller tests in conjunction with the data tests. After even more time I remembered about the Thread.CurrentPrincipal.

    Normally you probably wouldn't even notice a problem. It seemed as if though that when R# was trying to load the NHibernate Session it somehow didn't have permission because Visual Studios Principal was changed to a mock object! An ephiany ensued followed by kicking myself in the rear.

    To fix it, I just saved the CurrentPrincipal to an IPrincipal instance and set it back in the TearDown of my Controller test like so:

    private IPrincipal _oldPrincipal;

    [SetUp]
    public void Setup()
    {
        _oldPrincipal = Thread.CurrentPrincipal;
        Thread.CurrentPrincipal = _user;
    }

    [TearDown]
    public void TearDown()
    {
        Thread.CurrentPrincipal = _oldPrincipal;
    }

    Hopefully this post helps someone from falling into the same trap. Cheers!

  • Versioning with Cruise Control, Nant and Subversion

    I haven't had a chance to post any of my other posts sitting in the queue lately with the newborn and all so I figured I would post this topic as it didn't take long to perform the task at hand.

    There are a slew of versioning options. I won't even go into detail as to how many options you have. You could do date of the build, build number, SCM revision, the list goes on an on. A good discussion of your options have already be outlined by Jeff Atwood in this posting at codinghorror.com I chose to version like so:

    - MajorNumber.MinorNumber.BuildNumber.RevisionNumber

    So for instance: 1.5.182.5543 would be version 1.5, CC.Net build number 182 and SVN revision number 5543. Simple enough.

    After reviewing all of my options I felt this was most relevant. This tells me what build and revision the assembly is from which gives me a pretty good review of what code i'm looking at. Date on the version number is redundant as I can look up the date of the revision in subversion.

    Now, after reviewing a number of blog posts I found that there wasn't many options to version CC.Net builds with a specific SVN revision number. It is possible to automatically generate the AssemblyInfo.cs file with Nant, but how would I get the SVN revision number? That's what I came across Eugene's NAnt.SvnFunctions code. I have placed the code I modified on my Google Code Repository which you can find here. Just checkout the code and compile. The credit for the class goes to Eugen Anghel from Syntactic.org. All this class is really doing is just wrapping some basic SVN command line calls and parsing the output to use as a return parameter

    The function that I am using within his class from NAnt to get the SVN revision number is GetRevisionNumber(string path, string username, string password), since my SVN repository requires a username and password to access it. The original code did not have this capability. I also added IDictionary overloads to pass command line arguments to subversion.

    Now, before I did any of the steps below you have to create a SolutionInfo.cs file as a Solution Item. I placed at least the AssemblyVersionAttribute in the file so you can have all of your projects reference this one SolutionInfo.cs. This will version all of your assemblies in your solution with the same version number. I used a static build number like 1.0.0.0 in the SolutionInfo.cs file. This way, if you see this version in any build, you will know that it is from a developer machine and not your build server.

    Then in each of your projects, you have to remove the AssemblyVersion attribute from the AssemblyInfo.cs file so it does override the SolutionInfo.cs attribute. Within each project, you have to "Add an existing Item" > Select SolutionInfo.cs from your solution root and instead of clicking "Add", you have to click the little arrow next to "Add" and choose "Add as link". This will create a shortcut to SolutionInfo.cs. Once this is done for all projects, you can continue. 

    First compile the SvnFunctions class and place it on your build server somewhere. I place mine under the lib\tools\nant\ folder. Then add a "loadtask" element to your nant build file like so:

     <loadtasks assembly="path\to\lib\tools\nant\Nant.SvnFunctions.dll"/>

    This gives us access to all the functions within SvnFunctions.cs. Next you have to add the asminfo task BEFORE you build your application in your nant build file. This is what mine looks like:

      <asminfo output="${build.dir}\src\SolutionInfo.cs" language="CSharp">
       <attributes>
        <attribute type="System.Reflection.AssemblyVersionAttribute" value="1.5.${CCNetLabel}.${svn::get-revision-number('http://SVN-Server/svn/someproject', 'svn_username', 'svn_password')}"/>
        <attribute type="System.Reflection.AssemblyCompanyAttribute" value="SomeCompany" />
        <attribute type="System.Reflection.AssemblyCopyrightAttribute" value="Copyright - SomeCompany 2007" />
       </attributes>
      </asminfo>

    As you can see, what we are doing here is first telling nant that we want it to generate the SolutionInfo.cs file. Then we define what attributes to place into the file. In my example we have AssemblyVersion, AssemblyCompany and AssemblyCopyright. You can place more or less, this is what was generic for me across all projects.

    Notice in the AssemblyVersionAttribute we have 1.5.${CCNetLabel}. This places the CCNet Build number in ${CCNetLabel}. Then at the end we are calling the GetRevisionNumber method from the SvnFunctions class we compiled earlier passing in the repository address, username and password.

    The next time you build, you will notice all output assemblies from your solution will be versioned with the appropriate cc.net build number and revision number. This gives you a clear paper trail to be able to track down problems in a production assembly. Pretty useful.

    Thanks goes to Eugen Anghel that created the SvnFunction class to perform all the hard work. Please feel free to post problems you find or any questions you may have. Hope that helps someone!

Copyright Los Techies 2007. All rights reserved.
Powered by Community Server (Commercial Edition), by Telligent Systems