Monthly Archives: August 2013

Writing your first NHibernate Application

This tutorial writing your first nhibernate is a good tutorial but it misses out certain steps. Here are the missing steps.

1. In shareLib folder, copy Nhibernate (download first) and NUnit(download first). Then add references to nhibernate.dll and nunit.framework.dll to your project.

2. It does not specifically talk about SQL Server Compact Driver. The reference can be found in “Assemblies”. Type SQL and look for assembly System.Data.SqlServerCE at the bottom.

3. The parameters to new SchemaExport(cfg).Execute(false, true, false, false); are wrong. It should be 3 parameters.

Error loading player: Offline playback not supported : JWPlayer

Funny I did not get this error on my local html file with jw player, but when I copied it to another server, I got this error. I copied the whole directory so there wasn’t anything missing. I coped that directory (from Server) back to may local computer in a different folder, and I am again getting this error. This is quite strange.

Spring.NET XML Configuration File

A sample Spring.net configuration file looks like this


<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">

<object id="writer" type="Spring4.Writer,Spring4" singleton="false">
<propery name="Name" ref="Pen"/>
<propery name="Name" value="Pen"/>
</object>

<object id="writer" type="Spring4.Writer2,Spring4" singleton="false">
<constructor-arg index="0" value="Pen"/>
</object>

<object id="Pen" type="Spring4.Pen, Spring4" />
<object id="Pencil" type="Spring4.Pencil, Spring4" />
</objects>

Lets decompose it

It must start with <?xml version=”1.0″ encoding=”utf-8″ ?> because it is a standard xml file.

It must have xmlns=”http://www.springframework.net” namespace and possibly other if you need more features.

<object> refers to a C# class. It is same as bean in JavaWorld.

<propery> means C# property. I can take either value=”” argument, which is a string or a ref=”ID” which is an ID of another object in the XML. It is used for Setter Injection by Spring.NET

<constructor-arg> is used for Constructor Injection. It can also either take a value argument or a ref argument, just like the property. Value is a string and ref is ID of another object.

 

Spring.NET Constructor Injection Example

The following Console Application (C#) represents Constructor injection using Spring.NET. To use the code in as it is form, create a console application using C# in Visual Studio 2012 and name it Spring4. Copy the content of program file from below. Create another xml file named context.xml and add it to your project. Copy the contents of that file from what I have supplied. Last but not the least, download spring.net core from NUGET. You only need the Spring.net core, you can ignore the rest.

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Spring;
using Spring.Context.Support;

namespace Spring4
{
    class Program
    {
        /// <summary>
        /// This program demonstrates constructor injection using Spring.NET
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            var context = new XmlApplicationContext("../../context.xml");

            Writer wall = (Writer)context.GetObject("writer");
            wall.WriteMessage();

        }
    }

    public class WriteMessage
        {
            WriteMessage(IWritingDevice device)
            {
                Console.Write("I am writing with " + device.DeviceName());
            }
        }

        public class Writer
        {
            private IWritingDevice _device;
            Writer(IWritingDevice device)
            {
                _device = device;
            }

            public void WriteMessage()
            {
                Console.Write("I am writing with " + _device.DeviceName());
            }
        }

        interface IWritingDevice
        {
            string DeviceName();
        }

        public class Pen :IWritingDevice
        {
            public string DeviceName()
            {
                return "Fountain Pen";
            }
        }

        public class Pencil :IWritingDevice
        {
            public string DeviceName()
            {
                return "Grey Pencil";
            }
        }
}

Context.xml

<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">

  <object id="writer" type="Spring4.Writer,Spring4" singleton="false">
    <constructor-arg index="0" ref="Pen"/>
  </object>

  <object id="Pen" type="Spring4.Pen, Spring4" />
  <object id="Pencil" type="Spring4.Pencil, Spring4" />
</objects>

Explanation

In this example, I am using a writer Object to write something. But the writer object cannot write by itself. It needs a writing device to write with. It is required that is supplied the object in the constructor.

My Writer Object is dependent on writing device, which must implement IWritingDevice. We can pass anything to this class which is derived from IWritingDevice interface. In the given example, I trying to write with fountain Pen.

Fountain Pen or Simply Pen is passed to the constructor of Writer Object. This is configured in context.xml. Notice that you cannot just change the constructor argument on the fly, it is configured in XML file in Spring context and will always be the same.

