Google Sitemap .NET links

Some links with information about creating a Google Sitemap either directly in XML or via some .NET code.

http://gerardmcgarry.com/blog/creating-a-google-sitemap-aspnet-website

http://www.codeproject.com/KB/aspnet/GoogleSiteMapProvider.aspx

Memoryleaks with WinDbg

I have experienced the first major project with a memory leak problem… and how do one get started with Memory leak finding.

It is a tough task and the learning curve is quite steep.

You have to get to know the WinDbg. You can start here: http://blogs.msdn.com/tess/pages/net-debugging-demos-information-and-setup-instructions.aspx

Tess Ferrandez has some very nice “Debugging labs”, well take a look at: http://blogs.msdn.com/tess/

If you have a problem with a memory leak, take a look at his one:

http://blogs.msdn.com/tess/archive/2005/11/25/496899.aspx

Posted in Development. Tags: , , . 5 Comments »

Using Multiple Configuration files for NHibernate (HybridSessionBuilder)

 

I have been using Castle Windsor and NHibernate for some months now, so I am a bit new and still learning. But then again, it is learning developers like me, that asks all the questions.

This post is also written because of a simple question: Does the HybridSessionBuilder support multiple configurations?

Why this question? Well, I have a project where some repositories uses one database and some other uses another database.

Original implementation

My little journey started out with my looking original implementation of the HybridSessionBuilder (HSB). You can read about the original implementation here:

http://jeffreypalermo.com/blog/use-this-nhibernate-wrapper-to-keep-your-repository-classes-simple/

But as you can see:image

The original implementation used a static session factory and session, so this is does not support multiple configurations.

CodeCampServer/Tarantino

I then found out that the CodeCampServer also was using the HSB. Hoping to find an implementation that supported my request, I then started to read the source code.

But by viewing the source code for CodeCampServer I soon found out that the code, for the HSB, had been updated to support multiple factories and sessions. Yeeeaaaaa :)

Furthermore I found out that the HSB now is a part of a collection of libraries called Tarantino.

I was happy, the HybridSessionBuilder now supported my requests, and it was ready to be used in my projects. I thought.

Test web application

image Then I build a small test web application project. It used Castle Windsor (for dependency injection) and NHibernate. I quickly configured Castle Windsor and made two NHibernate configuration files.

nhibernate.cfg.xml used this connection string:

Data Source=localhost\sqlexpress;Initial Catalog=DB1;Integrated Security=true

nhibernate2.cfg.xml used this connection string:

Data Source=localhost\sqlexpress;Initial Catalog=DB2;Integrated Security=true

My windsor.config.xml looks like this:

<component id="firstRepository" type="HybridSessionBuilderTest.FirstRepository, HybridSessionBuilderTest">
  <parameters>
    <ConfigurationFile>hibernate.cfg.xml</ConfigurationFile>
  </parameters>
</component>
 
<component id="secondRepository" type="HybridSessionBuilderTest.SecondRepository, HybridSessionBuilderTest">
  <parameters>
    <ConfigurationFile>hibernate2.cfg.xml</ConfigurationFile>
  </parameters>
</component>

It basically just configures the two repositories with each own configuration.

But to my surprise, it did not work. I made a small page that prints the connection string of each of the repositories:

image

Only the default NHibernate configuration file was used.

Bug or feature?

That was not the output I was looking for, so to find the problem, I did some more test and dug into the code.

I was expecting an object structure like this:

image

But I got something like this:

