HwyFwk: Dynamic Database Connection String

In Highway.Data you must specify the connection string to be able to access the database.  Use the following approach to allow you to specify the connection string in the web.config and utilize Web Config Transformations to target the correct database for each environment.

Modify the Install method in /Installer/HighwayDataInstaller.cs

   1:  public void Install(IWindsorContainer container, IConfigurationStore store)
   2:  {
   3:      container.Register(
   4:          Component.For<IDataContext>().ImplementedBy<HighwayDataContext>()
   5:              .DependsOn(new
   6:              {
   7:                  connectionString = @"Data Source=.;Initial Catalog=ChangeMyConnectionString;Integrated Security=SSPI;"
   8:              })
   9:              .LifestylePerWebRequest(),
  10:          ...
  11:          );
  12:  }

To

   1:  public void Install(IWindsorContainer container, IConfigurationStore store)
   2:  {
   3:      container.Register(
   4:          Component.For<IDataContext>().ImplementedBy<HighwayDataContext>()
   5:              .DependsOn(new
   6:              {
   7:                  connectionString = ConfigurationManager.ConnectionStrings["KEY"].ConnectionString
   8:              })
   9:              ...
  10:          );
  11:  }

By using the ConfigurationManager you can specify which connection string from the web.config to use.  Now just set up the Web Config transformations for each environment you deploy to and your are good to go.

HwyFwk: Disable Database Initializer for Database First Approach

Highway.Data configures a Database Initializer out of the box.  Unfortunately where I work does not allow developers to create databases on our development data server.  We usually start our development with an already formed database.

To disable migrations do the following files:

  1. Delete /App_Start/DatabaseInitializerWireup.cs
  2. Delete /Config/HighwayDatabaseInitializer.cs
  3. Edit /Installers/HighwayDataInstaller.cs and remove the registration for IDatabaseInitializer

There you go, Highway.Data will not try to drop and re-create your database now.

MVC, Ajax and Relative URLs to Controller Actions

Today I needed to change the virtual directory my current project uses to more accurately reflect it purpose.  The application uses Ajax calls to post information back to a MVC Controller Action.  It has always bugged me that I had to put the virtual directory name in the Ajax URL parameter.  So today I set out to solve that problem.

Here is what my original code looked like:

$.ajax({
     url: '/MyVirtualDirectory/Controller/Action',
     type: 'POST',
     dataType: 'json',
     ....
})

First thing was to generate the URLs to the Controller Actions.  Using the Url.Action method I was able to generate a fully qualified URL to an action method. 

$.ajax({
     url: '@Url.Action("actionName", "controllerName")'',
     type: 'POST',
     dataType: 'json',
     ....
})

Since some of my ajax calls were in separate JavaScript (.js) files I decided add the following script section before any other script blocks on the page.

<script type="text/javascript">
    var myUrl = '@Url.Action("actionName", "controllerName")';
</script>

Now my ajax call looks like this:

$.ajax({
     url: myUrl,
     type: 'POST',
     dataType: 'json',
     ....
})

 

This provided a very simple straight forward way to allow my virtual directory name to change and not have to modify the ajax calls.  Thanks to many of the Stack Overflow posters especially dknaack for help me come to this solution.

System Performance Tweaks #2– Delete Pagefile.sys in Windows 7

I have 16GB of memory on my current laptop and recently discovered that it never seems to use much of it, but swaps to the hard drive like crazy. After investigating I found two things that have improved my performance this is one of them. I am also looking at having my work laptop replaced so I wanted to get this documented somewhere that was not going to be lost.

The pagefile.sys is the virtual memory file the system uses to cache data. This file can become very large. Whenever you don’t have enough physical RAM the system can use the virtual RAM to speed up the current process. So, the virtual memory is quite essential for a fast system and it is not recommended to disable it. Even on systems with a lot of RAM you will need this file badly. Actually, the more physical RAM you have, the more virtual memory you should have. As a rule of thumb, you should always have 1.5 times more virtual memory than physical RAM, recommended is 2 times more.

Deleting the pagefile.sys is not a good solution and you should only do it if you need disk space urgently

Disable/Delete Pagefile.sys

  1. Open Control Panel
  2. Click on System and Security
  3. Click on the System header
  4. Click “Advanced system settings” on the sidebar
  5. On the Advanced Tab in the Performance section click Settings…
  6. On the Performance Options dialog click Advanced
  7. Click the change button

image

Final steps

At this point what you do may vary depending what you want the outcome to be.  First uncheck “Automatically manage paging file size of all drives”.  As you can see I opted to keep the pagefile.sys file but make it very small, the other option is to operate without a pagefile.sys file by choosing the “No paging file” option.

System Performance Tweaks #1– Disable Hibernation

I have 16GB of memory on my current laptop and recently discovered that it never seems to use much of it, but swaps to the hard drive like crazy.  After investigating I found two things that have improved my performance this is one of them.  I am also looking at having my work laptop replaced so I wanted to get this documented somewhere that was not going to be lost.

Disable Hibernation

Hibernation uses a lot of disk space to store system state so it can be recovered.  The sleep mode does what I want, but I couldn’t figure out how to get rid of the Hibernate file (hiberfil.sys).  You can delete the hiberfile.sys but it will just come back so you need to disable the Windows Hibernation function.

Disable Hibernation GUI

The Hibernate settings are stored under the Control Panel’s Power Options applet, but they are buried under each power plan’s advanced power settings submenu.

http://i.techrepublic.com.com/blogs/win7_power_options_03.png?tag=siu-container;attachment_1496

Disable Hibernation – Command Line

This is the only guaranteed way to disable Hiberation on Windows Vista, and Windows 7.

  1. Open the command prompt with administrative privileges
  2. Enter “powercfg.exe –h off”
  3. Exit the command prompt

http://i.techrepublic.com.com/blogs/powercfg_01.png?tag=siu-container;attachment_1503

 

Re-enable Hibernation

To re-enable Hibernation, check the Enable Hibernation in the GUI or enter “powercfg.exe –h on” in a command prompt with administrative privileges.

Calendar

<<  July 2014  >>
MonTueWedThuFriSatSun
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

View posts in large calendar

Month List

AuthorList