Monthly Archives:

Combining OPF3 with Windows Communication Foundation

I've been experimenting lately with Windows Communication Foundation (WCF) and OPF3. I created two classes, one for OPF3 and one for my WCF service. It became increasingly tiresome to maintain both classes whenever the table was updated. I really wanted to clean up my code by removing any "convert-from-opf3-object-to-wcf-object" functions, so I decided to combine both of them!

This solution worked better than I thought it would. Now I have an object that can be loaded straight from an SQL query and returned from my WCF contract to the client Silverlight application. Really all it involved was adding a couple more attributes and viola! Here's an example of what the final class looked like.

[Persistent( Rights = PersistentRights.Load )]
[DataContract( Name = "DiveSite", Namespace = Information.DiveService.DataObjects )]
public class DiveSite : BaseServiceContract, ISelfContainingObject, IContainsFaultDetail
{
    [DataMember]
    [Field( "DIVESITEID" )]
    public int DiveSiteId { get; set; }

    [DataMember]
    [Field( "DIVESITENAME" )]
    public string DiveSiteName { get; set; }

    [DataMember]
    [Field( "PRIMARYDIVESITETYPEID" )]
    public int? PrimaryDiveSiteTypeId { get; set; }

    [DataMember]
    [Field( "MARKERTYPEID" )]
    public int MarkerTypeId { get; set; }

    [DataMember]
    [Field( "ISBOOKMARKED" )]
    public bool IsBookmarked { get; set; }

    [DataMember]
    [Field( "LATITUDE" )]
    public decimal Latitude { get; set; }

    [DataMember]
    [Field( "LONGITUDE" )]
    public decimal Longitude { get; set; }

    [DataMember]
    public DiveSiteDetail DiveSiteDetail { get; set; }

    public FaultDetail FaultDetail { get; set; }

    public ObjectInfo ObjectInfo { get; set; }

    public static List<DiveSite> GetDiveSites( Location northWest, Location southWest )
    {
        return ObjectContextFactory.ObjectContext.GetObjectSet<DiveSite>( new SqlQuery( "EXEC spGetPublicDiveSites " ) ).ToList();
    }
}


With the addition of the DataContact and DataMember attributes I now could have one class that could be loaded with OPF3 and then sent to the client. Really helped with cleaning up any "conversion" functions.

Visual Studio 2010 Designer Exception: UnresolvedAssemblyException

Recently I've run into an issue with while coding in XAML. Here at Pure Logic we have a custom library that handles our user input validation. Simply, the library has static methods such as IsEmail and IsPhoneNumber.

I wanted to leverage the power of XAML Validators and our custom library to create a regular expression ValidationRule. The goal was to create a ValidationRule that could be added to the Binding.ValidationRules element. Seemed easy enough to do. All that was left to be done was make a call to the validation library on the Validate method. The app compiled and worked like a dream. However, there was one issue that annoyed me greatly.

Any XAML that included one of my validators would throw a dreaded System.Reflection.Adds.UnresolvedAssemblyException within the designer. I has assumed that Visual Studio would just pull my custom library from the bin folder of my application, or even look up where the reference was from the project. But that's not the case. I took a screen shot so you can see what I was dealing with.

Designer Exception

The frustration of not being able to see what I was working with in the designer was driving me mad. After some digging around I found a solution to my problem. Microsoft has bundled a tool with the .Net framework called fuslogvw.exe. This little tool allows you to view the binding failure log.

I cleared out the log file, and started up fuslogvw.exe (VS command prompt). Then I opened up Visual Studio and waited for the designer to throw an exception. The exception was logged and showed up with fuslogvw.exe.

After reading through the log I found a number of locations as to where Visual Studio 2010 was looking for my custom library. I picked one of the locations, dropped my custom library in, and presto everything worked! I ended up dropping my in C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\PublicAssemblies but you can pick any of the locations listed in your log.

Hope this helps!

Winning in the New Economy

Winning in the New Economy - How to ensure your business thrives in tough times!

