McKelt.com

Remembering Thoughts

 

Recent comments

Authors

Categories


Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2010

Custom NHibernate User Type

How to use a custom NHibernate User Type

 

To store the state of a Risk (ie RiskState) as a value in a column we can use a NHibernate.UserType

 

The RiskState can be one of the following: (these are all derived from RiskState which implements IRiskState)

image

Our user type
 
 public class RiskStateNameUserType : IUserType
    {
        #region IUserType Members
 
        public object Assemble(object cached, object owner)
        {
            return cached;
        }
 
        public object DeepCopy(object value)
        {
            return value;
        }
 
        public object Disassemble(object value)
        {
            return value;
        }
 
        public int GetHashCode(object x)
        {
            return x.GetHashCode();
        }
 
        public bool IsMutable
        {
            get { return false; }
        }
 
        public object NullSafeGet(System.Data.IDataReader dr, string[] names, object owner)
        {
            var property0 = NHibernateUtil.String.NullSafeGet(dr, names[0]);
 
            if (property0 == null)
            {
                return null;
            }
 
            IRiskState state;
 
            if (owner is Risk)
            {
                state =
                    (IRiskState)
                    Activator.CreateInstance(Type.GetType(typeof(IRiskState).Namespace + "." + (string)property0),
                                             owner);
            }
            else
            {
                state =
                    (IRiskState)
                    Activator.CreateInstance(Type.GetType(typeof(IRiskState).Namespace + "." + (string)property0));
 
            }
 
            return state;
 
        }
 
        public void NullSafeSet(System.Data.IDbCommand cmd, object value, int index)
        {
            if (value == null)
            {
                ((IDataParameter)cmd.Parameters[index]).Value = DBNull.Value;
            }
            else
            {
                var state = (IRiskState)value;
                ((IDataParameter)cmd.Parameters[index]).Value = state.GetType().Name;
            }
        }
 
        public object Replace(object original, object target, object owner)
        {
            return original;
        }
 
        public Type ReturnedType
        {
            get { return typeof(RiskState); } 
        }
 
        public NHibernate.SqlTypes.SqlType[] SqlTypes
        {
            get { return new[] { NHibernateUtil.String.SqlType }; }
        }
 
        public new bool Equals(object x, object y)
        {
            if (x == null && y == null) return true;
            if (x == null || y == null) return false;
            return x.GetType() == y.GetType();
        }
 
        #endregion
 
    }
 
Using this on a property as follows:
 
 
 [Property(ColumnType = "Matlock.Core.Risks.RiskStateNameUserType, Matlock.Core")]
 public IRiskState RequiredState { get; set; }

 

We can now store types:

 

image

 

 

If importing from a file and we want to parse a string to our user type

 

        protected T GetCustomType<T>(XElement dsl, string attributeName)
        {
            string attribute = (string)dsl.Attribute(attributeName);
            var customType = (T)Activator.CreateInstance(Type.GetType(typeof(T).Namespace + "." + attribute));
            return customType;
        }
 

Posted by chris on Thursday, August 26, 2010 10:06 PM
Permalink | Comments (0) | Post RSSRSS comment feed

Invalid object name 'master.dbo.spt_values'.

This means your SQL Master DB is in a bad state.

To fix:

Error- Invalid object name 'master.dbo.spt_values'.

CD

"C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\Install"

run u_tables.sql in master....


Posted by chris on Tuesday, August 24, 2010 5:31 PM
Permalink | Comments (0) | Post RSSRSS comment feed

Agile Flavours

XP

  • 5 values
  • 14 principles
  • 12 Primary practices
  • 11 Corollary practices

Lean

  • 7 principles
  • 22 thinking tools

Scrum

  • 5 Values
  • 3 Roles (Product owner, scrum master, scrum team)
  • 3 Ceremonies  (Sprint planning meeting, Stand up, review & retrospective)
  • 3 Work Products (product backlog, sprint backlog, burndown chart)
  • (depending on which definition you choose)

DSDM Atern

  • 8 Principles
  • 5 Lifecycle phases
  • 12 roles
  • 17 work products
  • 5 key techniques

Agile Unified Process

  • 6 Principles
  • 7 Disciplines
  • 4 Lifecycle Phases
  • 14 Roles
  • 8 minimum deliverables
  • 4 guidance pieces

Posted by chris on Tuesday, August 03, 2010 11:44 AM
Permalink | Comments (0) | Post RSSRSS comment feed

RhinoMocks – WhenCalled

The following test would fail without this

 

.WhenCalled(invocation => invocation.ReturnValue = new TestResult(){IsTrue = true, Message = "BBB"})

 

 

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

using Matlock.Core.Shared;

using Microsoft.VisualStudio.TestTools.UnitTesting;

using Rhino.Mocks;

namespace Matlock.Tests
{
    [TestClass]
    public class ChrisTest
    {

        private IRuleService ruleService;

        [TestMethod]
        public void ShouldNotChangeReturnedTestResult()
        {
            ruleService = MockRepository.GenerateMock<IRuleService>();
            var testResult = new TestResult();
            testResult.IsTrue = true;
            testResult.Message = "AAA";
            ruleService.Stub(a => a.GetTestResult()).Return(testResult)
                .WhenCalled(invocation => invocation.ReturnValue = new TestResult(){IsTrue = true, Message = "BBB"});



            var testClass = new TestClass(ruleService);
            testClass.KillTheString();
            Assert.IsTrue(testClass.StringIsThere());
            
        }

