using RunXc.Web;
using RunXc.DB;


RunXc


Where the DB meets the Web

Interface Driven Extensibility in .Net

clock July 14, 2010 23:34 by author

Extensibility as Defined by Wikipedia-

In software engineering, extensibility (sometimes confused with forward compatibility) is a system design principle where the implementation takes into consideration future growth. It is a systemic measure of the ability to extend a system and the level of effort required to implement the extension. Extensions can be through the addition of new functionality or through modification of existing functionality. The central theme is to provide for change while minimizing impact to existing system functions.

Late Binding and Interfaces

If you want to your application to be able to grow and be used in ways not originally planned for their are two concepts that you need to keep in mind. The first is Late Binding. 

The concept behind Late Binding is that your application is not aware of the specific implementation of a piece of functionality at compile time.  This basically boils down to creating your application with the idea that you or someone else will add to or change its behavior in the future.

The use of Interfaces is what allows this Late Binding to be able to happen in a simple manner.  Basically you define how your application can be extended and create Interfaces that modal the extension points of your application.  In the case of a data driven web site maybe you are going to allow custom validation/rendering and actions on the data or maybe you are going to have some widget/webpart framework that you are going to use.  Applications like Paint.Net allow you to add Image transformation plug-ins as the application is designed around image manipulation.  The more areas you open up to extension the longer shelf life your application is liable to have and the fewer growing pains that it will have.

MEF-Scripting(Iron*)-IOC

MEF- If you haven't heard of MEF(Managed Extensibility Framework) than I highly recommend you check it out. MEF is built around the idea of Importing/Exporting parts(Interfaces in our discussion here).  It allows you to declare what Interfaces you want to import and where you want to look for the dll's that have the created the classes that implement these Interfaces.  MEF handles the discovery and instantiation of the classes and makes the late binding a 5-6 line affair.

Scripting-  Using IronRuby and IronPython in your .Net application isn't actually late binding as the scripting languages aren't compiled into dll's and loaded at runtime but they allow your application to get instantiated objects that implement the Interfaces that you define at runtime.  I will put a note here that debugging IronScripts running in your .Net application is not the easiest thing ever.  For more information on how to get .Net interfaces from an IronRuby script check out this previous post http://blog.runxc.com/post/2010/03/12/Calling-an-Interface-Method-using-IronRuby-in-C.aspx

IOC-  Inversion of Control has many of the same benefits as MEF but its focus is more on being able to easily change out a piece of your application such as the logging or Repository as compared to MEF which focuses on Extensibility and plugins.  I won't go into IOC too much here as much has been said on it.

Putting it all together

In the application that I have been building we want to make extending the application as easy as possible and we have been using both MEF and IronRuby for different scenarios.  Using them more and more I have been realizing that the two technologies can be used interchangeably if set up correctly.

Code Example

I have been giving a few demonstrations lately to developers where I work to bring them up to speed in MEF and decided to incorporate IronRuby and have an example that uses both of them.

I have created a small console application in .Net 4.0 that is based off of a blog article called the MEF Zoo which I recommend you read if you want to get up to speed on how to use MEF.  The application "Interface Driven Extensibility Zoo" or IDE Zoo is a zoo that lets each of the animals speak and lets you know where the came from below is the code for the console app.

namespace IDE
{
    class Program
    {
        static void Main(string[] args)
        {
            Zoo zoo = new Zoo();
 
            foreach (IAnimal animal in zoo.Animals)
            {
                Console.ForegroundColor = ConsoleColor.White;
                Console.WriteLine("The {0} says:", animal.GetType().ToString());
                Console.ForegroundColor = animal.Color;
                Console.WriteLine(animal.Speak());
 
            }
            Console.ReadKey();
        }
 
    }
 
    public class Zoo
    {
        public Zoo()
        {
            ExtensibilityHelper.Compose(this);
        }
        [ImportMany]
        public List<IAnimal> Animals { get; set; }
 
    }
 
    [InheritedExport]
    public interface IAnimal
    {
        ConsoleColor Color { get; }
        string Speak();
    }
}

 

All of the magic  in IDE Zoo happens in the static method ExtensibilityHelper.Compose  shown below.

