Software Development

Post-build Event Command Line

How to write a command line argument in Post Build event in Visual Studio? To get to the setting, right click on project, select properties, then go to the third entry on left “Build Events”. Mostly like you will need to copy from one folder to another, you either hardcode the full path or use macros to accomplish the task. The macros can bee see on this Microsoft link

If the path of the project contains white spaces in directories, then use quotes to enclose the path name.

Example: copy “$(ProjectDir)SharedLib\Nhibernate\Required_Bins\sqlce*.dll” “$(ProjectDir)$(OutDir)”

Here

$(ProjectDir) = The directory of the project (full path)
$(OutDir) = Path to the output file directory, relative to the project directory

Useful regular expression in Visual Studio

This is for Visual Studio 2012. VS 2012 uses a more standard RegEx rather than earlier version. This may not work in earlier version of VS.

Match images tags

 Regular Expression: <imageurl>.*</imageUrl>

 

Remove blank lines from Code

Regular Expressoin: ^[space]*\r\n

Reqular Expression in SQL Server Management Studio

Append comma to all integers at start of the lines

123
456

Reqular Expresson: ^:z
Replace with : \0,

Make sure you select regular expression when using Find Window.

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

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;