image  I identified the following method to be the central, when the session was instantiated:

   1: private ISession getExistingOrNewSession(ISessionFactory factory, string configurationFile)
   2:         {
   3:             if (HttpContext.Current != null)
   4:             {
   5:                 var session = GetExistingWebSession();
   6:  
   7:                 if (session == null || !session.IsOpen)
   8:                 {
   9:                     session = openSessionAndAddToContext(factory, configurationFile);
  10:                 }
  11:  
  12:                 return session;
  13:             }
  14:  
  15:             var currentSession = _currentSessions.ContainsKey(configurationFile)
  16:                                     ? _currentSessions[configurationFile]
  17:                                     : null;
  18:             if (currentSession == null || !currentSession.IsOpen)
  19:             {
  20:                 _currentSessions[configurationFile] = OpenSession(factory);
  21:             }
  22:  
  23:             return _currentSessions[configurationFile];
  24:         }

Patch/solution

The problem I identified is line #5. It returns the existing session based on the default configuration, no matter what you specify in the configurationFile parameter.

I changed it to:

   1: var session = GetExistingWebSession(configurationFile);

 

And tadaaa:

image

The second repository now uses the second configuration file :)

Conclusion

You might ask, why I spend time writing a blog post describing this little change to the HybridSessionBuilder.

First of all, having a HSB that can use multiple configurations is rather important to me and the projects I am working on.

Second of all, as NHibernate and Castle Windsor is still a bit new to me, one of the best ways to learn its capabilities and how to use it, is to dig in deep. And that is exactly what I have done.

And BTW – I have not just learned about HSB, but also CodeCampServer, Tarantino, S#arp and how it handles multiple configurations, and how it all works together.

NHibernateSessionModule

Please note. Before my change is used, you need to also update the NHibernateSessionModule. It does currently not dispose all the sessions that are opened.

Posted in Development. Tags: , , . No Comments »

Multiple Configurations for NHibernate using HybridSessionBuilder

 

I have been using Castle Windsor and NHibernate for some months now, so I am a bit new and still learning. But then again, it is learning developers like me, that asks all the questions.

This post is also written because of a simple question: Does the HybridSessionBuilder support multiple configurations?

Why this question? Well, I have a project where some repositories uses one database and some other uses another database.

Original implementation

My little journey started out with my looking original implementation of the HybridSessionBuilder (HSB). You can read about the original implementation here:

http://jeffreypalermo.com/blog/use-this-nhibernate-wrapper-to-keep-your-repository-classes-simple/

But as you can see:image

The original implementation used a static session factory and session, so this is does not support multiple configurations.

CodeCampServer/Tarantino

I then found out that the CodeCampServer also was using the HSB. Hoping to find an implementation that supported my request, I then started to read the source code.

But by viewing the source code for CodeCampServer I soon found out that the code, for the HSB, had been updated to support multiple factories and sessions. Yeeeaaaaa :)

Furthermore I found out that the HSB now is a part of a collection of libraries called Tarantino.

I was happy, the HybridSessionBuilder now supported my requests, and it was ready to be used in my projects. I thought.

Test web application

image Then I build a small test web application project. It used Castle Windsor (for dependency injection) and NHibernate. I quickly configured Castle Windsor and made two NHibernate configuration files.

nhibernate.cfg.xml used this connection string:

Data Source=localhost\sqlexpress;Initial Catalog=DB1;Integrated Security=true

nhibernate2.cfg.xml used this connection string:

Data Source=localhost\sqlexpress;Initial Catalog=DB2;Integrated Security=true

My windsor.config.xml looks like this:

<component id="firstRepository" type="HybridSessionBuilderTest.FirstRepository, HybridSessionBuilderTest">
  <parameters>
    <ConfigurationFile>hibernate.cfg.xml</ConfigurationFile>
  </parameters>
</component>
 
<component id="secondRepository" type="HybridSessionBuilderTest.SecondRepository, HybridSessionBuilderTest">
  <parameters>
    <ConfigurationFile>hibernate2.cfg.xml</ConfigurationFile>
  </parameters>
</component>

 

It basically just configures the two repositories with each own configuration.

But to my surprise, it did not work. I made a small page that prints the connection string of each of the repositories:

image

Only the default NHibernate configuration file was used.

Bug or feature?

That was not the output I was looking for, so to find the problem, I did some more test and dug into the code.