We are proud to sponsor the exciting and informative workshop "Winning in the New Economy". This educational event is presented by Newcap Radio and will be held at the Telus World of Science (11211 - 142 st, Edmonton) on October 14, 2010 - doors open at 7:00 am, workshop commencing at 8:00 am and running till 10:30 am. Continental breakfast provided by Fresh Start Bistro & Catering

This workshop is perfect for business owners and professionals who are interested in learning ways to fight the economic downfall with successful results!

Workshop topics include:

  • The business basics litmus test
  • The new media and traditional media mix
  • How to succeed without discounting
  • The roles of intrusive media versus passive media
  • The keys to unlocking advertising ROI
  • The Share of Market formula
  • How your customers define "value"

Tickets available at the door - admission $125.00

~Get Excited about Learning~

~Keep your eyes peeled for our Think Tank "mobile ad"~

~Visit our booth at the event for more information on our promotions~

Sys.Extended is undefined

Lately I've been playing around with the ToolKitScriptManager. I've read of some cool tricks to combine all those pesky scripts into one Javascript download. Not only would have make things much cleaner, it would reduce the overhead of files required to download. Now some of the projects that I work on rely heavily on Javascript to display the UI correctly. We love our clients so we wanted to do everything technically possible to speed up their monster application and the ToolKitScriptManager seemed like a logical place to start.

So I dropped a ToolKitScriptManager onto the page with a couple references to some commonly used files. And the result was my entire application breaking horribly in various places. Firebug reported a dreaded Sys.Extended is Undefined. Well that would be reason enough to revert all changes, but I wanted so bad for this feature to work I couldn't help but spend time researching to figure out the issue.

Basically it came down to the order of my composite scripts that fixed the issue. Instead of this


<compositescript>
<scripts>
<asp:scriptreference name="WebForms.js" assembly="System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
</asp:scriptreference>
<asp:scriptreference name="MicrosoftAjaxWebForms.js" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
</asp:scriptreference>
<asp:scriptreference name="MicrosoftAjax.js" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
</asp:scriptreference>
</scripts>
</compositescript>



I had to reorder my scripts to this


<compositescript>
<scripts>
<asp:scriptreference name="WebForms.js" assembly="System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" scriptmode="Release"></asp:scriptreference>
<asp:scriptreference name="WebUIValidation.js" assembly="System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" scriptmode="Release"></asp:scriptreference>
<asp:scriptreference name="MicrosoftAjax.js" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" scriptmode="Release"></asp:scriptreference>
<asp:scriptreference name="MicrosoftAjaxWebForms.js" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" scriptmode="Release"></asp:scriptreference>
</scripts>
</compositescript>

Hope this helps anyone else with this issue.

OPF3 - How to make relations between objects & tables

Treating tables as objects in my applications is a time saver. I don't have to worry about updating SQL statements, or even sanitizing my data against SQL injections. OPF3 takes care of all this very nicely for you.

Now typically, loading up an object from the database is the most you'll need to do, but like 100% of all the good databases out there, you'll have a relationship between two tables. Depending on the database you can have either a one-to-many or a many-to-many relationship. OPF3 has support to create these same relationships in your code. Best of all the relationships utilize lazy-loading, so they'll only be pulled from the database when they're called.

For our purposes we're going to have three tables (Orders, OrderItems and Customers) in our database. These three tables represent a majority of table relationships out there.

Table Relations

 

Our primary focus will be on the Orders table. This application design allows an Order to have only one customer at a time, but with many OrderItems. We'll start with the simple customer relationship.

OPF3 uses the ObjectHolder class to manage relationships. Within our Order class we'll define a private variable for our relationship.


private ObjectHolder<Customer> _CustomerRelation = new ObjectHolder<Customer>();

We now have an object to hold our relation but we need to define exactly what our relationship is. OPF3 does not, and can not guess your column names. Here's where we use the Relation attribute.


