Ang3lFir3 – Life as a Code Poet

January 26, 2011

Introducing Chewie for nuget

Filed under: .NET, C#, Microsoft, technology, Uncategorized, web development — Tags: , , , , , , , , — ang3lfir3 @ 4:45 pm

So anyone following my friends and co-workers Jeff Schumacher (@codereflection), Adron Hall (@adronbh) and Bobby Johnson (@NotMyself) is aware that today we [ok really just them, I was busy on a diff less awesome project] started giving nuget a serious trial run to manage our extensive list of dependencies. A lot of which are not available on nuget at the moment.

This prompted a few blog posts from the guys here and here.

We kinda came up with the idea that we really needed something along the lines of bundler for ruby gems. So I started with the most simple version possible and Chewie was born. (All credit for the name goes to @NotMyself). At the moment its a basic file and a single line of powershell goodness.

check it out and contribute @ github

January 25, 2011

ExpandoObject and Views

This sample was spawned by a comment made by my friend and co-worker Bobby Johnson (@NotMyself) . I can’t remember exactly what he said but it had something to do with dynamic and views. (maybe he will tell us in the comments :-) )

Let me first say that many of the projects at work on built on MonoRail which is often considered to be the predecessor to ASP.Net MVC and as such we don’t get a huge amount of exposure to the new toys in ASP.Net MVC immediately.

With that out of the way I was getting prepared to write this post and I just happened to notice that ASP.Net MVC views in .Net 4.0 projects seem to all be of type System.Data.ViewPage<dynamic> , which got me super excited! This makes things even easier! (more…)

January 24, 2011

Refactoring MVP to MVC the slow way : pt1 Extracting Services

Filed under: .NET, ASP.Net MVC, C#, Patterns, technology, Uncategorized, web development — Tags: , , , , , , , , , — ang3lfir3 @ 9:58 pm

In this series I would like to examine the process my team and I have been undertaking on our current project. One of the aspects of working on legacy applications is that when you dive in you often see patterns that are not easily testable. The current application my team is working on was built many years ago and implements MVP (model view presenter) ,which at the time was a useful pattern for developing testable ASP.Net webforms applications. In the years since then many new patterns and frameworks have emerged and it is our desire to move the application from MVP to MVC (model view controller).

This series assumes that you are familiar with a few patterns and concepts namely the following:

  • Dependancy Injection
  • Inversion of Control
  • SOLID principals
  • MVP
  • MVC
  • TDD
  • Mocks/Stubs

(more…)

October 19, 2010

I can haz tests?

Filed under: .NET, BDD, C#, Patterns, TDD, Uncategorized, web development — Tags: , , , , , , , , , , — ang3lfir3 @ 8:47 pm

So our friend David Burela is back at it again with Developer Blog Banter #2: How do you test your applications?

Asking:

How do you organize your tests. Do you separate your unit tests, integration tests and UI tests into separate projects? Do you do anything specific to keep track of your tests? What naming conventions do you use? Do you run them before a check in or is that what the build server is for?
If you are not testing, then how would you like to test your apps if given the opportunity?

This post is my response to the above question.

I tend to have a Specs assembly for each major component of a project, these usually include some form of integration tests using in memory SQLite databases. The first thing most people will think is that I am probably mixing integration tests with my specs/unit tests and getting everything all mishmashed together. That is probably a good observation. As I find myself almost exclusively using MSpec for testing I see no real reason to separate the tests into any other grouping other than by their system components.

An example set of Test assemblies would be :

Monkeys.Core.Specs
Monkeys.Web.Specs

I did learn a few tricks from my friend @cbilson that I really liked and continue to use. That is naming the actual files in the test assemblies about the feature or part of the system we are testing. So for a group of specs that test the calculation of a bunch of distribution dates for a retirement fund based on some frequency (monthly, quarterly, annually etc) the name of the file would be “creating_distribution_dates_for_funds.cs”. This name is also used for the namespace that all the tests live in since MSpec tests are each a separate class. Groups of related tests can be found quickly and helps others that may come on to the project find the tests that describe how something works.