public class ExtensibilityHelper
{
    public static void Compose(Zoo zoo)
    {
        var runtime = Ruby.CreateRuntime();
            
        var engine = runtime.GetEngine("rb");
        //A catalog that can aggregate other catalogs
        var aggrCatalog = new AggregateCatalog();
        string pluginFolder = 
            Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\Plugins";
        //A directory catalog, to load parts from dlls in the Extensions folder
        var dirCatalog = new DirectoryCatalog(pluginFolder, "*.dll");
        //An assembly catalog to load information about part from this assembly
        var asmCatalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());
 
        aggrCatalog.Catalogs.Add(dirCatalog);
        aggrCatalog.Catalogs.Add(asmCatalog);
 
        //Create a container
        var container = new CompositionContainer(aggrCatalog);
 
        //Composing the parts
        container.ComposeParts(zoo);
            
        //Lets now get all the parts that are exposed as IronRuby scripts
        var rubyFileNames = 
            Directory.GetFiles(pluginFolder, "*.rb").Select(x => new FileInfo(x));
        var IAnimals = rubyFileNames.Where(x=> x.Name.StartsWith("IAnimal")).ToList();
        foreach (var animalInfo in IAnimals)
        {
            var animal = engine.Execute<IAnimal>(File.ReadAllText(animalInfo.FullName));
            zoo.Animals.Add(animal);
        }
    }
}

 

And just to add even more code below are some of the animals.

IronRuby Animal

require 'IDE'


class Snake
    include IDE::IAnimal
             
    def Color
        return System::ConsoleColor.Green
    end

    def Speak()
        return "Hisss".ToString()
    end
end
return Snake.new

 

.Net Animal

    public class Tiger: IAnimal
    {
        #region IAnimal Members
 
        public ConsoleColor Color
        {
            get { return ConsoleColor.Yellow; }
        }
 
        public string Speak()
        {
            return "Roar";
        }
 
        #endregion
    }

 

IDEZoo SolutionIDEZoo.zip

Submit this story to DotNetKicksShout it   Bookmark and Share  


SharePoint the right tool for the right job

clock May 17, 2010 21:31 by author

So I have been getting to know SharePoint lately as the project that I have been working on since December was sold as a SharePoint solution.  Now I have no doubt that what we are building is going to turn out but it just seems that too often SharePoint is sold by some as a Silver Bullet where everything should go.  I love the product for its document Sharing abilities and for how easy it is to just throw up a wiki or a blog if you need an internal place to put some information.  It is also nice if you need to set up a place to track information for your organization as long as you don't have too much information or it isn't too complex.

Where SharePoint is oversold

As I mentioned above SharePoint is nice for throwing up a wiki but if you need a good wiki for somethink a bit larger I wouldn't use SharePoints wiki as there are better wikis out there.  Same thing with the blog there are probably 50+ blogengines that do a better job.   As for storing your data SharePoint can get you up quickly but if you have more than 2,000 records in a list it is going to start bogging down.  

SharePoint has a good calendar that integrates with Outlook but if you want to put something other than tasks on the calendar you are going to have to build your own Calendar.  There is a lot that SharePoint does but often there is a better system designed specifically for what you need to do.  An example of this would be a bug tracking system that integrated with your Source Control System.  There are a lot of free systems that do a good job at this though with a lot of configuration you could use SharePoint to accomplish the same task.

Webparts aren't the only way to go

Many systems do a better job than SharePoint at their specific task but don't do the other things that SharePoint does which is probably why SharePoint gets used so often outside of scope. that is some organization is already using it and just want to add additional functionality outside of what it was meant for.  The silver lining that I have found is that very often you can just use the master pages of SharePoint and add your own ASPX pages/User Controls/JQuery etc and get past some of the defecencies of creating a UI without a designer/html page to envision what you are creating. 

A Waste of time?

So to rap up this poor post(as it has no code. all good posts should have some code).  I have been finding that you can get the job done in SharePoint and if you have a simple application then you might save some time leveraging the Authentication/UI of SharePoint but as the project grows you will find yourself taking longer to create the same functionality as I would have to say that 30-40% more time is thrown away developing in a SharePoint environment as the deploying/debugging iterative development cycles take longer in SharePoint.  Why couldn't all of our government clients ask for ASP.NET MVC?