[Relation( "CustomerId=Id", PersistRelationship = PersistRelationships.ChildFirst )]
private ObjectHolder<Customer> _CustomerRelation = new ObjectHolder<Customer>();

Now basically the relation attribute defines our relationship between the Order class and Customer class. This is specified by the "CustomerId=Id". Next, since an Order cannot exist without the Customer, the PersistRelationships.ChildFirst enum tells OPF3 that the Customer relationship should be saved / updated into the database before the Order. Last but not least, we create a property to access our relationship. This just removes one step when accessing the object. The final result looks like this.


[Relation( "CustomerId=Id", PersistRelationship = PersistRelationships.ChildFirst )]
private ObjectHolder<Customer> _CustomerRelation = new ObjectHolder<Customer>();
public Customer CustomerRelation
{
    get { return _CustomerRelation .InnerObject; }
    set { _CustomerRelation .InnerObject = value; }
}

Now the relationship for the OrderItems is a one-to-many relationship. Instead of accessing a single object like the customer, we'll be accessing a enumeration of OrderItems. Instead of using the ObjectHolder class, we'll utilize the ObjectSetHolder class, and our property will return an ObjectSet (collection of Persistent objects). Here's what the result will look like.


[Relation("Id=OrderId", PersistRelationship = PersistRelationships.ParentFirst)]
private ObjectSetHolder<OrderItem> _OrderItemsRelation = new ObjectSetHolder<OrderItem>();
public ObjectSet<OrderItem> OrderItemsRelation 
{
    get { return _OrderItemsRelation.InnerObject; }
    set { _OrderItemsRelation.InnerObject = value; }
}

Three things to note here. First, the PersistRelationships is set to ParentFirst. This is because the parent (Order) must exist within the database before the OrderItems can. Second, the ObjectHolder is replaced with ObjectSetHolder, and third the property returns an ObjectSet instead of a single object. Now in your code your can access and manipulate relationships as easy as this


Order loadOrder = _context.GetObject<Order>( "Id={0}", 15 );

string message = "Dear " + loadOrder.CustomerRelation.FirstName + " " + loadOrder.CustomerRelation.LastName + ". Here is a summary of your order." + System.Environment.NewLine;

foreach(OrderItem item in loadOrder.OrderItemsRelation)
{
     message += item.ProductRelation.Name + " x " + item.Quantity + " @ " + item.ProductRelation.Price.ToString( "C" ) + " = " + ( item.Quantity * item.ProductRelation.Price ).ToString( "C" ) + System.Environment.NewLine;
}

//TODO send an the message via email

Inserting and Updating

Now while these relationships are awesome for loading, they're fairly straight forward for persisting to the database. Lets pretend that your shopping cart was going to create the order, customer and order items all at once. Here's what it would look like in your code.


Order newOrder = new Order();
newOrder.EmployeeId = CurrentUser.Id;
newOrder.OrderNumber = GenerateOrderNumber();
newOrder.OrderDate = DateTime.Now;
newOrder.OrderStatus = ( int ) eOrderStatus.New;
newOrder.Status = 1;

newOrder.CustomerRelation = new Customer();
newOrder.CustomerRelation.FirstName = txtFirstName.Text;
newOrder.CustomerRelation.LastName = txtLastName.Text;

newOrder.CustomerRelation.Address1 = txtAddress1.Text;
newOrder.CustomerRelation.PostalCode = txtPostalCode.Text;
newOrder.CustomerRelation.City = txtCity.Text;

newOrder.OrderItemsRelation = new ObjectSet<OrderItem>();
newOrder.OrderItemsRelation.Add( new OrderItem { ProductId = 1, Quantity = 3 } );
newOrder.OrderItemsRelation.Add( new OrderItem { ProductId = 4, Quantity = 1 } );
newOrder.OrderItemsRelation.Add( new OrderItem { ProductId = 5, Quantity = 6 } );

try
{
    using(Chili.Opf3.Storages.Transaction t = _context.StartTransaction())
    {
        _context.PersistChanges( newOrder );
        t.Commit();
    }
    //Success with the order, redirect
}
catch ( Exception ex )
{
    MessageDisplay( ex );
}