Okay so about those in-memory database integration tests. Well this is another thing that @cbilson and I worked on together (ok mostly him but I helped). Its certainly not a new idea , I got it from some blog post of ayende’s that I read, but it was a major breakthrough in helping us move quickly with testing and be extremely accurate. We were able to not only have nice _FAST_ tests for mappings in nHibernate but also were able to test queries quickly and accurately. Having this ability helps a lot when you want to be able to test with not only the database for your application but also for test versions of other databases you may need to access (most of our apps use at least 3-4 databases). This can make repository testing a no brainer and helps eliminate the kludgy methods people have had to use in the past. @NotMyself, @codereflection and I have even gone so far as to integrate NBuilder into the process for some scenarios making tests clean, expressive and to the point. I’m getting sidetracked I think…. testing is exciting stuff damnit!

Of course all our tests are run on the CI server and we “always” run them all before committing…. right? ;)

I would like to one day soon find a really elegant way to add some more integration testing, maybe even at the UI level, into the process. As this is very painful and hard to maintain today we do our best to test as much as we can. There is no substitution for great comprehensive QA we just hope we make their jobs a lot easier by building well designed rock solid software… that works.

Hope this covers the question and hope someone finds some value in it.

See also my response to the first Developer Blog Banter : My Technology Stack

August 17, 2009

Updates to Subclass and JoinedSubclass mapping in Fluent nHibernate 1.0 RC

Filed under: .NET, Fluent NHibernate, C#, NHibernate — Tags: , , , , — ang3lfir3 @ 3:20 pm

With the release of Fluent nHibernate 1.0 RC (download here) there are some great changes to the mapping structure of Subclass and JoinedSubclass. This excerpt from the 1.0 RC release notes gives a highlight of what those changes are and what is expected of us:

Separated subclass mapping – Subclasses can (and should be) defined separately from their parent mapping. Use SubclassMap<T> the same way as you would ClassMap<T>; if the top-most mapping (ClassMap) contains a DiscriminateSubclassesOnColumn call, the subclasses will be mapped as table-per-class-hierarchy, otherwise (by default) they’ll be mapped as table-per-subclass.

Okay so now we have quite a bit of info, but what does it all mean for the code. For those of you who just want to see the new mappings… I’ve got that for you. Using the same samples that I used in Table Per Class Hierarchy Inheritance Mapping with Fluent nHibernate and Table Per Subclass Inheritance Mapping with Fluent nHibernate I will show you the changes. Which are pretty simple and vary only slightly between the two approaches. This allows you to change mapping styles very quickly and flexibly.

 

The Mappings:

The Mappings for the Subclasses pretty much stay the same. The optional column name portions of the mapping are most helpful in mapping Table Per Class Hierarchy.

  public class BankAccountMap : SubclassMap<BankAccount>
  {
    public BankAccountMap()
    {
      Map(x => x.BankName, "BankAccount_BankName");
      Map(x => x.RoutingNumber, "BankAccount_RoutingNumber");
    }
  }

  public class CreditCardMap : SubclassMap<CreditCard>
  {
    public CreditCardMap()
    {
      Map(x => x.Type, "CreditCard_Type");
      Map(x => x.ExpirationMonth, "CreditCard_ExpirationMonth");
      Map(x => x.ExpirationYear, "CreditCard_ExpirationYear");
    }
  }

Here you can see the base class mapping for BillingDetails. To use a JoinedSubclass mapping as opposed to the Subclass mapping you just simply omit the DiscriminateSubClassesOnColumn() call.

  public class BillingDetailsMap : ClassMap<BillingDetails>
  {
    public BillingDetailsMap()
    {
      Id(x => x.Id);
      Map(x => x.Number);
      Map(x => x.Owner);
      Map(x => x.DateCreated);
      DiscriminateSubClassesOnColumn("BillingDetailsType");
    }
  }

kick it on DotNetKicks.com

August 6, 2009

Table Per Subclass Inheritance Mapping with Fluent nHibernate

Filed under: .NET, Patterns, Fluent NHibernate, ORM, C#, NHibernate — Tags: , , , , , , — ang3lfir3 @ 2:45 pm

Note: Updates for Fluent nHibernate 1.0 RC can be read here

This is the third in a short series of posts I am working on. While reading through NHibernate in Action I thought to my self that I should create these same examples of Inheritance mapping using Fluent nHibernate to show how simple these mappings can be.

The three types of Inheritance mappings that are mentioned on page 92 of NHibernate in Action are :

Model

  • Table Per Concrete Class
  • Table Per Class Hierarchy
  • Table Per Subclass

I will be using the example from NHibernate in Action to illustrate the mappings. This post covers the Table Per Subclass example. In Table per Subclass inheritance mapping we represent inheritance by using a foreign key to join tables together as needed.  This most closely represents the object model in the relational model of the database by using a table for the base class and one for each subclass.

