Sunday 8 July 2012

Usefull Tools

Data Tables

Before I went to razor 3 and even after I sometime need some nice table displaying tool.
This is really nice tool for getting data displayed fast.

Data tables


Data generation

Dummy data generation is very useful tool when you need context, or any text that is used to populate data in your software. In C# I have been using NBULDER to populate my object

nbuilder
nbuilder documentation


more will follow

Wednesday 4 July 2012

ORACLE query samples

Here you can find examples of sql queries.




Select all distinct (unique) rows from table for field:

SELECT UNIQUE (Field)  FROM Shema.TableName

Entity Framework tips

Tips


This article will be updated ongoing bases.


Have problem with getting information when exception has been thrown in your query result?

use following line:

when you have
var query = context.MyTable.Where(x => x.Id == RequestId)

var errorMessage = string.Format("Message:{0}", ((System.Data.Objects.ObjectQuery)query).ToTraceString()));

Normalisation of query using EF

 We all like IQueryable as it does not fetch data until we need to access data or starting to loop through records. I have been trying to make EF to create as few requests to the database as possible due the cost of getting data. Take example here: 
 
 var distinctRecords = sites.Select(y => y.Name).Distinct().ToList();

var loadedFullRecords = context.tblMasters.Where(x => distinctRecords.Contains(x.Name));

// loop and assign values
foreach (var site in sites.Distinct())
{
 var item  = loadedFullRecords.FirstOrDefault(x => x.siteName == site.Name);
 // rest of code
}



Getting the distinct records and loading all items about the record based on name. Great I got the list and it cost me one trip to the db. Next I itterate through distinct sites and getting details from loaded fill records and using FirstOrDefault as in case that database has been updated since, I got at least null object.
Now item should be loaded and I would expect to get all records be down in my loadedFullRecords colection, so I fire up SQL Server Profiler and run the query. To my suprise I have about 50 queries agains database.

So lets dig deeper.

I have read my code again, and realised that my object loadedFullRecords is still IQueryable untill I loop  and get data. And solution to the query is below.

Simple fix is : Materialise the collection using ToList()!!

 var distinctRecords = sites.Select(y => y.Name).Distinct().ToList();

var loadedFullRecords = context.tblMasters.Where(x => distinctRecords.Contains(x.Name)).ToList();

// loop and assign values
foreach (var site in sites.Distinct())
{
 var item  = loadedFullRecords.FirstOrDefault(x => x.siteName == site.Name);
 // rest of code
}




Change registration info of VS2010

How to change registered owner and registered organization for Visual Studio 2008 and 2010

Here are steps to change registered owner and registered organization for Visual Studio 2008 and 2010.
  1. Open registry editor (type 'regedit' at Run command, clicks OK button, and select 'Yes' if you have UAC confirmation).
  2. Navigate to HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\WindowsNT\CurrentVersion (or HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Note\Microsoft\WindowsNT\CurrentVersion on Widows x64).
  3. Change 2 string values RegisteredOwner and RegisteredOrganization as you expected (create them if they don't exist).
  4. Restart Windows and open Visual Studio, you will see new registration info.


After doing above steps, if you still see old registration info, please add these steps:

For Visual Studio 2010:
  1. Delete vs000223.dat from C:\ProgramData\Microsoft\VisualStudio\10.0\. It's splash screen for Visual Studio 2010.
  2. Run Visual Studio 2010, now you should see a blank splash, there is no registration info, don't be worry.
  3. Close Visual Studio 2010, then open it again. Now you should see new registration info.

source for this article: how-to-change-registered-owner

Tuesday 3 July 2012

How to use sdf database in project.

 Using Microsoft SQL Server Compact 4.0

SQL compact edition is ideal for secluded project where you are not worried about security of database, and is small application. I suppose its good to use as prototype version to display clients data based on database, but not too vital, where you can afford to use backup from whole system and is not crucial  for running 24/7.

You need to install 
Microsoft SQL Server Compact 4.0
http://www.microsoft.com/en-us/download/details.aspx?id=17876

This will give you all basic references and dll files you need to successfully use EF4.3 as I have done.

Sample of MEF composition factory

I have been introduced to MEF quite while ago, but For sample applications I always like to have small working code to use in order to make sure everything is working before I start on something more complex.

Here I have prepared small example of MEF composition with only one source.
The source is executing location in project.
Note: If you are using MEF in website, the code is on the same place where is your main web.config, unless you specified different location.

Eample:

This class will satisfy all dependencies for class "MyClass" using static composition factory.

  public static class Compose
    {
        /// <summary>
        /// Composes the factory.
        /// </summary>
        public static void ComposeFactory(object myObject)
        {
            var catalog = new AggregateCatalog(
                new AssemblyCatalog(System.Reflection.Assembly.GetExecutingAssembly()),
                new DirectoryCatalog(@"."));

            foreach (var container in catalog.Parts.Select(part => new CompositionContainer(catalog)))
            {
                container.ComposeParts(myObject);
            }        }
    }


Where usage might be such as:

public class MyClass(){

public MyClass(){
   Compose.ComposeFactory(this);
}

[Import(typeof(ILogger))]
 public ILogger Logger{get;set;}

// some of your implementation
}


Point of interest.

MEF has two major import types: Import and ImportMany

[Import] or [Import(typeof(IMyInterface))]

Import is used where you want to import only one item.
Unless you specify the typeof MEF will inspect the object of its decoration and automatically try to satisfy the dependency from catalog.
My preference is the second option, which allows you to be specific which object is supposed to satisfy this injection.

Import can be null.


[ImportMany] or [ImportMany(typeof(IMyInterface))]

Import many has to have at least one item, otherwise you will receive an exception.
Again you can leave the satisfaction of the dependency on MEF or be specific.

What is Oracle version?

Oracle Version

 
Today I needed to get find out the version of oracle database using SQL command.



I have achieved it by running::
 
select *
from v$version; 
 
or
 
select *
from product_component_version;