Thursday, August 25, 2011

My 1973 Belomo Vilia

belomo vilia by vahnpaper
belomo vilia, a photo by vahnpaper on Flickr.

Too bad its broken.. tsk.. tsk..

Monday, July 18, 2011

anito.NET source transferred to Sourceforge

anito.NET repository is officially transferred to source forge

http://anito-net.sourceforge.net

Codeplex site will still be available though as mirror

Friday, June 3, 2011

anito.NET Weekly Build Update - Pre-Alpha Version 1.0.1.6 Now Available

Build version 1.0.1.6 is now available for download in the link provided below

http://anito.codeplex.com/releases/view/67556

or Direct Download to http://anito.codeplex.com/releases/67556/download/246653

Affected work items for build 1.0.1.6

http://anito.codeplex.com/workitem/248
http://anito.codeplex.com/workitem/249
http://anito.codeplex.com/workitem/250

Build Notes

• One to Many Relationship
• DateTime values persistence Fixed for MySql
• DateTime values persistence Fixed for SQLite

Next : Build 1.0.1.7 will be on 6/10/2011 (Friday)

Tuesday, May 31, 2011

05-29-2011 @ church

ashy and baby arwenashy and baby arwencream-o holicarwen grabbing the cambaby arwen at church

05-29-2011, a set on Flickr.

Monday, May 30, 2011

Baby Arwen Laughing

anito.net Weekly Build Update - Pre-Alpha Version 1.0.1.5 Now Available

Build version 1.0.1.5 is now available for download at the link provided below

http://anito.codeplex.com/releases/view/66380

Affected work items

http://anito.codeplex.com/workitem/162
http://anito.codeplex.com/workitem/202

Build Notes
One to One Relation Entity Support
DateTime values persistence

Thursday, May 26, 2011

Arwen at Jbee

arwen at jbee by mdcuesta
arwen at jbee, a photo by mdcuesta on Flickr.

look how she loves sphagetti

Office Window rain droplets

office window water droplets by mdcuesta
office window water droplets, a photo by mdcuesta on Flickr.

Its a rainy cebu city today.

Thursday, May 19, 2011

anito.net - Build 1.0.1.5 will be moved on 5/27/2011

Sorry their will be no friday builds for anito.net tomorrow. My daughter was admitted to a hospital last monday, I have to take care of the releasing papers tomorrow.

Monday, May 16, 2011

anito.net Weekly Build Update - Pre-Alpha Version 1.0.1.4 Now Available

anito.net Build 1.0.1.4 is now released for testing and available for download in the link provided below

http://anito.codeplex.com/releases/view/65922

Release Notes:

DbNull Checking Performance Enhancement
Mapping support for inherited fields / members

Direct Download http://anito.codeplex.com/releases/65922/download/238826

Thursday, May 12, 2011

ASP.net MVC - customizing a ViewEngine to locate Views, Partial Views and MasterPages

When you create an MVC web project in Visual Studio there is already a default folder structure convention. Like the image below.

default asp.net mvc structure

I'm new to ASP.net MVC, but I've been developing .net applications for over 4 years now. The problem is I never liked the default folder structure of the asp.net mvc project and certainly this is now how I want my web project to be implemented. There are other things need to be considered, for instance the decoupling of components, for example if I want my controllers to be on a different assembly and so as my models.

Now when you move the files from the Views folder into the root directory, your web application will not run, because by design the default ViewEngine will always look up at this directory ~/Views to find your controls, master pages and views.

I want my mvc project to be structured something like this.

modified asp.net mvc structure

You could see, I created another project which holds the web controllers and the views are transferred to the root folder instead of ~/Views.

~/Home - Home folder (used to be ~/Views/Home)
~/Account - Acount (used to be ~/Views/Account)
~/Controls - Logon Control was transffered here instead of ~/Viws/Shared/

and the Site.Master and the Error.aspx was transferred to the ~/ (root)

With regards to the controllers HomeController and AccountController they were just transferred to another assembly but their namespaces are intact.

In order for this to work we need to customize a ViewEngine so we could locate our views/partial views.

Here is the customized ViewEngine class

public class CustomViewEngine : System.Web.Mvc.WebFormViewEngine
    {
        public CustomViewEngine()
        {
            ViewLocationFormats = new[] {
            "~/{0}.aspx",
            "~/{0}.ascx",
            "~/{1}/{0}.aspx",
            "~/{1}/{0}.ascx"
            };

            PartialViewLocationFormats = new[]{
            "~/Controls/{0}.aspx",
            "~/Controls/{0}.ascx"
            };

            MasterLocationFormats = new[] {
            "~/{0}.master",
            "~/Shared/{0}.master"
            };
        }
    }

ViewLocationFormats - where to find the views
PartialViewLocationFormats - where to find the controls
MasterLocationFormats - where to find the master pages

You could see in the implementation that the location of the views, partials views and master pages are hard coded, it would be best to make it dynamic, maybe put it in a config file or a database. And also its better if you could also put your customized ViewEngine class in a different assembly for reusability.

After you have a customized ViewEngine all you have to do is to add your customized ViewEngine to asp.net MVCs ViewEngine Collection.