Notice that no new kewyord is used. When Writer Object is created, Spring automatically knows that a IWritingDevice object is needed for Writer Object so it it can write with something.

Some useful links

Type of dependencies

Conditional Formatting in Excel – One column based on another

Conditional Formatting in Excel

Conditional Formatting in Excel

Match1 Match2
A B
D A
E E
F D
K K
Rule:	COUNTIF($B$2:$B$6,C2)

Read: If cell c2 value exists in the range B2:B6, then highlight the cells that I have selected in step 1.

The above will highlight All rows in column Match2 that exists in column Match1. In this case, A,E,D,K will be highlighted but B will not because it does not exist in Match1 column.

Procedure

1. Select cells range that you want to conditionally highlight, in this case B2:B7
2. Select Conditional Formatting, New Rule then select Formula
3. Enter the formula, =COUNTIF($B$2:$B$6,C2) (remember to put equal in formula)
4. Click format button and choose whatever formatting you choose.
5. Select ok when done. Your cells will be highlighted.

Points to be aware of

When enter cell C2, make sure you type it as it is. Do not select cell C2 with the mouse. If you do, that will hard code that particular cell and will not work with all the cells.

Enhancements

If you want to highlight the entire row in your selection, then simply prefix the cell you want to highlight by $. In the example you have two columns of data selected, instead of saying A2 for example you would enter $A2. This would be the second argument for the countif function.

How to ask questions at job?

You are new to the job and you didn’t how how something works. Or may be you were suppose to know something and you don’t know. How would you ask questions? Asking questions in bad way can jeopardize your career and possibly your job or at least it may tarnish your image.

Wrong of asking questions

How to compile the code?
I am assigned this bug, where to look for to resolve this issue/bug?
How do I connect to …
This stored procedure is giving me error …

Correct way of asking questions

I am trying to compile the code, but when I click on this it does not work.
I fixed this issue but when I look into the table, it shows the wrong value (old value in this case).
I am trying to connect to …
I am getting this strange error from this stored procedure …

Spring.net basic example

So Spring.NET is used for Dependency Injection among other things. Can you show us an example?

Spring.net Example

Create two class Rectangle and Triangle, both must be derived from Abstract Shape class which has a method call Draw(). If I create rectagle object, its draw method should pring, I am rectangle.

Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Spring;
using Spring.Context.Support;

namespace Spring2
{
    /// <summary>
    /// This is spring.net Console Application which demonstrates spring.net IOC and Instantiation of objects.
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            // If Spring.NET is not used
            Shape myshape1 = new Rectangle();
            Shape myshape2 = new Triangle();
            myshape1.Draw();
            myshape2.Draw();

            /* Now the same example in spring.net context */
            var context = new XmlApplicationContext("../../SpringSetup.xml");
            Shape myshape3 = (Triangle) context.GetObject("tri");
            myshape3.Draw();

        }
    }

    // All classes are part of Spring2 namespace
    public  class Shape
    {
        public virtual void Draw()
        {
        }
    }

    public class Rectangle: Shape
    {
        public override void Draw()
        {
 	         Console.WriteLine("I am rectangle");

        }
    }

    public class Triangle: Shape
    {
        public override void Draw()
        {
            Console.WriteLine("I am Triangle");
        }
    }
}

XML File Configuration

I have named as SpringSetup.xml

<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">
  <!--<object id="Sauce"
          type="Ploeh.Samples.MenuModel.SauceBéarnaise,
          Ploeh.Samples.MenuModel" />-->

  <object id="rect"
          type="Spring2.Rectangle,Spring2" />  

  <object id="tri"
          type="Spring2.Triangle,
          Spring2" />    
</objects>

Note that the above should be console C# Application. You must also include Spring.Core.DLL or installed Spring.Core from NUGET in your project.

Accessing Local Resources in MVC

You want to localized your resource or in other words put them in a resource file in the local folder, how do you access it in MVC?

Local resource demo

 

Here is how you will access it

    public class Student
    {
      // Constuctor
        public Student()
        {
            Name = MyStrings.name1;
        }

        public string Name { get; set; }
    }

A better way of doing it to put all resources in inside a separate folder and give them proper extension depending on where they will be used. For Example I can use
>>LocalizedString
>>>>>MyStrings.Resx
>>>>>Mystrings.en-GB.Resx

The program will automatically pick the right resource depending on the culture setting of the computer. In this case you will have to include using statement to point to the resource file.

using LocalResource.Models.LocalizedStrings;