I was expecting an object structure like this:

image

But I got something like this:

image  I identified the following method to be the central, when the session was instantiated:

   1: private ISession getExistingOrNewSession(ISessionFactory factory, string configurationFile)
   2:         {
   3:             if (HttpContext.Current != null)
   4:             {
   5:                 var session = GetExistingWebSession();
   6: 
   7:                 if (session == null || !session.IsOpen)
   8:                 {
   9:                     session = openSessionAndAddToContext(factory, configurationFile);
  10:                 }
  11: 
  12:                 return session;
  13:             }
  14: 
  15:             var currentSession = _currentSessions.ContainsKey(configurationFile)
  16:                                     ? _currentSessions[configurationFile]
  17:                                     : null;
  18:             if (currentSession == null || !currentSession.IsOpen)
  19:             {
  20:                 _currentSessions[configurationFile] = OpenSession(factory);
  21:             }
  22: 
  23:             return _currentSessions[configurationFile];
  24:         }

Patch/solution

The problem I identified is line #5. It returns the existing session based on the default configuration, no matter what you specify in the configurationFile parameter.

I changed it to:

   1: var session = GetExistingWebSession(configurationFile);

 

And tadaaa:

image

The second repository now uses the second configuration file :)

Conclusion

You might ask, why I spend time writing a blog post describing this little change to the HybridSessionBuilder.

First of all, having a HSB that can use multiple configurations is rather important to me and the projects I am working on.

Second of all, as NHibernate and Castle Windsor is still a bit new to me, one of the best ways to learn its capabilities and how to use it, is to dig in deep. And that is exactly what I have done.

And BTW – I have not just learned about HSB, but also CodeCampServer, Tarantino, S#arp and how it handles multiple configurations, and how it all works together.

Posted in Development. Tags: , . No Comments »

Multiple Configurations for NHibernate (HybridSessionBuilder)

 

I have been using Castle Windsor and NHibernate for some months now, so I am a bit new and still learning. But then again, it is learning developers like me, that asks all the questions.

This post is also written because of a simple question: Does the HybridSessionBuilder support multiple configurations?

Why this question? Well, I have a project where some repositories uses one database and some other uses another database.

Original implementation

My little journey started out with my looking original implementation of the HybridSessionBuilder (HSB). You can read about the original implementation here:

http://jeffreypalermo.com/blog/use-this-nhibernate-wrapper-to-keep-your-repository-classes-simple/

But as you can see:image

The original implementation used a static session factory and session, so this is does not support multiple configurations.

CodeCampServer/Tarantino

I then found out that the CodeCampServer also was using the HSB. Hoping to find an implementation that supported my request, I then started to read the source code.

But by viewing the source code for CodeCampServer I soon found out that the code, for the HSB, had been updated to support multiple factories and sessions. Yeeeaaaaa :)

Furthermore I found out that the HSB now is a part of a collection of libraries called Tarantino.

I was happy, the HybridSessionBuilder now supported my requests, and it was ready to be used in my projects. I thought.

Test web application

image Then I build a small test web application project. It used Castle Windsor (for dependency injection) and NHibernate. I quickly configured Castle Windsor and made two NHibernate configuration files.

nhibernate.cfg.xml used this connection string:

Data Source=localhost\sqlexpress;Initial Catalog=DB1;Integrated Security=true

nhibernate2.cfg.xml used this connection string:

Data Source=localhost\sqlexpress;Initial Catalog=DB2;Integrated Security=true

My windsor.config.xml looks like this:

<component id="firstRepository" type="HybridSessionBuilderTest.FirstRepository, HybridSessionBuilderTest">
  <parameters>
    <ConfigurationFile>hibernate.cfg.xml</ConfigurationFile>
  </parameters>
</component>
 
<component id="secondRepository" type="HybridSessionBuilderTest.SecondRepository, HybridSessionBuilderTest">
  <parameters>
    <ConfigurationFile>hibernate2.cfg.xml</ConfigurationFile>
  </parameters>