On the application start method of your global.asax add the following line of code

protected void Application_Start()
{
    //instantiate your customized view engine
    CustomViewEngine customViewEngine = new CustomViewEngine(); 

    System.Web.Mvc.ViewEngines.Engines.Clear();
    //add your customized view engine to the engine collection
    System.Web.Mvc.ViewEngines.Engines.Add(customViewEngine);
    RegisterRoutes(RouteTable.Routes);
}

Now its done, it should work.

Moalboal - Summer 2011

This is a late post, these photos were taken last March 27 2011 in Moalboal. Moalboal is a town in Cebu South famous for its beaches and diving spots. Too bad I didn't have any underwater camera housing, to provide underwater photos.

Moalboal BeachIMG_1112IMG_1106IMG_1111ashyIMG_1128
RobIMG_1126IMG_1127

Moalboal - Summer 2011, a set on Flickr.

Wednesday, May 11, 2011

Microsoft Acquires Skype for $8.5 Billion

See Microsoft press release http://www.microsoft.com/Presspass/press/2011/may11/05-10CorpNewsPR.mspx

Does this mean no more free video and skype to skype calls? haha. I have to mention $8.5 billion?.

For non-windows platforms, "Microsoft will continue to invest in and support Skype clients on non-Microsoft platforms." quoted from the link above. I guess it's still ok. For now.

Also from the link above, "Skype will support Microsoft devices like Xbox and Kinect, Windows Phone and a wide array of Windows devices, and Microsoft will connect Skype users with Lync, Outlook, Xbox Live and other communities." I believe those are good areas to integrate skype into.

Mapping a Data Reader to Object Properties with DBNull Support using Reflection.Emit

I came along with this post http://improve.dk/archive/2008/05/02/mapping-datareader-to-objects-using-reflection-emit.aspx which shows how to map records from a data reader into properties of objects.
The mapping performance is fas but doesn't support DBNull. I tried to add capabilities for DBNull handling and here's what I came up.

A converter class which will do the checking for DBNull values and will return a type compatible value.
Inside the class are static methods that will do the DbNull check.

private static T IsDbNull<T>(object value)
{
    return IsDbNull(value, default(T));
}

private static T IsDbNull<T>(object value, T returnedValue)
{
    if (value == DBNull.Value)
        return returnedValue;
    return (T) value;
}

Now all we have to do is implement the checking methods for each type in the class.

for Int32

public static System.Int32 ToInt32(object value)
{
    return IsDbNull<System.Int32>(value);
}
Heres the full code for the Converter Class (Download);
Converter Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sampler.Emit.Mapping
{
    public class Converter
    {
        public static System.Int16 ToInt16(object value)
        {
            return IsDbNull<System.Int16>(value);
        }

        public static System.Int32 ToInt32(object value)
        {
            return IsDbNull<System.Int32>(value);
        }

        public static System.Int64 ToInt64(object value)
        {
            return IsDbNull<System.Int64>(value);
        }

        public static System.Single ToSingle(object value)
        {
            return IsDbNull<System.Single>(value);
        }

        public static System.Boolean ToBoolean(object value)
        {
            return IsDbNull<System.Boolean>(value);
        }

        public static System.String ToString(object value)
        {
            return IsDbNull<System.String>(value);
        }

        public static System.DateTime ToDateTime(object value)
        {
            return IsDbNull<System.DateTime>(value);
        }

        public static System.Decimal ToDecimal(object value)
        {
            return IsDbNull<System.Decimal>(value);
        }

        public static System.Double ToDouble(object value)
        {
            return IsDbNull<System.Double>(value);
        }

        public static System.Guid ToGuid(object value)
        {
            return IsDbNull<System.Guid>(value);
        }

        public static System.Byte ToByte(object value)
        {
            return IsDbNull<System.Byte>(value);
        }

        public static System.Byte[] ToBytes(object value)
        {
            return IsDbNull<System.Byte[]>(value);
        }

        private static T IsDbNull<T>(object value)
        {
            return IsDbNull(value, default(T));
        }

        private static T IsDbNull<T>(object value, T returnedValue)
        {
            if (value == DBNull.Value)
                return returnedValue;
            return (T) value;
        }
    }
}

And heres the improved Mapper Class (Download)
Mapper Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Reflection;
using System.Reflection.Emit;

namespace Sampler.Emit.Mapping
{
    public class Mapper
    {
        //Mapping Delegate
        private delegate T ToTDelegate<T>(DbDataReader reader);
        
        private static Dictionary<Type, Delegate> m_mapperCache;
        
        private Dictionary<Type, Delegate> MapperCache
        {
            get
            {
                if (m_mapperCache == null)
                    m_mapperCache = new Dictionary<Type, Delegate>();
                return m_mapperCache;
            }
        }

        public void ClearMappingCache()
        {
            MapperCache.Clear();
        }

        public T ToT<T>(DbDataReader reader)
        {
            if (reader.Read())
                return ToT<T>(reader, GetMappingMethod<T>());
            return default(T);
        }