Wrap it all up in a transaction and you're all set. OPF3 will find the relationships you created and insert the objects in this order: CustomerRelationship, Order, and finally OrderItemsRelationship.

Umbraco & XSLT

I've recently been falling in love with XSLT and what it can accomplish for me. The heart and soul of Umbraco is built upon XSLT, so when I was first introduced I had a hard time adjusting to how everything would work. Last night that all changed. If you're in anyway confused as to how XSLT works, you should read the articles on WC3 Schools. Their XPATH chapter greatly helped me get a grasp on all the select statements going on.

Next you need to burn this XSLT element into your brain.


<xsl:copy-of select="" />

One of the major issues I've had with XSLT is not being able to see the XML/data that I'm playing with. This handy little element outputs whatever you put into the select attribute as XML that you can work with.

So if you want to learn XSLT and XPATH quicker, and you want to know where all the built-in Umbraco values come from, simply put this in your XSLT template.


<xsl:copy-of select="$currentpage" />

Now view source on the page calling the marco and you'll have some XML that you can touch and feel!

Combined with some reading at W3Schools for XPATH and you'll be well on your way to coding XSLT in no time!

Syntax Highlighter for Umbraco 4

It's been a challenge for the past couple weeks to integrate a syntax highligher into Umbraco. I've done countless Google searches, read blogs, form posts and how-to articles, but I still haven't come across an update-to-date syntax highlighter tutorial for Umbraco 4. I've decided to rewrite this article to use more up-to-date methods and libraries, and to also provide a full explanation of setting up a syntax highlighter in Umbraco 4.5

There are two parts the the syntax highlighter.

  • Client side javascript to display and colour the code
  • Tinymce plugin to make it eaiser to manipulate content

We're first going to start with the tinyMCE plugin. There are many plugins to choose from for tinyMCE. At the time of this article I choose Rich Guks syntakhl. Download syntaxhl and put it in your umbraco_client\tinymce3\plugins folder. Make sure the folder has read permissions allowed.

Next we need to tell Umbraco that we want to use the plugin we just installed. Crack open config\tinyMceConfig.config and add the following to the commands element.

<command>
   <umbracoAlias>syntaxhl</umbracoAlias>
   <icon>images/editor/codehighlight.gif</icon>
   <tinyMceCommand value="" userInterface="true" frontendCommand="template">syntaxhl</tinyMceCommand>
   <priority>76</priority>
</command>

The priority number should be unique. Just increment it from the last command element you find. Mine happened to be 76.

Next add the following to the plugins element

<plugin loadOnFrontend="false">syntaxhl</plugin>

Save the config file and restart the website (open up and save web.config file). Now in the developer section of Umbraco, look up the Richtext editor datatype. In the buttons list should be your syntax highlighter button. Check the box to add it and then save the datatype. Now all of your Richtext editors in Umbraco will have the code hightlighter button appended to the end of the list. You can now start to write content that uses the syntax highlighter. This is only the first part. Now that we have the means to create and manipulate content as code, we need to display it on the client side using javascript library.

Download Alex Gorbatchev's SyntaxHighlighter library and add it to your websites script folder. At the time of this article the version was 3.0.83

Place the following code right before the </body> tag in your master page:


<!-- SyntaxHighlighter CSS and JavaScript -->
    <link type="text/css" rel="stylesheet" href="/syntaxhighlighter/styles/shCore.css">
    </link>
    <link type="text/css" rel="stylesheet" href="/syntaxhighlighter/styles/shThemeDefault.css">
    </link>

    <script type="text/javascript" src="/syntaxhighlighter/scripts/shCore.js"></script>
    
    <!-- Brushs! Include the file for each language you want to use -->
    <script type="text/javascript" src="/syntaxhighlighter/scripts/shBrushCSharp.js"></script>
    <script type="text/javascript" src="/syntaxhighlighter/scripts/shBrushJava.js"></script>
    <script type="text/javascript" src="/syntaxhighlighter/scripts/shBrushXml.js"></script>
    <script type="text/javascript" src="/syntaxhighlighter/scripts/shBrushSql.js"></script>
    
    <script type="text/javascript"> 
        SyntaxHighlighter.config.stripBrs = true;
        SyntaxHighlighter.defaults['smart-tabs'] = true;
        SyntaxHighlighter.all(); 
    </script>

 