</component>

It basically just configures the two repositories with each own configuration.

But to my surprise, it did not work. I made a small page that prints the connection string of each of the repositories:

image

Only the default NHibernate configuration file was used.

Bug or feature?

That was not the output I was looking for, so to find the problem, I did some more test and dug into the code.

I was expecting an object structure like this:

image

But I got something like this:

image  I identified the following method to be the central, when the session was instantiated:

   1: private ISession getExistingOrNewSession(ISessionFactory factory, string configurationFile)
   2:         {
   3:             if (HttpContext.Current != null)
   4:             {
   5:                 var session = GetExistingWebSession();
   6:  
   7:                 if (session == null || !session.IsOpen)
   8:                 {
   9:                     session = openSessionAndAddToContext(factory, configurationFile);
  10:                 }
  11:  
  12:                 return session;
  13:             }
  14:  
  15:             var currentSession = _currentSessions.ContainsKey(configurationFile)
  16:                                     ? _currentSessions[configurationFile]
  17:                                     : null;
  18:             if (currentSession == null || !currentSession.IsOpen)
  19:             {
  20:                 _currentSessions[configurationFile] = OpenSession(factory);
  21:             }
  22:  
  23:             return _currentSessions[configurationFile];
  24:         }

Patch/solution

The problem I identified is line #5. It returns the existing session based on the default configuration, no matter what you specify in the configurationFile parameter.

I changed it to:

   1: var session = GetExistingWebSession(configurationFile);

 

And tadaaa:

image

The second repository now uses the second configuration file :)

Conclusion

You might ask, why I spend time writing a blog post describing this little change to the HybridSessionBuilder.

First of all, having a HSB that can use multiple configurations is rather important to me and the projects I am working on.

Second of all, as NHibernate and Castle Windsor is still a bit new to me, one of the best ways to learn its capabilities and how to use it, is to dig in deep. And that is exactly what I have done.

And BTW – I have not just learned about HSB, but also CodeCampServer, Tarantino, S#arp and how it handles multiple configurations, and how it all works together.

Posted in Development. Tags: , , , . No Comments »

Publish a website application using MSBuild

MSBuild is soooo nice when it comes to automating compilation, testing, deployment or some other tedious task.

Until now, I believed that publishing a website application was a manual task, performed from Visual Studio.

But it is actually possible to perform publishing from the command line. Take a look at this Build.xml file:

<?xml version="1.0" encoding="utf-8" ?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5">
  <PropertyGroup>
 
    <OutputFolder>$(MSBuildProjectDirectory)\..\Published\</OutputFolder>
 
    <LibDir>..\Lib\</LibDir>
 
  </PropertyGroup>
 
  <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
  <Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v9.0\WebApplications\Microsoft.WebApplication.targets" />
  <Import Project="$(LibDir)\msbuild\MSBuild.Community.Tasks.Targets"/>
 
 
  <Target Name="Publish">
    <RemoveDir Directories="$(OutputFolder)"
               ContinueOnError="true" />
    
    <MSBuild Projects="..\Core\bmv3.org.oxweb.csproj"
             Targets="ResolveReferences; _CopyWebApplication"
             Properties="WebProjectOutputDir=$(OutputFolder); OutDir=$(WebProjectOutputDir)\" />
  </Target>
 
</Project>

 

If you want to read more about using MSBuild, then take a look at this tutorial:

http://codingcockerel.co.uk/2008/04/15/automating-the-build-with-msbuild/

Are you interested in reading more about the publishing using MSBuild, then read here:

http://codingcockerel.co.uk/2008/05/18/how-to-publish-a-web-site-with-msbuild/

http://blog.m.jedynak.pl/2008/03/publishing-web-application-with-msbuild.html

NHibernate – many-to-many relationship with data attached

