Category: Uncategorized

  • Passed the certification exam

    Passed the certification exam

    I just passed the Mobile Developer Certification Exam – but just barely with 80% correct answers. The exam contained a lot of questions about compilation, garbage collection etc. as well as some more general questions regarding software development.

    Xamarin has chosen ExamBuilder as the software provider for the exams. I had a lot of problems with this. During the first half hour of the 3 hr. examination, I lost connection with exambuilder at least 10 times – where I had to log out and log in again. This was quite annoying even though it got better as the exam progressed. I hope I was just unlucky.

  • Onion architecture with Xamarin and PCL

    Onion architecture with Xamarin and PCL

    Over the short history of Xamarin, there has been a bunch of different ways to share code between platforms, when using Xamarin to create IOS, Android and Windows Phone applications. At first, there were only simple ways to share code, like file linking where the developer had to drag and drop files from one code project to another. If the shared code needed to do anything platform specific, the code would need to be filled with #IFDEF statements to target each platform.

    This approach is problematic: If the code that needs sharing is spread over multiple files, all the files has to be linked – and most projects would have a high number of files all needed to be linked. Another problem is the IFDEF statements. It is basically a violation of Separation Of Concerns, when a class can handle multiple platforms. In stead the code should be separated in different classes and the dependency framework should inject the proper code for each specific platform

    Portable Class Libraries

    Near the end of 2013, Xamarin announced support for Portable class libraries in cooperation with Microsoft (http://blog.xamarin.com/pcl-projects-and-vs2013/). The Portable Class library (PCL) makes it possible to share an entire assembly using the same project referencing techniques known from other platforms like web and windows projects. Just create a PCL assembly, add your shared code, add unit tests etc, and then reference the project from your main application or another PCL library.

    When creating a PCL project, you have to choose a profile for the PCL. The profile basically describes which platforms the PCL should target. The more platforms you choose, the more restrictions you will have on the .net framework features available. If you for instance choose to target Windows Phone, the PCL will not be able to use HttpClient directly.

    Onion Architecture

    Onion Architecture was originally described by Jeffrey Palermo back in 2008 (http://jeffreypalermo.com/blog/the-onion-architecture-part-1/) and it has a history going even further back. Its a widely recognized architecture when using domain driven development and IOC.

    The architecture describes a core which only contains a domain model. Around the domain model, there is a bunch of domain services, who knows the core. But the core has no knowledge about the domain services. So all references goes from the outside to the inside. The next layer is application services and UI / infrastructure. The UI is kind of self explaining, but the infrastructure needs a few words.

    Lets say the application needs to read information from a file. In stead of placing this functionality directly in the domain services, the service will provide an interface and code against this interface. The actual implementation of the file writing functionality is placed in the infrastructure layer and injected into the domain service layer through the use of IOC.

    Given we want to retire early, I want to create a competitor to Instagram and sell it to Twitter for an insanely large figure. But I will start simple though. My app will only be able to capture an image and save it to disk.

    My domain model (core) would look similar to this.

    namespace MyApp.Core.DomainModel
    {
      public class Image
      {
        public Image (byte[] imageData)
        {
          ImageData = imageData;
        }
     
        public byte[] ImageData 
        {
          get;
          private set;
        }
      }
    }

    The domain model is simple and reflects my only domain which is the image.

    I need a domain service to actually capture images and store them somewhere. But as this is platformspecific and uses infrastructure that is different on each platform, I choose to implement my domain service as an interface.

    namespace MyApp.Core.DomainServices
    {
    public interface ICameraService
      {
        MyApp.Core.DomainModel.Image CaptureImage();
        void SaveImage(MyApp.Core.DomainModel.Image  image);
      }
    }

    Another important benefit is, that my app, so far hasn’t decided weather the image should be saved as a file or posted to an online account. All this depends on the implementation of the interface.

    On top of this simple domain model and service, I will provide my most outer ring of the onion. Off course I provide a UI for IOS and another for Androind. But I will also provide an implementation of the ICameraService, that will actually capture images on each platform.

    The result is that all code that can be shared, is shared and platform specific code i separated between the platforms.

    I will end up with a solution looking like this (ooops… forgot my unit-test projects)

     

    Why is Onion Architecure such a great fit with Xamarin?

    Xamarin gives the advantage, compared with other x-platform tools, that you can share native code between platforms and only provide platform specific code for the parts of the code that differ for each platform. This model fits perfectly with the onion architecture, where all code in the inner circles of the onion is general and can be shared between platforms. The code in this part of the onion should never contain platform specific code and even the most restrictive PCL profile should be useful for this.

    There is a bunch of solution templates available for making Xamarin based apps using the onion architecture. Take a look at these links and try it out.

    https://github.com/Kizmar/xDriven

    https://github.com/OSTUSA/xdriven

    _

  • Studyguide for Xamarin Mobile Developer exam

    Studyguide for Xamarin Mobile Developer exam

    I have collected the links to additional information provided in each of the session videos. If any links are missing, please add them in the comments. The link collection can be used as a studyguide for Xamarin Mobile Developer Exam.

    XAM101 Intro to Mobile / Kickstart
    http://docs.xamarin.com/guides/cross-platform/application_fundamentals/building_cross_platform_applications/

    AND101 Intro to Android with Xamarin Studio
    http://docs.xamarin.com/guides/android/getting_started/

    AND102 Intro to Android with Visual Studio
    http://docs.xamarin.com/guides/android/getting_started/

    IOS101 Intro to IOS with Xamarin Studio
    http://docs.xamarin.com/guides/ios/getting_started/

    IOS102 Intro to IOS with Visual Studio
    http://docs.xamarin.com/guides/ios/getting_started/

    AND101 List Views and Adapters in Android
    http://docs.xamarin.com/guides/android/user_interface/working_with_listviews_and_adapters/

    IOS110 Tables and Collection Views in iOS
    http://docs.xamarin.com/guides/ios/user_interface/introduction_to_storyboards/
    http://docs.xamarin.com/guides/ios/user_interface/tables/
    http://docs.xamarin.com/guides/ios/user_interface/introduction_to_collection_views/

    XAM140 Introduction to Cross-Platform Mobile Development
    http://docs.xamarin.com/guides/cross-platform/getting_started/introduction_to_mobile_development/
    http://docs.xamarin.com/guides/cross-platform/application_fundamentals/pcl/introduction_to_portable_class_libraries/

    XAM150 Introduction to Cross-Platform Web services
    http://docs.xamarin.com/guides/cross-platform/application_fundamentals/web_services/

    XAM160 Data in mobile
    http://docs.xamarin.com/guides/ios/application_fundamentals/working_with_the_file_system/
    http://docs.xamarin.com/guides/cross-platform/application_fundamentals/data/

    XAM205 Mobile Navigation Patterns
    http://docs.xamarin.com/guides/android/platform_features/fragments/
    http://developer.android.com/guide/practices/screens_support.htmlhttp://docs.xamarin.com/guides/ios/user_interface/creating_tabbed_applications/

    XAM210 Backgrounding
    http://docs.xamarin.com/guides/cross-platform/application_fundamentals/backgrounding/

    XAM220 Publishing an App
    http://docs.xamarin.com/guides/android/deployment,_testing,_and_metrics/publishing_an_application/
    http://docs.xamarin.com/guides/ios/deployment,_testing,_and_metrics/app_distribution_overview/

    AND230 Maps and location in Android
    http://docs.xamarin.com/guides/android/platform_features/maps_and_location/

    IOS230 Maps and location in IOS
    http://docs.xamarin.com/guides/ios/platform_features/ios_maps/

  • Review of Xamarin 140: Intro to Cross-Platform Mobile Development

    Review of Xamarin 140: Intro to Cross-Platform Mobile Development

    Im just about to join a session on Xamarin University, XAM 140: Intro to Cross-Platform Mobile Development. I spend last night reading through the course material made available by Xamarin University. And basically this course is about two things:

    1) Learning how to reference a shared assembly

    2) Learning how to link to a shared file

    My impression is that attendees at Xamarin University already know how to do regular .net development.

    Hence is referencing a shared assembly very very basic. And sharing functionality by linking files is probably possible (I guess its also possible using Visual Studio) but it is definitely not a good way to share common code between projects.

    I really hope that the course will bring me more than mentioned above. Otherwise it will be a waste of time.

    Most amazing is, that this course is mandatory (1 of 3 courses) to take the Xamarin Certified Mobile Developer exam.

    Follow up: Im disappointed. The class did not go through anything but referencing assemblies and using a “worst” practice of sharing files. At least the tutor told that this isn’t an optimal way of sharing stuff.

    I hope to see a class of real cross platform mobile architecture. I have a clue that the use of Onion Architecture will be optimal for Cross Platform Mobile Dev.

  • Strongly typed databinding in WinForms – aka. no more magic strings!

    I’ve been working on a WinForms project for a while and I’m getting progressively more tired of the magic strings used when doing databinding. (See MSDN documentation here) The problem is that the datamember of the datasource being bound to the control is a plain old System.String:

    e.g:

    lblName.DataBindings.Add("Text", myViewEntity, "UserName");

    I can live with the propertyName (“Text”) being a System.String, as this property will probably not change the name in the next many releases of .net, but the “DataMember” is nagging me.

    The problem is:
    * Misspelling is only discovered at run time
    * Renaming the property will cause the databinding to break – at runtime.

    The last point is mostly handled by using productivity tools such as ReSharper, but I have often had the experience that the ReSharper refactoring didn’t rename correctly – probably because I was blindly pressing yes or no when asked to refactor.

    A few weeks ago, I started to look around for a better solution and came up with this little snippet:

    public static string GetPropertyName(Expression expression)
    {
        var body = (MemberExpression)expression.Body;
        return body.Member.Name;
    }
    
    
    This little snippet could be used like this:
    
    
    lblName.DataBindings.Add("Text", myViewEntity, GetPropertyName(() => myViewEntity.UserName));

     

    - And the magic string has disappeared :-)
    After a little more work, I ended up making an extension method for the BindingCollection class:
    public static Binding Add(this ControlBindingsCollection collection, string propertyname, object dataSolurce, Expression expression, bool formattingEnabled = true, DataSourceUpdateMode updateMode = DataSourceUpdateMode.OnPropertyChanged, object nullValue = null, string formatString = "", IFormatProvider formatProvider = null)        
    {
        return collection.Add(propertyname, dataSolurce, GetPropertyName(expression), formattingEnabled, updateMode, nullValue, formatString, formatProvider); 
    }

    Using this method, I could write the following code when binding:

    lblName.DataBindings.Add("Text", myViewEntity, () => myViewEntity.UserName);
    
    
    Now we're talking!
    
    

     

    Finally I wrote a whole extension library containing the most common bindings - for binding to a Text, Enabled, Checked etc. causing my code to look like this:
    lblName.BindToText(myViewEntity, () => myViewEntity.UserName);
    
    
    Check out the library in the attached .cs file.
  • Testing private members with PrivateObject and PrivateType

    I’m currently working on a legacy project that has been ported from Visual Studio 2010 to Visual Studio 2012. The project uses the Visual Studio 2010 feature that allowed to access private members through some automatically generated code.

    In Visual Studio 2012, this causes a warning when compiling:

    This task to create private accessor is deprecated and will be removed in a future version of visual studio

    I’ve been looking around to find a solution. The right solution would obviously be to refactor the code in a way that testing private methods is not necessary, but that is not (currently) an option in the solution I’m working on. In stead, I’m relying on two new classes in the .net framework : PrivateObject and PrivateType.
    The PrivateObject can be used to access private methods, properties, fields where the PrivateType class can be used to access static members.

    I have made a simple test project to show the features. The adder.cs class contains a public method as well as some private fields and private static methods. Don’t mind that the code doesn’t make sense as it is only for demo purposes.

    using System.Collections.Generic;
    using System.Linq;
    
    namespace PODemo
    {
        public class Adder
        {
            private int a;
            private int b;
            private int result;
    
            public void Add(int one, int two)
            {
                a = one;
                b = two;
                result = AddNumbers(one, two);
            }
    
            private static int AddNumbers(int one, int two)
            {
                return one + two;
            }
    
            private static int AddNumbers(IEnumerable numbers)
            {
                return numbers.Sum(p => p.Value);
            }
        }
    
        public class Number
        {
            public int Value { get; set; }
        }
    }

    The Add method doesn’t return the result. Suppose I would like to test the functionality by verifying the result field:

            [TestMethod]
            public void PrivateFieldsTest()
            {
                var test = new Adder();
                var po = new PrivateObject(test);
                test.Add(2, 4);
    
                Assert.AreEqual(2, po.GetField("a"));
                Assert.AreEqual(4, po.GetField("b"));
                Assert.AreEqual(6, po.GetField("result"));
            }

    I create a new Adder object and then creates a PrivateObject that wraps the test object. The I can use the GetField methods to access private fields. Also, GetProperty and Invoke methods are available.

    Suppose I would like to test the static methods, I can use the PrivateType object instead.:

            [TestMethod]
            public void PrivateStaticMemberTest()
            {
                var pt = new PrivateType(typeof (Adder));
                var result = (int) pt.InvokeStatic("AddNumbers", BindingFlags.NonPublic | BindingFlags.Static, 2, 4);
    
                Assert.AreEqual(6, result);
            }

    A little aberdabai is that invoking members that take an interface as argument, must be casted to the interfacetype – otherwise the PrivateType cannot identify the correct method to execute.

            [TestMethod]
            public void PrivateStaticMemberWithInterfaceParameterTestSuccess()
            {
                var pt = new PrivateType(typeof(Adder));
    
                IEnumerable param = new[] {new Number {Value = 2}, new Number {Value = 4}};
                var result = (int)pt.InvokeStatic("AddNumbers", BindingFlags.NonPublic | BindingFlags.Static, param);
                
                Assert.AreEqual(6, result);
            }

    Obviously, these classes just wraps some reflection magic underneath the covers, but they still come ind handy when testing legacy code. I would never advise to use these classes when doing green-field development. I strongly believe that it is a code-smell if it’s necessary to use these objects for testing new code.

    MSDN; PrivateType
    MSDN: PrivateObject

  • Why are my clients always wasting their money on me?

    Update 2011/08/31: I’ve had a lot of response from other freelancers with similar experiences. They suggested a few extra issues which have been added below. Thanks.
    —————————–

    As a freelancer / contractor, I startup on new projects or new clients every 6 months or so. I usually get the job through an agent after an interview with the client. During the interview, i try to figure out what the project is about and how the project team is organized, but often the interview is half an hour or less which can make it hard to get all the details nailed down.
    Before i actually start, i try to prepare as much as possible, by googling the company, looking at past projects if any is avaliable etc.
    But recently i concluded that the preparation is mostly waste… Because my clients doesn’t make the same effort to prepare for my arrival. I usually meet up to find that a development environment is missing, the team don’t know where the latest version of requirements is located etc. I usually spend the first week either waiting for people to get me ready or to find the people who knows the procedures of the company. If only the clients preparred, i could be productive much faster => make myself worth the money they pay much faster.

    I’ve made a list of things that should be ready for the contractor (or any other new people on a project – either new employees or existing employess who is transferred to a project.

    • Pc. The hardware must be ready. If it takes time to be preparred by internal it, order the machine to be ready a week before the contractor arrives and test it before. That gives time to fix any issues.
    • Table and chair. Dont start the first day with rearranging people to make space or clear a corner of one of the other consultant desks. A consultant is no different from other employees. Productivity increases in a clean and tidy environment. And make sure the desk is close to the rest of the development team.
    • Make sure the user account is up and running. Both normal user accounts with e-mail access etc, and special dev accounts if needed.
    • Make sure remote access is configured and any security tokens are ready
    • Have a physical access card ready or make an appointment with a photographer if a photo is required on the access card.
    • Make a list of people related to the project. Ensure name, email and phone number. If pictures are available, include it. There is a lot of new faces to remember on any new project. Even better: send the list by e-mail with vcf cards attached. That makes it easy to transfer the contacts to my personal phone, which i will usually use.
    • Make sure that every team member has shared his / her calendar.
    • Have a manual for time registration ready.
    • E-mail with links to
      • Requirements
      • Architecture and design documentation
      • Development standards
      • Decision logs
    • Make sure a few workpackages are ready and assigned and make sure an existing team member is assigned to do pair development the first few days. The pair development makes it much faster to grasp the idea of the design and the toughts behind the code. Its also a good way to make some social engineering with the existing team – which is a huge benefit for both client and contractor.

    All items above must be fixed sooner or later. A late fix is an expensive fix. If the fix is made early, it can be planned and carried out by a way person way cheaper than a consultant , e.g. a project secretary.

    So please prepare and let me do what you pay me to do.

  • Developer pc

    Its been two years since my last blog post. I havent died, but even though im still breathing, its been a coupple of busy years. Another kid, a new house etc have taken all my time. But things are looking brighter and im starting to get more tech focused again.

    Thia blog post is about my thoughts on getting some new hw. I think i already decided, but i need some arguments to make sure that the decission i mad with my heart also is the right one for my needs.

    For the last coupple of years, i’ve been running on mo or less the same hardware. I got a Dell studio XPS 13 a fw year ago. It was equipped with a core 2 dual processor – state of the art at the moment, a decent graphics adapter 4 gigs of mem and 500 gigs of harddrive. It has been upgraded to 8 gb ram to support multiple vm machines and in two steps upggraded, first to a 256 ocz ssd drive and recently to a 600 gb Intel Ssd drive. I even upgraded the battery making the machine run for 5 hrs on a charge. But now it is dying… Its freshly reinstalled and still freezes quite often. It even complains during bios load once in a while. So now its time to look for something new.

    My needs is a state of the art machine for running windows and multiple dev machines on vm ware. I do lots of development in visual studio.

    Im quite into apple gear. I just got my ipad 2 and im loving it. I, of course, have an iPhone and all my digital assets (pics and music) is storred on a mac mini. So my first thought is to get a mac book pro and run windows on it, either natively or using parallels. But wow it is expensive. And it has a few caveats. First, i like to get a 13 inch machine as i do a lot of travelling. But i cant get the 13 inch with a i7 quad core processor, which i would very much like since im doing all my develpment using vm ware machines. The 15″ machine would do it, but it is still to bulky and its very expensive.

    My criterias are as follows
    Intel core i7 quad processor
    8 gb of ram and i will not complain if its possible to upgrade to more.
    Hd isn’t important as i will move the new 600 gb ssd disk into the new machine anyway.
    I prefer a 13 inch because i do lots of commuting (currently 3 hrs pr. day), but a slim 15 inch will do it as well.
    I need a a high resolution on the screen as well. My isight is brilliant and i dont mind if my fonts are small as long as there is lots of pixels to use.
    I dont care about dvd drive. I dont use it at all.

    Ive been looking around using google, and its really hard to find any matches. I’ve been Looking at Lenovo machines, but it looks like a german tank. The Sony Vaio might be a match but have no idea of the build quality. Samsung series 9 is good looking but i cant change the hdd to my ssd.

    So what should i do. Pay dkk 20.000 + for an Apple 15 inch of half the price for a new Dell XPS 15 which meets all my requirements except it seems a little bulky? I really dont mind paying the price as this is a tool that is going to be used every day for the next years. So 10k or 20k… Its down to dkk 27 pr day for the most expensive machine… And thats like the first two minutes of payed work every day…shouldn’t i just accepting that i work for two minutes to have the tool i really want?

    If i were a carpenter, i wouldn’t buy the cheapest nails that bend every time the hammer fall.

    Note. This blog post is written on my ipad… I excuse all the typos.

  • TechEd day 4

    TechEd day 4

    Thomas Garp in the HUUGGGEEEE eating all at TechEd

    Day 4 on TechEd is going to be spend on the following program:

    • Test driven development
    • Custom features that enabled users to develop rate and share content with moss 2007. –
    • No budget for tools – hot tools on a limited budget
      MS silverlight on sharepoint architectural and development patterns
    • Design patterns in the real world
    • .net on linux and mac

    I dont know if I will be able to take part in all the sessions… I hope to be able to escape for some serious shopping for a coupple of hours as well. And tonight is the Jam Session party, and I should be ready for that as well…

  • TechEd Day 3 followup

    Day 3 went off with the Zen of Architechture by Juval Lowy… se other post.

    After that breathtaking session, I went for a certification session. The concept is basically that a guy spends 1 hr telling you how to pass a certification test. What is necessary to know, what isn’t. I attended a session covering the MS SharePoint 2007 configuration exam. Im pretty confident that I could pass it after reading just a little.

    After lunch, I attended the Applied Entity Framework. But I’m afraid that jetlag and 4 days with only very little sleep kicked in in that session, so I didn’t get the benefits I was hoping for. It provided a bit of knowledge in an area that i havent covered in detail before. I think I will try it out, but oher people tell me that I should go for the NHibernate – (When will somebody call it nHyperNate??? It seems to be the new “HYPE” that everything is Hyper 🙂 )

    I realized that I wasn’t able to listnen to more people that day, so I went into solo nerding mode and tried some of the stuff out that i have heard over the last few days. Pretty nice just being me and my friend Visual Studio.

    The day ended in Long Beach with swimming (Not me… I was a woosy) and more mexican food.