        public L ToList<L, T>(DbDataReader reader)
            where L : IList<T>, new()
            where T : class, new() 
        {
            ToTDelegate<T> mappingMethod = GetMappingMethod<T>();
            L list = new L();
            while (reader.Read())
                list.Add(ToT<T>(reader, mappingMethod));
            return list;
        }

        public IEnumerable<T> ToEnumerable<T>(DbDataReader reader)
        {
            ToTDelegate<T> mappingMethod = GetMappingMethod<T>();
            while (reader.Read())
                yield return ToT<T>(reader, mappingMethod);
        }

        private T ToT<T>(DbDataReader reader, ToTDelegate<T> mapper)
        {
            return mapper(reader);
        }

        private ToTDelegate<T> GetMappingMethod<T>()
        { 
            Type typeT = typeof(T);
            if (!MapperCache.ContainsKey(typeT))
            {
                Type[] methodArgs = { typeof(DbDataReader) };
                DynamicMethod dm = new DynamicMethod("MapDatareader", typeof(T), methodArgs, typeof(T));

                ILGenerator il = dm.GetILGenerator();
                il.DeclareLocal(typeof(T));
                il.Emit(OpCodes.Newobj, typeof(T).GetConstructor(Type.EmptyTypes));
                il.Emit(OpCodes.Stloc_0);
                
                PropertyInfo[] properties = typeT.GetProperties();

                foreach (PropertyInfo info in properties)
                {
                    il.Emit(OpCodes.Ldloc_0);
                    il.Emit(OpCodes.Ldarg_0);

                    il.Emit(OpCodes.Ldstr, info.Name);

                    MethodInfo methodInfo = typeof(DbDataReader).GetMethod("get_Item", new Type[] { typeof(string) });
                    il.Emit(OpCodes.Callvirt, methodInfo);

                    il.Emit(OpCodes.Call, GetConverterMethod(info.PropertyType));

                    il.Emit(OpCodes.Callvirt, typeof(T).GetMethod("set_" + info.Name, BindingFlags.Public | BindingFlags.Instance));
                }

                il.Emit(OpCodes.Ldloc_0);
                il.Emit(OpCodes.Ret);
                MapperCache.Add(typeof(T), dm.CreateDelegate(typeof(ToTDelegate<T>)));
            }
            return MapperCache[typeT] as ToTDelegate<T>;
        }

        private MethodInfo GetConverterMethod(Type type)
        { 
             switch (type.Name.ToUpper())
             {
                 case "INT16":
                     return CreateConverterMethodInfo("ToInt16");
                 case "INT32":
                     return CreateConverterMethodInfo("ToInt32");
                 case "INT64":
                     return CreateConverterMethodInfo("ToInt64");
                 case "SINGLE":
                     return CreateConverterMethodInfo("ToSingle");
                 case "BOOLEAN":
                     return CreateConverterMethodInfo("ToBoolean");
                 case "STRING":
                     return CreateConverterMethodInfo("ToString");
                 case "DATETIME":
                     return CreateConverterMethodInfo("ToDateTime");
                 case "DECIMAL":
                    return CreateConverterMethodInfo("ToDecimal");
                 case "DOUBLE":
                     return CreateConverterMethodInfo("ToDouble");
                 case "GUID":
                     return CreateConverterMethodInfo("ToGuid");
                 case "BYTE[]":
                     return CreateConverterMethodInfo("ToBytes");
                 case "BYTE":
                     return CreateConverterMethodInfo("ToByte");
             }
             return null;
        }

        private MethodInfo CreateConverterMethodInfo(string method)
        {
            return typeof(Converter).GetMethod(method, new Type[] { typeof(object) });
        }
    
    }
}

Here's how we use the Mapper class to map data readers.

public class Customer
{
    public int ID { get; set; }
    public Guid CustomerCode { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }
    public string City { get; set; }
    public string Country { get; set; }
    public decimal Balance { get; set; }
    public DateTime DateCreated { get; set; }
}

string connectionString = @"connectionstring";
SqlConnection connection = null;

try
{
    connection = 
   new System.Data.SqlClient.SqlConnection(connectionString);
    connection.Open();

    SqlCommand 
   command = 
   new System.Data.SqlClient.SqlCommand();
    command.CommandText = "SELECT * FROM Customer";
    command.Connection = connection;

    SqlDataReader reader = command.ExecuteReader();
    Mapper mapper = new Mapper();
    List<Customer> list = mapper.ToList<List<Customer>, Customer>(reader as DbDataReader);
    //Enumerable
    IEnumerable<customer> enumerable = 
   mapper.ToEnumerable<customer>(reader as DbDataReader);
}
catch (Exception ex)
{
    if (connection.State != ConnectionState.Closed)
        connection.Close();
}

Tuesday, May 10, 2011

Portrait

Ashleh

Portrait, a set on Flickr.

Landscapes, HDR & Aerial

compostella green lagoonhunasOffice Top

Landscapes, HDR & Aerial, a set on Flickr.

Architecture

church

Architecture, a set on Flickr.

Long Exposure

rotondafireworksghostly lights

Long Exposure, a set on Flickr.

Macro

crossing antfly2flyBlackDSC_0534

Macro, a set on Flickr.