Friday, April 13, 2012

HOWTO: Fix “A referral was returned from the server” error

Required Operating System: Windows Vista/7.

One of the reasons for this error to occur is due to UAC (User Account Control) and the Local Security Policy settings.

Here are the steps to fix it.

  1. Go to the Control Panel >> System and Security >> Administrative Tools >> Local Security Policy.
  2. In the Local Security Policy browse to Local Policies >> Security Options
  3. Make sure that the following policy is disabled “User Account Control: Only elevate executables that are signed and validated.

Monday, April 9, 2012

HOWTO: Restore the default user junctions and permissions

Required Operating System: Windows Vista/7.

Changing the default profile location on Windows can be quite tedious especially when you aren’t careful and messing up the default user junctions and permissions either because you accidentally deleted them or made a copy of the directory without knowing that the junctions will not be reserved; it can be quite painful to restore them manually so I’ve written a batch file to do the work.

Download: Restore Junctions

e.g. restore-user-default-junctions “D:\Users\<Some User>”

Thursday, January 12, 2012

Entity Framework 4 Data Generator: FAQ

Table of Content

Question: I can't see some of the files that were generated what can I do ?

Select the "Show All Files" option from the solution explorer and check whether these files were generated and add them manually.

Question: I get the following error "Value does not fall within the expected range." when I'm trying to add the generator to the model, why ?

If you already added/used the generator once in the past make sure that the following directories are deleted '\Core', '\Entities', '\Repositories' and '\T4', , Select the "Show All Files" option from the solution explorer and delete them manually.