Thats it! I later plan to rewirte the tinyMCE plugin to include the new features of SyntaxHighlighter. Enjoy!

Welcome to OPF3

OPF3: The Painless ORM

I've been working with OPF3 for the past three years and I tell you, it has been a huge time saver. During my schooling, the standard method for manipulating a database would include writing SQL stored procedures, and then write methods within your data access layer to communicate with these stored procedures. Return values, @@IDENTITY, and methods taking lengthy parameters are all but a thing of the past, thanks to OPF3. When I was first introduced to OPF3, my first and initial thoughts were "huh?" and "what's wrong with stored procedures?

First of all, there is nothing wrong with stored procedures. In fact I still use them alongside the OPF3 framework. OPF3 isn't without its limitations though. Currently there is no support for return values. OPF3 was designed as a lightweight ORM (Object Relationship Manager) to eliminate mundane coding.

How does OPF3 work?

Imagine each table in your database as an object. For every table you have an object equivalent in your application. And just as you might expect, each object would have properties that map to the corresponding column in your database. It's a pain free process. Here's a tiny example of what such a class would look like.


[Persistent( "WebUsers" )]
        public class WebUser :ISelfContainingObject
        {
            [Field( "Id", Identifier = true, AutoNumber = true, AllowDBNull = false )]
            public int Id { get; set; }

            [Field( "Login", AllowDBNull = false )]
            public string Login { get; set; }

            [Field( "FirstName", AllowDBNull = false )]
            public string FirstName { get; set; }

            [Field( "LastName", AllowDBNull = false )]
            public string LastName { get; set; }

            [Field( "SALT", AllowDBNull = false )]
            public string SALT { get; set; }

            [Field( "HASH", AllowDBNull = false )]
            public string HASH { get; set; }

            [Field( "CreateDate", AllowDBNull = false )]
            public DateTime CreateDate { get; set; }

            [Field( "Status", AllowDBNull = false )]
            public int Status { get; set; }

            private ObjectInfo _ObjectInfo = new ObjectInfo();
            public ObjectInfo ObjectInfo
            {
                get { return _ObjectInfo; }
                set { _ObjectInfo = value; }
            }
        }

 

As you can see from my WebUser class, I've got full control of all columns in the WebUsers table. Now inserting / updating is as easy as this!


public enum eWebUserStatus
        {
            Active,     
            Pending,     
            Inactive
        }

        public void btnSaveWebUser_Click( object sender, EventArgs args )
        {
            //instansiate our WebUser class and assign the variables     
            WebUser newUser = new WebUser();
            newUser.FirstName = "Matt";
            newUser.LastName = "Tycholaz";
            newUser.Login = "matty";
            newUser.CreateDate = DateTime.Now;
            newUser.SALT = Tools.GenerateSALT();
            newUser.HASH = Tools.GenerateHASH( newUser.SALT + "secretpassword" );
            newUser.Status = ( int ) eWebUserStatus.Pending;

            try
            {
                //do an insert using a transaction         
                using ( Chili.Opf3.Storages.Transaction t = ObjectContextFactory.ObjectContext.StartTransaction() )
                {
                    //push our changes into the database             
                    ObjectContextFactory.ObjectContext.PersistChanges( newUser );
                    //commit the transaction             
                    t.Commit();
                }
            }
            catch ( Exception ex )
            {
                //if an error occurs then display it         
                MessageDisplay.DisplayMessage( ex );
            }
        }

 

Stay tuned for my next post where I'll show you more advanced scenarios.