I just found a good blog post about the may-to-many relationship in NHibernate.

The configuration sis done in Fluent NHibernate.

http://codebetter.com/blogs/kyle.baley/archive/2008/12/24/many-to-many-relationships-with-data-attached-in-nhibernate.aspx

Posted in Development. Tags: , , , . No Comments »

How will you parallelize your existing codebase? Try R.A.S.P

Original post:

http://www.atalasoft.com/cs/blogs/rickm/archive/2008/12/23/how-will-you-parallelize-your-existing-codebase-try-r-a-s-p.aspx

———————————————————————————————————————-

How will you parallelize your existing codebase? Try R.A.S.P.

There
has been much talk of how we will be writing all of our new code with
parallelization in mind.  However, what of our existing code?  It’s
unlikely that everyone will just suddenly dump decades of existing code
and write everything from scratch.  In this article I’m going to
provide a simple methodology for how we might deal with the ever
building problem of parallelizing our existing mountains of code. 
Comments and contributions are welcome.

 

Methodologies of the Past

From
the STL to .NET, the frameworks we have constructed our applications
around have been heavily dependent on the idea of an application having
a single thread.  Given that the foundation of what we have all been
using for a very long time was constructed around this preconception,
it’s unreasonable to expect that much of our existing code will ever be
fully parallel.  Even those that wrote code on top of a thread safe
framework may find that years of patches and and poor design decisions
make ground up parallelization impossible.

If we set our
expectations reasonably, we see that we should instead focus on
leveraging parallelism to improve the performance of the slowest parts
of our software.  From this viewpoint, parallelization is an
optimization problem.  Like all optimization, the difficulty of
parallelizing code will have much to do with the methodologies which
were used to write it.  

Of course, object-oriented code being written with modern S.O.L.I.D. principles
and will be easier to parallelize than older procedural code.  At the
same time, a poorly organized codebase or poorly written code will
always make change difficult and so also hard to parallelize.  This is
why well written code is worth the investment.  We will see the
investment paying off in spades for the companies who have bothered to
care about code quality.  Others who find they have spaghetti code
under the hood will find they will need to deeply segregate and
modularize before parallelization is possible.

 

A Methodology for Revisiting the Past

In
most cases it would be a poor choice to implement your own threading
API.  Efficient and easy to use parallelization APIs are coming to (or
already part of) every commonly used language and framework.  Most of
these APIs are not only built on top of years of research,
they also have been written and debugged by a large number of people
with specific expertise.  These APIs are a godsend because they will
allow most developers to parallelize existing software with a minimum
amount of pain.  The parallelization of existing code bases will be
much the same as any other kind of performance tuning. 

The
key will be using a profiler to identify places in the code that would
be sped up by parallelization and leveraging these new APIs to take
advantage of the available hardware.  The exciting part is that this
can be done with any existing profiler and many existing APIs.  The
unfortunate part is that because memory sharing is such a big issue,
parallelization requires a degree of separation beyond other types of
optimization and so is likely to require some amount of refactoring.

Not
all types of performance problems are conducive to being solved by
parallelization, careful evaluation of the problem at hand is
required.  Also, as with anything that requires significant code
change, building a solid test fixture is key to introducing as few bugs
as possible.  By leveraging the ideas of avoiding premature optimization, pragmatic unit testing, using existing APIs, and mindful refactoring it will be possible to introduce parallelization into many already existing projects with a manageable amount of risk.

 

What is RASP?

While not included in the acronym, the first step in any kind of optimization is profiling
Before you can begin to parallelize your code, you must determine where
the bottlenecks might be.  A broadly defined list of parallelizable
things to look for would be, to quote Rich Hickey,
“independent data/work, moderate-to-course-grained work units and/or
complex coordination logic that would be simplified with threads”.  A
couple quick examples of low hanging fruit to be on the lookout for
would be slow iterative loops and blocking I/O.  It is important to
note that as a general guideline it would be wrong to parallelize
anything if it would not significantly increase the speed of your
software.