Entity Framework 4 Data Generator: Getting Started

    Table of Content

    If anything went wrong and something is not working for you, you are more than welcome to post it at CodePlex.

    Prerequisites

    Creating The Database

    1. Open the Sql Server Management Studio
    2. Create a new database and name it Demo.

      newdatabase
    3. Download the demo project from CodePlex.
    4. Extract the zip file and and look for the demo.sql file.
    5. Right click on the database name and click on “New Query”.

      newquery
    6. Edit the demo.sql file and copy its content to the query editor.
    7. Right click on the editor and click on “Execute”.
    8. Go back to the database name and click on Refresh.

    Using The EF4 Data Generator Extension

    1. Download and install the extension from the Visual Studio Gallery.
    2. After the extension is installed you should see it in the Visual Studio Extension Manager.

      extensionmanager
    3. Inside the zip file there are two folders, the Application.Data and the Application.Demo, go to the Application.Demo and open the solution file “EF4DataGenerator.Demo.sln”.

      solution
    4. Open the Model.edmx file and right click on an empty space and click on the Add Code Generation Item.

      addcodegenerationitem
    5. Select the ADO.NET EF4.0 Data Generator and change the name of the file to Model.tt or anything you like. 

      addnewitemdialog
    6. All the necessary classes and templates should populate automatically now.

      generatedfiles
    7. Done! ;)

    Getting To Know The Provided Classes And Their Responsibilities

    Core Classes

    The following core classes are not generated by the T4 templates, they are predefined classes I wrote to extend the current functionality, so you can actually delete them if you don’t want to use them, although to do that you will need to make some changes to the T4 so it’s better to leave them.

    *.Core.ObjectContextExtensions – Provides additional functionality to the ObjectContext.

      • CreateQuery<T>() – Creates a query based on the provided type.

    *.Core.QueryableExtensions – Provides additional functionality to IQueryable.

    • Page – Takes a page index and a size to perform pagination.
    • SelectScalar – Takes an integer value and a property name to get a single entity.
    • OrderBy – Takes a property name and direction to get a sorted results.

    Entities and Repositories

    The entities and the repositories are generated by the T4 templates, the names of the classes, properties and pretty much everything is taken from the entity data model (EDM).

    As you can see each generated file has this suffix “*.part”  as part of the name, it stands for partial, I chose to add it for two reasons.

    1. So I can tell tools like ReSharper to ignore these files for its analysis.
    2. So it will be easier to identify and distinguish the generated classes and these that are not generated.

    Context, Context, Context!

    The DataContextBase as pointed in the comments “Provides an access point to the data layer.”  is relatively simple class which is generated by the T4 templates and allows you to access the data layer repositories.

    The DataContext class is derived from the DataContextBase and allows you to alter existing functionality as well as adding functionality and is the class that you should use to get access to the repositories outside to the data layer.

    By design you can’t derive from the DataContextBase outside to the data layer or derive from the DataContext at all, in my opinion like the general opinion the application shouldn’t have any persistent knowledge outside to the data layer.

    The Demo Project

    The demo project contains a simple example to show you how to access and work with the repositories.

    Entity Framework 4 Data Generator: T4

      Table of Content

      What’s T4 ?

      T4 stands for Text Template Transformation Toolkit, it’s available since Visual Studio 2005 and is a template based code-generation engine.

      It allows you to use a DSL and write templates that transforms into proper code that otherwise you had to write yourself.

      You write the template once and you never have to do it again.

      Why should we use it ?

      You don’t! but it can make your work a lot easier, most of the time we write the same code over and over again and duplicate it from project to project for no reason at all.

      The same repetitive code is written in every project, the only difference is that we customise it to the project requirements, we can let the the code generation handle the repetitive pieces of code and still make modifications either to the T4 itself or the generated classes.

      If you may use it wisely and for the right things and purposes you may experience major improvements in code maintainability as well as productivity.

      What’s the T4 Toolbox ?

      The T4 Toolbox is a library developed by Oleg Sych, I use it because it helps to create a more manageable and reusable templates.

      Entity Framework 4 Data Generator

      Table of Content

      Awhile back I’ve published my EF4.0 Data Generator and I thought like posting about the philosophy behind some of the decisions I took there and provide some more documentation to make it more useful for developers that want to use it in their work.

      There are few generators that are doing almost the same thing, why another one ?

      There are few reasons to that.

      • The ADO.NET C# POCO Entity Generator provided by Microsoft produces unreadable code so I modified it to generate a more readable output.
      • I tried ADO.NET Unit Testable Repository Generator and I didn’t really like it, while testability is great I have my way of doing things and with all due respect I find it silly (explanation below), I just wasn’t satisfied with existing solutions.
      • I needed more control over the templates and both of these generators were written in such a way that it’s almost impossible to modify as they are barely readable.
      • I wanted to study the materials into depth and so I had to build everything from the ground up, practice it and write it myself to meet my own expectations and choices.

      Abstractions and Unit Testable Repositories

      Anything on the data layer that execute queries should be tested against a real database system through integration tests and integration tests only, why ?

      Ask yourself why are you doing unit tests at all ? to test that the code in question is working as expected, right ?

      So you mock the ObjectContext to make the repositories testable and you’re in the assumption that just because the code works and you got yourself a green bar the code will work in production ? you’re wrong.

      Your mocked version just used a different kind of LINQ provider under the hood and it may not work as you expect it to work, in the worst case scenario you will have unnoticed bugs or better yet an exception, if you’re lucky.

      Different LINQ providers have different functionality and features, even if everything was supported, the abstraction is mainly for testability and has no real value to the application so in my opinion it’s not worth the time and effort.

      That’s exactly the reason I chose to use ObjectContext directly in my repositories.

      If you want to support multiple database systems or a different kind of ORM you can just move the interfaces of the repositories into another assembly and create a dedicated assembly for each database or ORM that you want the application to support.

      Tuesday, August 23, 2011

      Firefox Add-ons That Really Makes A Difference!

      I’ve been really busy the past few months and I couldn’t really do anything besides work, hopefully I’ll have spare time to write and work on my blog. :)

      In the upcoming months I’m going to review and write about tools I’ll start with FireFox add-ons and move on to a series of posts that focus on general computer tools, system and development.

      Download Manager Tweak
      Adds quite a bit of functionality and enhances the standard Download Manager that comes with FireFox.

      downloadmanager_thumb4

      Easy YouTube Video Downloader
      Most of the videos I bother to watch in YouTube are educational videos and sometimes I feel like downloading the video and save a copy either because it’s good or is a resource that I might need in the future (can’t remember everything, you know… :)).

      This tool is beyond excellent in my opinion and if you’re like me you will like it.

      easyyoutubevideodownloader_thumb1

      Googlebar Lite
      Do you like your google bar ? you don’t like all its bloat and the noises it makes ? then you should definitely think about replacing it with this extension, it’s very small and quite amazing!

      googlebarlite_thumb1

      Status-4-Ever
      Unfortunately  FireFox 4.x+ doesn’t have a statusbar, it was removed and replaced with a new toy made by Mozilla called the addon bar, I don’t like it and fortunately there’s an addon that does justice to this not so useful innovation.

      status4ever_thumb4

      Tab Utilities Lite
      Giving you full control over your FireFox tabs, the reason I picked it up initially was mainly due to the color customisation feature the rest is a bonus. :)

      tabutilitieslite_thumb1

      VTZilla  (VirusTotal)
      Did you ever wish you could perform a virus scan before downloading the file to your computer ? Well, this add-in scans your files online and with the best AV engines available.

      FireFox Download Dialog.

      VTzilla_thumb3

      VirusTotal Web Page.

      VTzillaPage_thumb1