Sunday, January 31, 2010

patterns & practices - Unity - Discussions - separate config file path issue

patterns & practices - Unity - Discussions - separate config file path issue: "I have created a unity.config file & placed it in my website bin folder. I have used following code to configure the container.

var assembly = Assembly.GetExecutingAssembly();
Uri uriPath = new Uri(assembly.CodeBase);
string path = Path.Combine(Path.GetDirectoryName(uriPath.AbsolutePath.Replace('%20',' ')), 'unity.config');
var map = new ExeConfigurationFileMap() { ExeConfigFilename = path };
var config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
var section = (UnityConfigurationSection)config.GetSection('unity');
var container = new UnityContainer();
section.Containers['container'].Configure(container);
return container;

A typical thing happened in my code. The uriPath.AbsolutePath returns following path which is the location of my machine

E:\Working%20Folder\IocUnity\IocUnity.Website\bin\

Now if I go with this path, the config file didn't load but if I replace %20 with a space, it worked fine. I could not sort why it is behaving like this. If you look at my code, you can see I used Replace function to get rid of this problem.

Any idea....."

Friday, November 13, 2009

Tail recursion

An amazing long article about recursion in C# (and F# a bit) Bart De Smet's on-line blog
Another nice one could be found here.

Serializing Dictionary to/from XML

I spent a bit of time searching for the magic of how to use XmlSerialize with the Dictionary type this morning and thought I’d share the easy-to-use result:

Assuming you have an object, let’s say it’s called “data” that is of type Dictionary, you can save it to an XML file whose name is contained in the string “dataName” like this:

XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
XmlWriter writer = XmlWriter.Create(dataName, settings);
DataContractSerializer serializer = new DataContractSerializer(typeof(Dictionary));
serializer.WriteObject(writer, data);
writer.Close();

(XmlWriterSettings is used to cause the resulting XML come out on human-readable multiple lines instead of all on one very long line.)

and read it back like this:

XmlReader reader = XmlReader.Create(dataName);
DataContractSerializer serializer = new DataContractSerializer(typeof(Dictionary));

result = (Dictionary)serializer.ReadObject(reader);
reader.Close();

Published 09 November 09 04:08 by Michael Lehman

Thursday, November 12, 2009

Update manger thoughts

While thinking about the Update Manager: I found an old but unknown for me Mocrosoft's technology called Sync Framework. (btw SyncToy is built upon this framework)

I also discovered that WSS web-services could be used for file copy.

Wednesday, November 11, 2009

Lectures on Physics:Tuva

Lectures on Physics, sponsored by B.Gates now available here
http://research.microsoft.com/apps/tools/tuva/

New Mock/Stub Framework as part of Pex

Stubs framework - lightweight test stubs and detours for .NET has been added to PEX.
Pex - Downloads - Microsoft Research: "Stubs framework, lightweight test stubs and detours for .NET"

AOP:AspectF

Sometimes you need to make some code look like AOP. This small class helps you to make a simple Chain of Responsibility.
CodeProject: AspectF Fluent Way to Add Aspects for Cleaner Maintainable Code. Free source code and programming help: "AspectF"


 AspectF.Define
.Log(Logger.Writer, "Inserting customer the easy way")
.HowLong(Logger.Writer, "Starting customer insert",
"Inserted customer in {1} seconds")
.Retry()
.Do(() =>
{
CustomerData data = new CustomerData();
data.Insert(firstName, lastName, age, attributes);
});