TablePerSubclassFiles

Here you can see that again I only have one mapping file which covers the entire class Hierarchy.

Below you can see the Fluent Mappings for this approach. The JoinedSubClass<T> uses the Type as the table name by default (“CreditCard” and “BankAccount”) and the first parameter of the method is the name to use for the foreign key column. Additionally the mapping for the class being joined is included.

  public class BillingDetailsMap : ClassMap<BillingDetails>
  {
    public BillingDetailsMap()
    {
      Id(x => x.Id);
      Map(x => x.Number);
      Map(x => x.Owner);
      Map(x => x.DateCreated);
      JoinedSubClass<CreditCard>("CreditCard_Id", m =>
                                                    {
                                                      m.Map(c => c.Type);
                                                      m.Map(c => c.ExpirationMonth);
                                                      m.Map(c => c.ExpirationYear);
                                                    });
      JoinedSubClass<BankAccount>("BankAccount_Id", m =>
                                                      {
                                                        m.Map(b => b.BankName);
                                                        m.Map(b => b.RoutingNumber);
                                                      });
    }
  }

Which maps to the following nHibernate XML mapping file.

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="">
  <class name="TablePerSubclass.Model.BillingDetails, TablePerSubclass, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" table="`BillingDetails`" xmlns="urn:nhibernate-mapping-2.2">
    <id name="Id" type="Int32" column="Id">
      <generator class="identity" />
    </id>
    <property name="Number" type="String">
      <column name="Number" />
    </property>
    <property name="Owner" type="String">
      <column name="Owner" />
    </property>
    <property name="DateCreated" type="DateTime">
      <column name="DateCreated" />
    </property>
    <joined-subclass name="TablePerSubclass.Model.CreditCard, TablePerSubclass, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
      <key column="CreditCard_Id" />
      <property name="Type" type="Int32">
        <column name="Type" />
      </property>
      <property name="ExpirationMonth" type="String">
        <column name="ExpirationMonth" />
      </property>
      <property name="ExpirationYear" type="String">
        <column name="ExpirationYear" />
      </property>
    </joined-subclass>
    <joined-subclass name="TablePerSubclass.Model.BankAccount, TablePerSubclass, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
      <key column="BankAccount_Id" />
      <property name="BankName" type="String">
        <column name="BankName" />
      </property>
      <property name="RoutingNumber" type="String">
        <column name="RoutingNumber" />
      </property>
    </joined-subclass>
  </class>
</hibernate-mapping>

And the corresponding Database Schema. I followed the naming from the NHibernate in Action so you can see here maybe this is a little confusing. But basicly the “BankAccount_Id” is the primary key of the BankAccount table and contains is also a foreign key to the BillingDetails table.

TablePerSubclassSchema

kick it on DotNetKicks.com

August 4, 2009

Table Per Class Hierarchy Inheritance Mapping with Fluent nHibernate

Filed under: .NET, Patterns, Fluent NHibernate, ORM, C#, NHibernate — Tags: , , , , , — ang3lfir3 @ 2:32 pm

Note: Updates for Fluent nHibernate 1.0 RC can be read here

This is the second in a short series of posts I am working on. While reading through NHibernate in Action I thought to my self that I should create these same examples of Inheritance mapping using Fluent nHibernate to show how simple these mappings can be.

The three types of Inheritance mappings that are mentioned on page 92 of NHibernate in Action are :Model

  • Table Per Concrete Class
  • Table Per Class Hierarchy
  • Table Per Subclass

I will be using the example from NHibernate in Action to illustrate the mappings. This post covers the Table Per Class Hierarchy example. In Table per Class Hierarchy  inheritance mapping we enable polymorphism by using a type discriminator column to determine the Type when rehydrating objects. This means that all of the properties of all the subclasses including those of the base class are included in the single table. All columns that are not part of the base class must be nullable allowing the single row to reflect only the data required for the type being stored.

TablePerHierarchyFiles

Here you can see I have only one mapping file. This mapping file covers the entire class Hierarchy in a single file.