--Just a note.  My experience/opinions noted above are about SharePoint 2007.   I have heard that debugging and storing more complex data is much better in SharePoint 2010 but I don't foresee our clients getting SP2010 for a couple years.

Submit this story to DotNetKicksShout it   Bookmark and Share  


Custom Software in Ogden Utah

clock April 5, 2010 22:39 by author

If you are looking for some custom software in Ogden, Utah you should take a look at the new company that I recently started this last year, Tek-Flow.   If you have a business need for some custom software I would like to hear about it. 

Specialties of Tek-Flow

  • Business Applications requiring input validation and business rules enforcement
  • Database Design with web and mobile interfaces to access and edit your data
  • Security Including encryption, password protection and threat analysis
  • Automating Reports and manual processes
  • Barcoding technologies including creating barcodes and mobile applications to read those bar codes

    Worried about cost?

    One of the focuses of Tek-Flow is to create Software Services to help small businesses automate their businesses processes and reduce the amount of paperwork.  If you have a business need for software let us know what you are willing to pay on a monthly basis and we will work with you to create a solution to solve your needs.
Submit this story to DotNetKicksShout it   Bookmark and Share  


MVC App for an Ogden Dentist

clock March 13, 2010 11:23 by author

So I recently created a small website for an Ogden Dentist by the name of Dr. J. Bryan Gilbert.  If you live in Ogden Utah and are looking for a Dentist I can highly recommend him as he is my family Dentist.

With that plug aside I found it quiet interesting building the site as I was able to use Oomph for the first time to create a virtual contact card along with the use of an hcard so that search engines and people can more easily find his dental office in South Ogden.

I built the site using ASP.NET MVC2 which made it quiet fun and I found that it was very easy to integrate some jQuery functionality to the site.

SparkViewEngine

I have been using the SparkViewEngine lately in any demo applications that I  build and have decided that I will be creating my next home project using the SparkViewEngine as it has some definite syntactical sugar.  So if you need a website I am looking for an excuse to build something to try out this new technology.

Oh and just for another plug go and check out the website of Dr. J. Bryan Gilbert

Submit this story to DotNetKicksShout it   Bookmark and Share  


Calling an Interface Method using IronRuby in C#

clock March 12, 2010 20:49 by author

So today while at work I needed to create a simple IronRuby prototype to make sure I understood how to host or call IronRuby code from C#.   I know that I have read a number of articles about how easy it is but when "Binging" around for a solution all I returned were a bunch of forum posts on the IronRuby site and no simple snippets that put all of the pieces below.

Using an Interface to make life easier in .Net 3.5

After reading a number of posts and putting some pieces together I decided that what I wanted to do was use a .Net Interface to interact with my IronRuby class that is meant to be as an extension layer to a .Net application that I am building.  Below is the code that demonstrates how to create a hosting engine in C# and call IronRuby methods from C# without using .Net 4.0 as my application is running on .Net 3.5.

using System;
using System.Collections.Generic;
using Microsoft.Scripting.Hosting;
using IronRuby;
 
namespace IronRubyConsole
{
    class Program
    {
        static int Main(string[] args)
        {
            var runtime = Ruby.CreateRuntime();
            var engine = runtime.GetEngine("rb");
            //engine.Execute("puts 'Just a Trial Method'");
            var scope = engine.Execute(@"
            require 'IronRubyConsole'
 
            class ExampleCalc
               include IronRubyConsole::ICalc
             
              def calculate()
                puts 'hello IronRuby from C# code'
                return 0
              end
            end
            return ExampleCalc.new
");
 
            ICalc calculator = (ICalc)scope;
            var result = calculator.calculate();
            return 0;
        }
 
    }
 
    public interface ICalc
    {
        object calculate();
    }
}
Submit this story to DotNetKicksShout it   Bookmark and Share  


Blog.RunXc

View Bret Ferrier's profile on LinkedIn

Read an Article and Need Help?

Consulting/Contracting -Get a bid

OpenSouce Projects I like -jQuery, SubSonic, Mono, CC.Net

Languages- C#,javascript, VB, SQL, T-SQL, PSQL

DataBases- SqlServer,Oracle,MySql, SQLite, Sql Anywhere

Linux Flavors- OpenSuse, Ubuntu

VM Preference - VirtualBox

Least Favorite Reporting Technology-Crystal Reports

Sign in