Archive

Posts Tagged ‘Framework’

Microsoft CRM Project Creator

November 12, 2010 Leave a comment

Just a quick update on my CRM project on CodePlex. It can create CRM projects for you and setup your NAnt build script automagically 🙂

Still a few loose ends to tie up but should be release in a few days.

Advertisements

CRM Dynamic Entity Extensions

September 10, 2010 Leave a comment

Dynamic Entities can be a real PITA. Here some code to help lessen the hurt. I have developed a heap of extension methods to let you access field values without all the nasty casts and property contains nonsense.

Some of this is in the new CRM SDK but are not extensions on DynamicEntity itself.

If this helps you then send me a money order 🙂

using System;
using Microsoft.Crm.Sdk;

namespace Microsoft.CRM.ThirdParty.Extensions
{
    public static class DynamicEntityExtensions
    {
        public static string GetStringValueOfPropertyFor(this DynamicEntity entity, string property)
        {
            if (entity.Properties.Contains(property))
                return entity.Properties[property].ToString();
            return String.Empty;
        }

        public static string GetKeyAsStringValueOfPropertyFor(this DynamicEntity entity, string property)
        {
            if (entity.Properties.Contains(property))
                return ((Key)(entity.Properties[property])).Value.ToString();
            return String.Empty;
        }

        public static Guid GetKeyAsGuidValueOfPropertyFor(this DynamicEntity entity, string property)
        {
            if (entity.Properties.Contains(property))
                return new Guid(((Key)(entity.Properties[property])).Value.ToString());
            return Guid.Empty;
        }

        public static int GetIntegerValueFor(this DynamicEntity entity, string property)
        {
            if (entity.Properties.Contains(property))
                return ((CrmNumber)(entity.Properties[property])).Value;
            return 0;
        }

        public static int GetPicklistValueOfPropertyFor(this DynamicEntity entity, string property)
        {
            if (entity.Properties.Contains(property))
                return ((Picklist)entity.Properties[property]).Value;
            return Int32.MinValue;
        }

        public static string GetPicklistNameOfPropertyFor(this DynamicEntity entity, string property)
        {
            if (entity.Properties.Contains(property))
                return ((Picklist)entity.Properties[property]).name;
            return String.Empty;
        }

        public static double GetFloatValueFor(this DynamicEntity entity, string property)
        {
            if (entity.Properties.Contains(property))
                return ((CrmFloat)(entity.Properties[property])).Value;
            return 0;
        }

        public static double GetDoubleValueFor(this DynamicEntity entity, string property)
        {
            if (entity.Properties.Contains(property))
                return ((CrmDouble)(entity.Properties[property])).Value;
            return 0;
        }

        public static decimal GetDecimalValueFor(this DynamicEntity entity, string property)
        {
            if (entity.Properties.Contains(property))
                return ((CrmDecimal)(entity.Properties[property])).Value;
            return 0;
        }

        public static decimal GetMoneyValueFor(this DynamicEntity entity, string property)
        {
            if (entity.Properties.Contains(property))
                return ((CrmMoney)(entity.Properties[property])).Value;
            return 0;
        }

        public static DateTime GetDateTimeValueFor(this DynamicEntity entity, string property)
        {
            if (entity.Properties.Contains(property))
                return ((CrmDateTime)(entity.Properties[property])).UserTime;
            return DateTime.MinValue;
        }
    }
}

Hope this helps you.

Blair…

CSharp Arrays

May 10, 2010 Leave a comment

This follows on from my last post. This code was created as a snippet using the framework posted before and highlights pretty much most of the stuff you can do with arrays in .NET.

I am also going to start comparing Javascript arrays to .NET arrays as I am currently learning / educating myself with Javascript.

using System;
namespace CSharpInDepth
{
	public class ArrayExamples : IRunnable
	{
		public ArrayExamples()
		{
		}
		