        public interface IRuleService
        {
            TestResult GetTestResult();
        }

        public class TestResult
        {
            public bool IsTrue { get; set; }
            public string Message { get; set; }
        }

        private class TestClass
        {
            private readonly IRuleService ruleService;

            public TestClass(IRuleService ruleService)
            {
                this.ruleService = ruleService;
            }

            public void KillTheString()
            {
                var result = ruleService.GetTestResult();
                result.Message = string.Empty;
            }

            public bool StringIsThere()
            {
                var result = ruleService.GetTestResult();
                return !string.IsNullOrEmpty(result.Message);
            }
        }
    }
}

Posted by chris on Tuesday, July 06, 2010 10:51 AM
Permalink | Comments (0) | Post RSSRSS comment feed

Crystal Software Development

Crystal Software Development Download


Posted by chris on Wednesday, June 30, 2010 3:56 PM
Permalink | Comments (0) | Post RSSRSS comment feed

Integrated Security = SSPI -- Security Support Provider Interface

Using Integrated Security in connection strings should be either of the following:

Integrated Security=SSPI

or

Integrated Security=false

It should not be

Integrated Security=true

Posted by chris on Tuesday, June 29, 2010 5:39 PM
Permalink | Comments (0) | Post RSSRSS comment feed

LIFO vs FIFO

LIFO

FIFO

higher COGS   lower COGS
lower taxes   higher taxes
lower net income   higher net income
lower inventory balances   higher inventory balances
higher cash flows (less tax paid out)   lower cash flows (more tax paid out)
lower net and gross margins   higher net and gross margins
lower current ratio   higher current ratio
higher inventory turnover   lower inventory turnover
DA and DE higher   DA and DE lower

 

Under IFRS the permissible cost flow methods are:

  • Specific Identification
  • FIFO
  • Weighted average cost

Categories: CFA
Posted by chris on Monday, June 21, 2010 10:30 PM
Permalink | Comments (0) | Post RSSRSS comment feed

WPF UI Thread Dispatcher

A simple implemention for calling asych methods from the UI

 

Examples

 

1.

dispatcher.ExecuteOnMainUIThread(CommandManager.InvalidateRequerySuggested);

 

2.

dispatcher.Execute(() =>
{
SomeLongRunningMethodHere();
});

 

The interface

 
using System;

namespace Mvvm
{
public interface IDispatcher
{
void Execute(Action action);

void ExecuteOnMainUIThread(Action action);
}
}

Synchronous for use in Testing

 
using System;

namespace Mvvm
{
public class SynchronousDispatcher : IDispatcher
{
public void Execute(Action action)
{
action();
}

public void ExecuteOnMainUIThread(Action action)
{
action();
}
}

}

Asynchronous for use by the application at run time

 
using System;

namespace Mvvm
{
using System.Windows;
using System.Windows.Threading;

public class AsynchronousDispatcher : IDispatcher
{
public void Execute(Action action)
{
action.BeginInvoke(CallBack, action);
}

public void ExecuteOnMainUIThread(Action action)
{
Dispatcher dispatcher;

if (Application.Current != null)
{
dispatcher = Application.Current.Dispatcher;
}
else
{
dispatcher = Dispatcher.CurrentDispatcher;
}

dispatcher.Invoke(action);
}

private void CallBack(IAsyncResult result)
{
try
{
((Action)result.AsyncState).EndInvoke(result);
}
catch (Exception ex)
{
// Need to raise the exception on the main thread
ExecuteOnMainUIThread(() =>
{
throw ex;
}
);
}
finally
{
result.AsyncWaitHandle.Close();
}


}
}
}


Categories: .Net
Posted by chris on Wednesday, May 26, 2010 12:15 PM
Permalink | Comments (0) | Post RSSRSS comment feed

Rhino Mock Constraints -- AssertWasCalled

Rhino Mock Constraints allow use to test a methods parameters were called with the correct arguments.

public interface IDocumentService
{
void Save(string userName, Document document, Stream stream);
}
 
 

Some ways to ensure the method that contains the save method passes the correct internally constructed arguments include
 
documentService.AssertWasCalled(
a=>a.Save("chris", doc, adaptor.InputStream),               
b => b.Constraints(Is.Equal(“chris”), Is.NotNull(), Is.AnyThing()));
 
 
Passing in Property.AllPropertiesMatch(this.MyTestObjectWithPropertiesThatShouldMatch)
will check values against each object
 


Posted by Chris on Thursday, May 20, 2010 5:55 PM
Permalink | Comments (0) | Post RSSRSS comment feed

CFA – Accounting Ratios

Liquidity ratios

 

\mbox{Current ratio} = \frac {\mbox{Current Assets}} {\mbox{Current Liabilities}}

 

 

\mbox{Quick (Acid Test) Ratio} = {\mbox{Cash and Cash Equivalent} + \mbox{Marketable Securities} + \mbox{Accounts Receivable}\over \mbox{Current Liabilities}}

Cash ratio is the same as Quick without the accounts receivable

 

 

Solvency ratios

 

Long term debt to equity =  total debt / total equity

 

Debt to equity = total debt / total equity

 

Total debt ratio = total debt / total assets

 

Financial leverage ratio = total assets / total equity


Categories: CFA
Posted by chris on Saturday, May 08, 2010 11:42 AM
Permalink | Comments (1) | Post RSSRSS comment feed