Below you can see the Fluent Mappings for this approach. The “DiscriminateSubClassOnColumn” indicates that name of the of the column in the Database that should be used to descriminate the types of each subclass. Fluent nHibernates default behaviour is to use the Class name as the descriminiator value. You can see how each Subclass is mapped. Notice that I used a prefix on the column names for the Subclass mappings. This allows the database schema to be easily readable.

  public class BillingDetailsMap : ClassMap<BillingDetails>
  {
    public BillingDetailsMap()
    {
      Id(x => x.Id);
      Map(x => x.Number);
      Map(x => x.Owner);
      Map(x => x.DateCreated);
      DiscriminateSubClassesOnColumn("BillingDetailsType")
        .SubClass<CreditCard>(m =>
                                {
                                  m.Map(c => c.Type, "CreditCard_Type");
                                  m.Map(c => c.ExpirationMonth, "CreditCard_ExpirationMonth");
                                  m.Map(c => c.ExpirationYear, "CreditCard_ExpirationYear");
                                })
        .SubClass<BankAccount>(m =>
                                 {
                                   m.Map(b => b.BankName, "BankAccount_BankNumber");
                                   m.Map(b => b.RoutingNumber, "BankAccount_RoutingNumber");
                                 });
    }
  }

Which maps to the following nHibernate XML mapping file.

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="">
  <class name="TablePerHierarchy.Model.BillingDetails, TablePerHierarchy, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" table="`BillingDetails`" xmlns="urn:nhibernate-mapping-2.2">
    <id name="Id" type="Int32" column="Id">
      <generator class="identity" />
    </id>
    <discriminator column="BillingDetailsType" type="String" />
    <property name="Number" type="String">
      <column name="Number" />
    </property>
    <property name="Owner" type="String">
      <column name="Owner" />
    </property>
    <property name="DateCreated" type="DateTime">
      <column name="DateCreated" />
    </property>
    <subclass name="TablePerHierarchy.Model.CreditCard, TablePerHierarchy, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
      <property name="Type" type="Int32">
        <column name="CreditCard_Type" />
      </property>
      <property name="ExpirationMonth" type="String">
        <column name="CreditCard_ExpirationMonth" />
      </property>
      <property name="ExpirationYear" type="String">
        <column name="CreditCard_ExpirationYear" />
      </property>
    </subclass>
    <subclass name="TablePerHierarchy.Model.BankAccount, TablePerHierarchy, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
      <property name="BankName" type="String">
        <column name="BankAccount_BankNumber" />
      </property>
      <property name="RoutingNumber" type="String">
        <column name="BankAccount_RoutingNumber" />
      </property>
    </subclass>
  </class>
</hibernate-mapping>

And finally you can see the relational model this maps to. Every property of every type is represented as a column. Those of the base class are included as well. You can see the discriminator column “BillingDetailsType” as well.

TablePerHierarchySchema

kick it on DotNetKicks.com

March 18, 2009

Fluent NHibernate new style mappings – powerful semantics

Filed under: Uncategorized, Fluent NHibernate, ORM, C#, NHibernate — Tags: , , , , , , — ang3lfir3 @ 5:56 pm

So today I updated to the latest build of Fluent NHibernate.  As any of you who might have done the same have discovered there are some breaking changes. I wasn’t sure I liked the new class based conventions at first, especially since it wasn’t clear at first how to tackle altering my mappings. Then it dawned on me how powerful the conventions would end up being while also promoting DRY.

as @jagregory said :

“brevity was sacrificed for power in this case.”

This can be seen in the example below which is a self referencing Parent Child relationship.

The description of the relationship is:

Categories can have one or none parents.

Categories can have many or no children.

The parent Category is always found in a property called “Parent”.

The children are always found in a property called “Children”.

Before

Original Mapping:

public class CategoryMap : ClassMap<Category>
{
  public CategoryMap()
  {
    Id(x => x.Id);
    Map(x => x.Name);
    References(x => x.Parent).TheColumnIs("parent_id").Cascade.All();
    HasManyToMany(x => x.Products).Inverse();
    HasMany(x => x.Children).WithKeyColumn("parent_id").Cascade.All().Inverse();
  }
}

**Note:  These aren’t exactly “Conventions” but it turned out that ‘WithKeyColumn’ got dropped and I had to look for a better way. The new style conventions offered that even over older convention styles.

After

The new style Convention Mappings :

The Convention classes below create a convention that reads like:

“For a HasMany when the child type matches the type of the root and the name of the property is ‘Children’  then use the column ‘parent_id’ as the KeyColumn. For a Reference when the child type matches the type of the root and the name of the property is ‘Parent’ then set its reference ColumnName to ‘parent_id’ “