		public void RunExamples ()
		{
			Console.WriteLine ("Examples of declaring and using arrays");
			
			int[] values1 = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
			
			Console.WriteLine ("Printing array numbers");
			Array.ForEach (values1, x => Console.WriteLine ("Value: {0}", x));
			
			Console.WriteLine ("Number of dimensions: {0}", values1.Rank);
			Console.WriteLine ("Number of elements: ", values1.Length);
			
			Console.WriteLine ("Reversing array");
			Array.Reverse (values1);
			Console.WriteLine ("Printing array numbers");
			Array.ForEach (values1, x => Console.WriteLine ("Value: {0}", x));
			
			Console.WriteLine ("Sorting the elements");
			Array.Sort (values1);
			Console.WriteLine ("Printing array numbers");
			Array.ForEach (values1, x => Console.WriteLine ("Value: {0}", x));
			
			Console.WriteLine("Number 10 exists in the array is: {0}",Array.Exists<int>(values1, x => x == 10));
			int value = Array.Find<int>(values1, x => x.Equals(10));
			Console.WriteLine("The item found in the array matching the predicate is: {0}", value);
			
			Console.WriteLine("Finding the number 10 in the array");
			int index = Array.BinarySearch(values1,10);
			Console.WriteLine("Found at index position: {0}",index);
			
			int[,] values2 = new[,]{{1,2,3,4,5},{6,7,8,9,10}};
			Console.WriteLine("Values 2 rank is: {0}", values2.Rank);
			Console.WriteLine("Dimension 1: {0}", values2.GetLength(0));
			Console.WriteLine("Dimension 2: {0}", values2.GetLength(1));
			
			int[] values3 = new int[values1.Length - 1]; // Declare array reference for copying
			Array.Copy(values1,values3,values1.Length - 1);
			Console.WriteLine ("Printing new array numbers");
			Array.ForEach (values1, x => Console.WriteLine ("Value: {0}", x));
			
			Console.WriteLine("Clearing the array");
			Array.Clear(values1,0,values1.Length);
			Console.WriteLine("Count of the items in the array after clearing is: {0}", values1.Length);
			Console.WriteLine ("Printing cleared array numbers");
			Array.ForEach (values1, x => Console.WriteLine ("Value: {0}", x));
			
			
		}
	}
}

Blair….

Creating a sample code snippet runner

May 10, 2010 Leave a comment

This may seem like a strange post but I thought I would share with you how I create code samples and run them. I create classes with the concepts I want to learn and implement IRunnable and my framework lists them in a menu and then you can run the classes. I really quite like this.

Heres the code. Nothing special, I just hope it helps you organise you code when your learning concepts.

Just a note these samples where all developed on Mac OS X Snow Leopard using the latest mono and mondevelop bits.

using System;
using System.Collections.Generic;
using System.Reflection;
using System.Linq;

namespace CSharpInDepth
{

	public interface IRunnable{
		void RunExamples();
	}

	public class InvalidSelectionException : Exception{
	
	}

	public class MenuGenerator{
	
		private string typeNameToExecute;
		private IEnumerable<string> cachedTypes;
		private RunnerableTypeNameGenerator generator;
		
		public MenuGenerator ()
		{
			generator = new RunnerableTypeNameGenerator();
			cachedTypes = new List<string>();
		}
	
		public void GenerateMenu()
		{
			
			cachedTypes = generator.GetTypes ();
			
			Console.WriteLine("Enter the number of the type you want to run.");
			
			if (cachedTypes != null && cachedTypes.Any ()) {
				int counter = 0;
				foreach (string type in cachedTypes){
					Console.WriteLine("{0}. {1}",++counter,type);
				}
			}
		}
		
		public void AcceptSelection(int selection){
			if(selection < 0)
				throw new InvalidSelectionException();
			
			typeNameToExecute = cachedTypes.ElementAt(selection - 1); 
			
		}
		
		public void Execute(){
			var instance = Activator.CreateInstance(Assembly.GetExecutingAssembly().GetName().FullName, typeNameToExecute);
			var runnerableType = (IRunnable)(instance.Unwrap());
			runnerableType.RunExamples();
		}
	}
	
	public class RunnerableTypeNameGenerator{
		public IEnumerable<string> GetTypes(){
			return Assembly.GetExecutingAssembly()
				    .GetTypes()
				    .Where(x => x.GetInterfaces().Any(y => y.Name.Equals("IRunnable")))
				    .Select(x => x.FullName);
		}
	}

	class MainClass
	{
		public static void Main (string[] args)
		{
			MenuGenerator generator = new MenuGenerator();
			generator.GenerateMenu();
			int selection = Convert.ToInt32(Console.ReadLine());
			Console.WriteLine("You selection was: " + selection);
			generator.AcceptSelection(selection);
			generator.Execute();
			Console.ReadKey();
		}
	}
}


Blair…