• Back From Training Syndrome

    Lately I've had some luck capitalizing on people's energy after having some training. I think it was good that they've all been going to training and events around the same time frame because it's easier to push a boulder up hill with 5 people than only 1.

    If you're pressing for agile practices or trying to encourage a shift in thinking PLEASE try this advice:

    1. get everyone to agree on at least a similiar type of training. TDD or Scrum for example
    2. if possible send everyone at the same time so they all have the same frame of reference and they can all push together
    3. when they get back encourage them to try what they've learned and pick specific places to use it
    4. help remove any roadblocks to their experiments
      1. remove work if necessary until a steady pace or the new practices take hold
      2. block interruptions to their learning process to let them solidify their new skills at work

    Training is good if you can capitalize on the result of it otherwise people will move on to somewhere that they can use those skills. Don't become that company that trains people for other companies.

     

    Full story

    Comments (0)

  • Is Ninject 2 Stealthy

    Based on this documentation for Ninject , they claim to be stealthy!

     

    Stating:

    Ninject will not invade your code. You can easily isolate the dependency on Ninject to a single assembly in your project.”

     

    Well, at first I was skeptical and just could not figure it out. So I put on my scientist hat for a little while in an attempt to prove them right!

     

    Here’s what my test bed looked like… a pretty standard project configuration with multiple DLLs

     

      

     

     

    1. A console application. My driver program.
    2. A contracts DLL. My abstracts and interfaces that are known to the driver program.
    3. A service locator DLL. My services and Factories that will provide my driver program and class libraries everything they need.
    4. My first class library dll. This is where I would usually put some type of layered architecture or group of assemblies for a new release.
    5. My second class library dll. This is the same as above but separate from the previous one. 

     

       Now here are the references:  

     

    1, 4, and 5 reference 2 and 3. (and every dll you ever make after this would reference 2 and 3)

    3 references 4 and 5 (and any new dlls you ever make) 

     

    Here’s the layout of where all the type definitions are so you can visualize it:

     

     

     

    Here’s the driver program code. The test bed for this little experiment: 

    using System;

    using IsolatingTheNinja.Contracts;

    using IsolatingTheNinja.Services;

     

    namespace IsolatingTheNinja

    {

        class Program 

        {

            static void Main(string[] args)

            {

                ILibrary1Type type1Instance = ServiceLocator.CreateLibrary1Type();

                ILibrary2Type type2Instance = ServiceLocator.CreateLibrary2Type();

     

                Console.WriteLine(type1Instance);

                Console.WriteLine(type2Instance);

                Console.WriteLine("Press any key to exit");

                Console.ReadKey();

            }

        }

    } 

      

     

    Here’s my Service Locator implementation:

    using IsolatingTheNinja.Contracts;

    using Ninject;

    using Ninject.Modules;

     

    namespace IsolatingTheNinja.Services

    {

        public class ServiceLocator 

        {

            private static readonly IKernel _kernel = new StandardKernel(new INinjectModule[] { new Library1Module(), new

    Library2Module() });

     

            public static ILibrary1Type CreateLibrary1Type()

            {

                return _kernel.Get<ILibrary1Type>();

            }

            public static ILibrary2Type CreateLibrary2Type()

            {

                return _kernel.Get<ILibrary2Type>();

            }

        }

    }

      

     

    Here’s an example of my module:

    using IsolatingTheNinja.Contracts;

    using IsolatingTheNinja.Library1;

    using Ninject.Modules;

     

    namespace IsolatingTheNinja.Services

    {

        public class Library1Module : NinjectModule 

        {

            public override void Load()

            {

                Bind<ILibrary1Type>().To<Library1Type>();

            }

        }

    }

     

    The end result: SUCCESS. Ninject IS Stealthy! 

     Here’s the Source code

     

     

     

     

     

     

     

     

     

     

     

    Full story

    Comments (0)

  • Setting Up Fitnesse for C# .Net

    I use to use cory foy's example, but it's a bit dated now.

     

    Step 1:

    Download fitnesse

    http://fitnesse.org/FrontPage.FitNesseDevelopment.DownLoad

     

    click the fitnesse.jar link

     

    extract it to c:\fitnesse

     

    run java –jar fitnesse.jar –p [port]

    (If you want port 80 then leave off the –p and port)

     

    Navigate to http://localhost/ to try it out or http://localhost:[port]

     

    Step 2:

    Get fitsharp so you have the dlls for making fixtures and slim runner

    go to: http://github.com/jediwhale/fitsharp/downloads

    extract it to c:\fitnesse\slim

     

    Step 3:

    Make your test fixtures class library in visual studio

     

    Step 4:

    reference the fit and fitsharp dlls from c:\fitnesse\slim

    and reference any of your “System under test” dlls. (your dlls that your system uses that you want to test)

     

    Step 5:

    Write a fixture

    using fit;

     

    namespace Fitnesse

    {

        public class CalculateDiscount : ColumnFixture

        {

        }

    }

     

    Step 6:

    Make your first wiki page.

    Such as editing the front page and adding MyFirstFitnessePage

    It will show up like this after you save it:

    MyFirstFitnessePage?

    Click on the ‘?’ to create it.

     

    Step 7: make it a test page

    go to properties and change it to a “test” page.

     

    Step 8: Add all this stuff to the top of it

    !define TEST_SYSTEM {slim}

    !define COMMAND_PATTERN {%m -r

    fitSharp.Slim.Service.Runner,C:\Fitnesse\slim\fitsharp.dll %p}

    !define TEST_RUNNER {C:\Fitnesse\slim\Runner.exe}

    !path C:\projects\Fitnesse\Fitnesse\bin\Debug\Fitnesse.dll

     

    (the last line should be a direct path to your fixtures dll)

     

    Step 9:

    Make a test table

    |!-Fitnesse.CalculateDiscount-!|

    |amount     |discount?    |

    |0          |0             |

    |100        |0             |

    |999        |0             |

    |1000       |0             |

    |1010       |50.5          |

    |1100       |55            |

    |1200       |60            |

    |2000       |1000          |

     

    the !- -! Syntax forces literal text so it won’t interpret “CalculateDiscount” as a wiki page link you’ll get an error like this if you don’t do it

     

    Could not find class CalculateDiscountATitleCreatePageHrefCalculateDiscountEditNonExistentTrueA

     

    Apparently “ATitleCreatePageHrefCalculateDiscountEditNonExistentTrueA” gets appended when the ? is next to it.

     

     

     

    Full story

    Comments (0)

  • NHibernate basic 2 table example

    I’m slowly becoming intrigued by NHibernate and was playing around with it against AdventureWorks tonight.

     

    I came up with this basic 2 table tutorial using Employee and Contact.

     

    First, I created a wpf project for my user interface and I slapped a ListView on the main window like this:

    <Window x:Class="NHAdventureWorks.Window1"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="Window1" Height="300" Width="300"

         Loaded="Window_Loaded">

        <Grid>

            <ListView x:Name="lvwEmployees">

                

            </ListView>

        </Grid>

    </Window>

     

    Then I hooked up the Loaded Event:

    private void Window_Loaded(object sender, RoutedEventArgs e)

            {

                this.lvwEmployees.ItemsSource = LoadEmployeesFromDatabase();

            }

     

    Then I added this code from the ‘hello world’ example in Manning’s “NHibernate in action” book:

            static ISession OpenSession()

            {

                if (factory == null)

                {

                    Configuration c = new Configuration();

                    //c.AddClass(typeof(Employee));

                    c.AddAssembly(Assembly.GetCallingAssembly());

                    factory = c.BuildSessionFactory();

                }

                return factory.OpenSession();

            }

            static ISessionFactory factory;

     

            static IList<Employee> LoadEmployeesFromDatabase()

            {

                using (ISession session = OpenSession())

                {

                    IQuery query = session.CreateQuery(

                    "from Employee as emp order by emp.Contact.LastName asc");

                    IList<Employee> foundEmployees = query.List<Employee>();

                    return foundEmployees;

                }

            }

     

    Then I declared my domain model classes Employee and Contact as such:

        internal class Employee

        {

            internal int EmployeeId;

            internal string LoginId;

            internal Contact Contact { get; set; }

            internal int ContactId;

            public override string ToString()

            {

                return string.Format("{0} : {1} {2}", LoginId, Contact.FirstName, Contact.LastName);

            }

     

        }

        internal class Contact

        {

            internal int ContactId;

            internal string FirstName;

            internal string LastName;

     

        }

     

    Then I had to do the NHibernate specific stuff. First I added an xml file to the project called Employee.hbm.xml and then changed it’s build action to “Embedded Resource” (VERY IMPORTANT)

    Then I added a Contact.hbm.xml file as well (also marked as Embedded Resource). Then their contents looked like this respectively:

     

    <?xml version="1.0"?>

    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"

    auto-import="true">

      <class name="NHAdventureWorks.Employee, NHAdventureWorks"

             table="HumanResources.Employee" lazy="false">

        <id name="EmployeeId"

            column="EmployeeID"

            access="field">

          <generator class="native" />

        </id>

        <property name="LoginId" access="field" column="LoginID"/>

        <many-to-one

          name="Contact"

          column="ContactID"

          class="NHAdventureWorks.Contact, NHAdventureWorks"

          not-null="true" />

      </class>

    </hibernate-mapping>

     

    <?xml version="1.0"?>

    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"

    auto-import="true">

      <class name="NHAdventureWorks.Contact, NHAdventureWorks"

             table="Person.Contact"

             lazy="false">

        <id name="ContactId"

           column="ContactID"

           access="field">

          <generator class="native" />

        </id>

        <property name="FirstName" access="field" column="FirstName"/>

        <property name="LastName" access="field" column="LastName"/>

      </class>

    </hibernate-mapping>

     

    Of course there are some app.config settings that had to be put into place:

    <?xml version="1.0" encoding="utf-8" ?>

    <configuration>

      <configSections>

        <section name="hibernate-configuration"

                 type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />

        <section name="log4net"

                 type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />

      </configSections>

      <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">

        <session-factory>

          <property name="connection.provider">

            NHibernate.Connection.DriverConnectionProvider

          </property>

          <property name="connection.driver_class">

            NHibernate.Driver.SqlClientDriver

          </property>

          <property name="connection.connection_string">

            Server=(local);database=AdventureWorks;Integrated Security=SSPI;

          </property>

          <property name="dialect">

            NHibernate.Dialect.MsSql2000Dialect

          </property>

          <property name="show_sql">

            false

          </property>

          <property name='proxyfactory.factory_class'>NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>

        </session-factory>

      </hibernate-configuration>

      <log4net>

        <appender name="ConsoleAppender"

        type="log4net.Appender.ConsoleAppender, log4net">

          <layout type="log4net.Layout.PatternLayout, log4net">

            <param name="ConversionPattern" value="%m" />

          </layout>

        </appender>

        <root>

          <priority value="WARN" />

          <appender-ref ref="ConsoleAppender" />

        </root>

      </log4net>

    </configuration>

    Full story

    Comments (0)

  • Scrum Starting Practices: Sprint Planning

    This may or may not be helpful to some people but i just wanted to do a walk-through of a typical sprint planning 'process' I just tried using with the current team I'm on. We have me (programmer/scrum master), a tester, a business person, a requirement writer, and various 'specialists' giving us hours here and there (mainframe, webservices, and database). I'm basically acting as scrum master and sometimes as 'product owner' in many regards. (generally the product owner situations i end up in are ones where the technical team must prioritize and the customer team does not care one way or another on the priority at that low of level)

    1. I fire up Breeze (it's basically like a Webex or typical desktop sharing utility)
    2. I get everyone connected so they can see my desktop.
    3. I launch scrumworks (our chosen tool for distributed scrum) - maybe you use a spreadsheet or story cards or whatever.
    4. I load up our PRD (product requirements document) for a particular project (usually to add to our existing 'evergreen' product)
    5. I read the first "Detailed Business Requirement" outloud or ask the team to read it on the their own. (usually they have their own copy of the word document.
    6. Create a backlog item in scrumworks that represents that requirement.
    7. Have a timeboxed discussion (i usually don't set a time limit because people already have a tough time speaking up, but if it goes on too long in silence i start asking specific people questions... like 'if you had this software in front of you and you were going to validate this requirement, what would you do?)
    8. Ask the QA people what they would plan on testing. Maybe ask the business what QA might have missed or vice versa.
    9. break down the feature/backlog item into tasks. In scrumworks i usually denote each task with a functional group and then the task. for example the backlog item might be "User can save their settings". The tasks would maybe be "Dev: build aspx page layout", "Dev: build model classes", "Dba: setup tables and sprocs", "QA: write test scripts", "QA: execute test scripts" "BIZ: user acceptance test"
    10. Once the tasks have been decomposed and estimated in 'ideal hours' i total that up and assign the backlog item that as the 'ideal hour' size. Yes most tasks can run concurrently but the point is getting a general 'complexity' or 'size' to it so i figure there's a bit of shwag to it anyhow.
    11. Ask the team if they have enough room to commit to this backlog item taking into consideration all the functional groups needed to get to "Done" on it.
    12. Add it to the Sprint or skip it depending on 10
    13. Repeat steps 5-12 as needed until no more backlog items can be committed to.
    14. Some requirements may need consolidated into one backlog item (for example if a requirement is Dependant on another, I put them together as one and see if it makes sense... sometimes then we split them back out if it makes sense to split them in a different way by 'features' instead of by layers of software)
    15. Revisit estimates on the next reasonable amount of backlog items or estimate any un-estimated backlog items in the product backlog.

    Full story

    Comments (0)

  • Why is my scrum team not reporting impediments?

    5 little things I have done to lose the scrum team’s trust

     

    1. Turned responsibility back solely on the individual reporting the impediment.
    2. Tell them that it’s ‘just the way things work’ at this company or otherwise keep status quo
    3. Lose track of open impediments. Fall through the cracks.
    4. Assign someone different work when they’re impeded
    5. Embarrass them for silly questions or use “but” language in front of peers.

     

    Things we can do to get people to report all impediments

    1. Take every single impediment seriously
    2. be genuinely concerned and interested. Note your body language.
    3. Write it down somewhere visible.
    4. Make the solution to every impediment clearly visible to the whole team
    5. Take personal responsibility to ‘facilitate’ a solution with ‘team accountability’
    6. Thank them for reporting the issue

    Full story

    Comments (0)

  • Making a custom mouse cursor in silverlight 3

    When you write games in silverlight you are going to want to make a custom mouse cursor. Maybe you want a gun cross hair or maybe an animated cursor of some sort. I’ve found no way to do this other than to turn off the cursor and hook an image up to the mouse move events.

     

    Basically let’s start with a silverlight application and change the Grid to a Canvas like this:

    <UserControl x:Class="MouseCursorReplacement.MainPage"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

        mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">

        <Canvas x:Name="LayoutRoot">

           

        </Canvas>

    </UserControl>

     

    Now add a cursor representation as an image tag and point it to your new cursor file. In this case I’m using a cross hair. I make the ZIndex high so it floats on top of anything I put in.

    <UserControl x:Class="MouseCursorReplacement.MainPage"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

        mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">

        <Canvas x:Name="LayoutRoot">

            <Image x:Name="CustomCursorImage" Source="cross.png" Canvas.ZIndex="100"/>

        </Canvas>

    </UserControl>

     

    Now go into your code behind and setup a load event and disable the cursor:

        public partial class MainPage : UserControl

        {

            public MainPage()

            {

                this.Loaded += new RoutedEventHandler(MainPage_Loaded);

                InitializeComponent();

            }

     

            void MainPage_Loaded(object sender, RoutedEventArgs e)

            {

                this.Cursor = Cursors.None;

            }

        }

     

    Now add a mouse move event and finally the code that will make the image move around as your new cursor.

            public MainPage()

            {

                this.Loaded += new RoutedEventHandler(MainPage_Loaded);

                this.MouseMove += new MouseEventHandler(MainPage_MouseMove);

                InitializeComponent();

            }

     

            void MainPage_MouseMove(object sender, MouseEventArgs e)

            {

                Point position = e.GetPosition(LayoutRoot);

                double leftOffset = CustomCursorImage.ActualWidth / 2;

                double topOffset = CustomCursorImage.ActualHeight / 2;

                double newLeft = position.X - leftOffset;

                double newTop = position.Y - topOffset;

                Canvas.SetLeft(CustomCursorImage, newLeft);

                Canvas.SetTop(CustomCursorImage, newTop);

            }

     

    The last part. And I find this a little ‘magical’ is that when the background was white the mouse kept “losing” the new cursor. So I changed the background to classic ‘directx’ cornflowerblue and the problem went away (I don’t know why and believe me I don’t usually fall for the ‘it must be magic’ comment, but apparently it works?):

     

    <UserControl x:Class="MouseCursorReplacement.MainPage"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

        mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">

        <Canvas x:Name="LayoutRoot" Background="CornflowerBlue">

            <Image x:Name="CustomCursorImage" Source="cross.png" Canvas.ZIndex="100"/>

        </Canvas>

    </UserControl>

     

     

    Full story

    Comments (0)

  • Some TDD in the real world

    One of the most common things that I run into when doing TDD with NUnit in the 'real world' is that the ugly truth is dependencies are either hard or impossible to test.

     

    Take for example this code snippet where we're calling a webservice a couple times to do some work.

        public class HumanResources

     

        {

            public void FireThisGuy(long employeeId)

            {

                HumanResourcesService service = new HumanResourcesService();

                GetEmployeeRequest request = new GetEmployeeRequest();

                request.EmployeeId=employeeId;

                Employee emp = service.GetEmployee(request).Employee;

                emp.TerminationDate = DateTime.Now;

     

                SetEmployeeRequest setRequest = new SetEmployeeRequest();

                setRequest.Employee = emp;

                service.SetEmployee(setRequest);

            }

        }

     

     

     

     

     

    We know it's a bad practice to call webservices in our unit tests for a few reasons. (namely it's slow). So let's refactor a little and remove anything related to the webservices out. I extract method on the get...

           public void FireThisGuy(long employeeId)

            {

                Employee emp = GetEmployeeById(employeeId);

                emp.TerminationDate = DateTime.Now;

     

                HumanResourcesService service = new HumanResourcesService();

                SetEmployeeRequest setRequest = new SetEmployeeRequest();

                setRequest.Employee = emp;

                service.SetEmployee(setRequest);

            }

     

            protected Employee GetEmployeeById(long employeeId)

            {

                Employee emp;

                HumanResourcesService service = new HumanResourcesService();

                GetEmployeeRequest request = new GetEmployeeRequest();

                request.EmployeeId=employeeId;

                emp = service.GetEmployee(request).Employee;

                return emp;

            }

     

    notice this wasn't a straight up 'extract to method' on your refactoring tool. I did about 5 small refactorings before i did the extract but just notice the complete extraction of the dependency on the webservice to a new method "GetEmployeeById".

     

    now the same for the set:

     

           public void FireThisGuy(long employeeId)

            {

                Employee emp = GetEmployeeById(employeeId);

                emp.TerminationDate = DateTime.Now;

                SetEmployee(emp);

            }

     

            protected void SetEmployee(Employee emp)

            {

                HumanResourcesService service = new HumanResourcesService();

                SetEmployeeRequest setRequest = new SetEmployeeRequest();

                setRequest.Employee = emp;

                service.SetEmployee(setRequest);

            }

     

            protected Employee GetEmployeeById(long employeeId)

            {

                Employee emp;

                HumanResourcesService service = new HumanResourcesService();

                GetEmployeeRequest request = new GetEmployeeRequest();

                request.EmployeeId=employeeId;

                emp = service.GetEmployee(request).Employee;

                return emp;

            }

     

    now for my unit test the only thing i really want to test is the "FireThisGuy" logic. so first i would need to inherit from HumanResources and make a testing class that overrides the 2 dependencies i do not want.

        public class HumanResources

        {

            public void FireThisGuy(long employeeId)

            {

                Employee emp = GetEmployeeById(employeeId);

                emp.TerminationDate = DateTime.Now;

                SetEmployee(emp);

            }

     

            protected virtual void SetEmployee(Employee emp)

            {

                HumanResourcesService service = new HumanResourcesService();

                SetEmployeeRequest setRequest = new SetEmployeeRequest();

                setRequest.Employee = emp;

                service.SetEmployee(setRequest);

            }

     

            protected virtual Employee GetEmployeeById(long employeeId)

            {

                Employee emp;

                HumanResourcesService service = new HumanResourcesService();

                GetEmployeeRequest request = new GetEmployeeRequest();

                request.EmployeeId=employeeId;

                emp = service.GetEmployee(request).Employee;

                return emp;

            }

        }

        public class TestingHumanResources : HumanResources

        {

            public Employee GetEmployeeByIdTestingEmployee { get; set; }

            protected override Employee GetEmployeeById(long employeeId)

            {

                return GetEmployeeByIdTestingEmployee;

            }

     

            public Employee SetTestingEmployee { get; set; }

            protected override void SetEmployee(Employee emp)

            {

                SetTestingEmployee = emp;

            }

        }

     

     

     

     

     

     

     

     

     

    Now i can actually write a test that validates

      public class HumanResources

        {

            public void FireThisGuy(long employeeId)

            {

                Employee emp = GetEmployeeById(employeeId);

                emp.TerminationDate = DateTime.Now;

                SetEmployee(emp);

            }

     

            protected virtual void SetEmployee(Employee emp)

            {

                HumanResourcesService service = new HumanResourcesService();

                SetEmployeeRequest setRequest = new SetEmployeeRequest();

                setRequest.Employee = emp;

                service.SetEmployee(setRequest);

            }

     

            protected virtual Employee GetEmployeeById(long employeeId)

            {

                Employee emp;

                HumanResourcesService service = new HumanResourcesService();

                GetEmployeeRequest request = new GetEmployeeRequest();

                request.EmployeeId=employeeId;

                emp = service.GetEmployee(request).Employee;

                return emp;

            }

        }

        public class TestingHumanResources : HumanResources

        {

            public Employee GetEmployeeByIdTestingEmployee { get; set; }

            protected override Employee GetEmployeeById(long employeeId)

            {

                return GetEmployeeByIdTestingEmployee;

            }

     

            public Employee SetTestingEmployee { get; set; }

            protected override void SetEmployee(Employee emp)

            {

                SetTestingEmployee = emp;

            }

        }

        [TestFixture]

        public class HumanResourcesTests

        {

            [Test]

            public void FireThisGuy_UserIdIn_FiredEmployeeSaved()

            {

                TestingHumanResources hr = new TestingHumanResources();

                hr.GetEmployeeByIdTestingEmployee = new Employee();

                hr.GetEmployeeByIdTestingEmployee.TerminationDate = null;

     

                hr.FireThisGuy(200);

                Assert.IsNotNull(hr.SetTestingEmployee.TerminationDate);

     

            }

        }

     

     

     

     

     

     

     

     

     

     

     

     

     

    This is why im a big fan of object oriented programming and using inheritance to break dependencies. It's a real fast solution to some of the unique dependency issues i fight every day and it is a lot easier to read and debug than tons of interfaces.  This and other examples of seaming and seperation can be found in Michael Feather's book "Working Effectively with Legacy Code"

     

     

     

     

    Grab the code here

    Full story

    Comments (10)

  • Using Silverlight initParams with ASP.NET MVC

    I looked around and couldn't really find a clean way to add init params to the view of my mvc silverlight host aspx file. Here's what i ended up doing... just wanted to blog it so that i didn't forget it :)


    <asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server">
    <script src="../../Silverlight.js" type="text/javascript"></script>
        <object height="100%" width="100%" data="data:application/x-silverlight-2," type="application/x-silverlight-2" >
     <param name="source" value="../ClientBin/PalladiumEngine.xap"/>
     <param name="onError" value="onSilverlightError" />
     <param name="background" value="white" />
     <param name="EnableGPUAcceleration" value="true" />
     <%=ViewData["InitParams"] %>
     <param name="minRuntimeVersion" value="3.0.40624.0" />
     <param name="autoUpgrade" value="true" />
     <a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40624.0" style="text-decoration:none">
       <img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style:none"/>
     </a>
       </object><iframe id="_sl_historyFrame" style="visibility:hidden;height:0px;width:0px;border:0px"></iframe>
    </asp:Content>


    then my Controller action code looked like this:

      public ActionResult Index()
            {
                string port = WebConfigurationManager.AppSettings["PORT"];
                if(string.IsNullOrEmpty(port))
                    throw new ApplicationException("Put a PORT value into the web.config please!");
                ViewData["InitParams"] = string.Format("<param name=\"initParams\" value=\"Port={0}\" />",port);
                ActionResult retval = View();
                return View();
            }

    then my silverlight code using it looked like this:
         private void Application_Startup(object sender, StartupEventArgs e)
            {
                int port = int.Parse(e.InitParams["Port"]);
                Client = new MessageClient<SocketMessage>(port, Serializer);
                Client.ConnectCompleted += OnConnected;
                Client.MessageRecieved += new EventHandler<MessageRecievedEventArgs<SocketMessage>>(client_MessageRecieved);
                Client.ConnectAsync();

                RootVisual = MainPage;
                MainPage.LayoutRoot.Children.Clear();
                MainPage.LayoutRoot.Children.Add(LoginPage);
            }


    I was using it to pass the port into silverlight from my web.config. For security reasons be sure you don't do this with user specified content!

    Full story

    Comments (1)

  • Sockets Light rules for silverlight sockets

    http://socketslight.codeplex.com/

    If you're trying to do server push to silverlight clients this is the scalable and flexible way to go. The polling http sucks compared to this.

     

    I need not say more.

    Full story

    Comments (0)

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. Next page