public class SelfReferencingHasManyConvention : IHasManyConvention
{
  public bool Accept(IOneToManyPart target)
  {
     return target.Member.ReflectedType == target.EntityType && target.Member.Name == "Children";
  }

  public void Apply(IOneToManyPart target)
  {
     target.KeyColumnNames.Clear();
     target.KeyColumnNames.Add("parent_id");
  }
}
public class SelfReferencingReferenceConvention : IReferenceConvention
{
  public bool Accept(IManyToOnePart target)
  {
    return target.Property.ReflectedType == target.EntityType && target.Property.Name == "Parent";
  }

  public void Apply(IManyToOnePart target)
  {
     target.ColumnName("parent_id");
  }
}

The Mapping after the convention :

Clean and clear, the conventions themselves are not cluttering the Mapping. More importantly the conventions help me stay DRY.

public class CategoryMap : ClassMap<Category>
{
  public CategoryMap()
  {
    Id(x => x.Id);
    Map(x => x.Name);
    References(x => x.Parent).Cascade.All();
    HasManyToMany(x => x.Products).Inverse();
    HasMany(x => x.Children).Cascade.All().Inverse();
  }
}

Adding Mappings to my Persistence Model

You can see that adding the conventions was pretty easy and straight forward. This applies to the fact that I am using the PersistenceModel approach.

public class DataModel : PersistenceModel
{
  public DataModel()
  {
     AddMapping(new ProductMap());
     AddMapping(new CategoryMap());
     ConventionFinder.AddFromAssemblyOf<DataModel>();
   }
}

kick it on DotNetKicks.com

May 30, 2008

ASP.Net MVC , Visual Web Developer 2008 Express SP1 Beta

After reading ScotGu’s Blog post in regards to the recent drop of ASP.Net MVC Preview 3 I discovered that the express Edition of VWD (Visual Web Developer) 2008 should now have support for MVC projects with the improvements tht came from VSD SP1 Beta. Exciting I know! Actual support in Express editions of VS products.

I really want to give a big thank you to Phil Haack, Scott Hanselman, Scott Guthrie and the rest of the ASP.Net MVC team. The bit of screen shot below really shows the level of improvement coming from MSFT development teams. Additions like this were things we couldn’t have dreamed of before in Express additions and now… they are available to everyone to share in the MVC goodness. This particular team are definitely raising the bar on excellence and creating a new standard I hope other teams follow.

 

March 26, 2008

Developing InfoPath 2007 Solution == Most Painful experience in my life

The title says it all!!! Well almost. For the last few days I have been banging my head on InfoPath 2007 as a platform for developing a solution. There are great many painful experiences in InfoPath 2007 as a development platform that I was already expecting (it is after all an Office product thus making it inherantly painful)¹ but I wasn’t expecting to have it just randomly crash VS any time I write a little code.

What I am trying to do, and hopefully someone smarter than me can tell me how, is to incorporate a custom dll into an InfoPath 2007 Template project in VS2005 AND be able to use that dll to do work on the Template (the library contains validation functions specific to US and I need it to be reusable). Now you might be thinking life was easy here, but let me through in a few wrenches…

We aren’t using the forms on machines that are NOT connected to our network. The Templates are published to machines that are in the field. No SharePoint,  No Forms Server, just templates and a mdb with the values for the drop down menus in it. Yup… each template comes with its very own personal copy of the database that contains nothing more than tables with values and labels for drop down lists. At a later date the data in the .xml files will be uploaded to a Database after being collected.

 Hopefully I have made it clear how these templates are being used (not my idea so i can’t provide any justifications)

So how does one use InfoPath 2007 to publish a template containing custom validation routines (complex enough that they need to be written in C#) that use a shared library and access fields on the form? WITHOUT THE WHOLE THING EXPLODING!!!! There are no examples that I can find anywhere of even developing with InfoPath in this sort of manner. I am not finding anything related to my issues on the team blog either.

Maybe I am missing something or simply just don’t get it…. what ever the case… hopefully someone can explain this to me…. cuz right now… I’m drowning.

HELP!!!

1) office applications in general always seem to have the goofiest API’s and worst documentation. If I just want to do something once and not become an expert in <Office_application_X_Development /> the pain is almost unbearable. It should be easy guys…. no really it should!

Theme: Silver is the New Black. Get a free blog at WordPress.com

Follow

Get every new post delivered to your Inbox.