For each of the bottlenecks found while profiling, parallelization is best separated into four steps:

Review: Review code to determine if it is a good candidate for parallelization.
Anchor: Create a unit test fixture to ensure that the behavior of the to be parallelized code does not change.
Separate: Ensure that the to be parallelized code has no shared memory constraints, and if it does, factor them out if possible.
Parallelize: Minimally refactor for parallelization while leveraging an existing API to do the heavy lifting.

As
the specifics of what each of these would entail depends greatly on
exactly which platform and language is in use, I will not go into them
deeply now.  Overall, it’s a simple methodology but I think both
sufficient for the task at hand and broadly applicable. 

 

Conclusion

Review,
Anchor, Separate, Parallelize.  It’s not intended to be a difficult
concept but instead to provide a simple path to parallelization.  I
would be very interested in hearing about any opinions on what RASP
might be missing or how it may be better clarified.  While I didn’t
have time to discuss them deeply in this post, parallelization patterns
are also a key concept in using RASP as if you can’t easily identify
what can be parallelized than it would be impossible to use any
parallelization methodology.  In the future I hope flush out RASP
further as well as discuss parallelization patterns in depth.

Create Word Documents using .NET without Interop

Original blog post:

http://www.invoke.co.nz/products/docx.aspx

————————————————————————————–

We have decided to release another component we've been using (FREE of charge, click here to download).
This library creates Word Documents (.docx) using .NET. It is written
purely in C#, you don't need any Word viewing applications or Interop
(COM) dlls installed/registered.

It works very similar to a
Repeater control, instead of HTML markup you create a docx template
using Word 2007 (Open XML format), specify parameters/placeholders to
hold values then pass the document to the library, it will then merge
those fields.

Here's an example:
To create a Word (.docx document)
- Open up Word 2007, create a new file and type in the following (without the " "):
"Hello %NAME%"
- Save & Exit (save as hello.docx)

- Create a new VS 2008 Console Application project, add a reference to the docx library, copy paste the code into Program.cs

static void Main(string[] args)
{
    File.Copy("hello.docx", "hello_ready.docx", true); // create a copy of template file for later use
    // DocumentDataSource holds name/value pair data
    DocumentDataSource source = new DocumentDataSource();
 
    source["NAME"] = "Joe Bloggs"; // assign a value to parameter %NAME%
    DocumentRenderer.ProcessDocument("hello_ready.docx", source); // process the document
 
    Process.Start("hello_ready.docx"); // run MS Word to see merged document
}

Result
Create a Word Document using C# without Interop (COM)

That's an over simplified example, the library is able to do a lot more, if you'd like to download it or find out more please click here,

Posted in Development. Tags: , , , , . No Comments »

Ayende explains cascading with NHibernate

To continue my little "post with good links"… here is a good article by Ayende, explaining cascadin with NHibernate:

http://ayende.com/Blog/archive/2006/12/02/NHibernateCascadesTheDifferentBetweenAllAlldeleteorphansAndSaveupdate.aspx

Here is the basics:

Here is what each cascade option means:

  • none – do not do any cascades, let the users handles them by themselves.
  • save-update – when the object is saved/updated, check the assoications and save/update any object that require it (including save/update the assoications in many-to-many scenario).
  • delete – when the object is deleted, delete all the objects in the assoication.
  • delete-orphans – when the object is deleted, delete all the objects in the assoication. In addition to that, when an object is removed from the assoication and not assoicated with another object (orphaned), also delete it.
  • all – when an object is save/update/delete, check the assoications and save/update/delete all the objects found.
  • all-delete-orhpans – when an object is save/update/delete, check the assoications and save/update/delete all the objects found. In additional to that, when an object is removed from the assoication and not assoicated with another object (orphaned), also delete it.

 

Posted in Software. Tags: , , , , . No Comments »