Sunday, March 31, 2013

Multiple File upload with image preview in Asp.Net


In the last week I searched in the Google for multiple image upload with image preview in Asp.Net while I find some articles however it wasn’t satisfied me. Finally I got the Mr Sam article it was very amazing article. Although the example project was very confused to me. So I modified the project and Attached here
Thanks
Niventh@ abiruban 

Thursday, March 21, 2013

How to implement Dependency Injection in MVC



Introduction

In This article describes the, How to implement Dependency Injection in MVC. Dependency Injection is allows for easier TDD and Dependency Injection used avoid tight coupling.

How can we do this?  We do this in five steps.

Step1: Add Model class
Step2: Add data access class
Step3: Create UnityDepentacyReslover
Step4: Configure the Global.asax
Step5: Add Controller

Let's we create a simple MVC application named as DependencyInjectionPOC.

Step1: Add Model class

Let's we create a new model class name as "Student", it's a DTO (Data Transfer Object) class used to transfer data across multiple layers of an application. Just look at this code in bellow.

     public class Student
    {
        public int StudentId;
        public string StudentName;
    }

Step2: Add data access class

We create an Interface named as "IStudentRepository”. Interface can have multiple methods weather we have simply used only one method. In this time your mind has ask some question, why we would create Interface. We want swap the mock class (Used to define TDD) so only we used Interface.  How to swap the class?  I will be describe in to next blog


  public interface IStudentRepository
    {
        void AddStudents(Student student);
    }

Then we create a data access class named as "Student Repository". This class must be inherited into IStudentRepository Interface then we implement the Interface method in to "StudentRepository" class as shown in the following code

  public class StudentRepository : IStudentRepository
    {
        public void AddStudents(Student student)
        {
            List<Student> StudentList = new List<Student>();
            StudentList.Add(student);
        }
    }


Step3: Create UnityDepentacyReslover

Let's we create a class named as "UnityDepentacyReslover". This implementation will accept an IUnityContainer and will use this container for resolving the requested types. If the DependencyResolver is unable to resolve a type, it should always return null for the GetService method and an empty collection for the GetServices method. That is why a try catch is used in the UnityDependencyResolver.

   public class UnityDepentacyReslover : IDependencyResolver
    {
        IUnityContainer Container;

        public UnityDepentacyReslover(IUnityContainer Container)
        {
            this.Container = Container;
        }
        public object GetService(Type ServiceType)
        {
            try
            {
                return Container.Resolve(ServiceType);
            }
            catch
            {
                return null;
            }
        }
        public IEnumerable<object> GetServices(Type ServiceType)
        {
            try
            {
                return Container.ResolveAll(ServiceType);
            }
            catch
            {
                return null;
            }

        }
                       
    }

Step4: Configure the Global.asax

Let's we create a class name as "UnityConfig". This class is used to configure the UnityContainer.

  public class UnityConfig
    {
        public static void RegisterUnityContainer()
        {
           // A simple, extensible dependency injection container.
            UnityContainer container = new UnityContainer();
            //RegisterType a type mapping with the container, where the created instances
            container.RegisterType<IStudentRepository, StudentRepository>();
            DependencyResolver.SetResolver(new UnityDepentacyReslover(container));
        }
    }

Then call the above method into Global.asax , in the Application_Start method. Like

     protected void Application_Start()
        {
            UnityConfig.RegisterUnityContainer();
           
        }

Step5: Add Controller

Then we create a new controller named as "StudentController"

   public class StudentController : Controller
    {
        [Dependency]
        public IStudentRepository repository { get; set; }

        public ActionResult Index(Student student)
        {
            repository.AddStudents(student);
            return View();
        }

    }

Conclusion

This post is used to, How to implement Dependency Injection in MVC

Friday, March 15, 2013

Simple Windows 8 apps using JavaScript and Web API


Hi friends,

Day by day  micro soft introduced new technologies. one of the new technologies is windows 8 apps. In this blog we discussed about
how to create Windows Store apps using JavaScript and Web API

Step 1

Let's create a new app named "Love Calculator". Here's how:
From the File menu select New Project. Further steps shown in bellow image


















In this project we choose blank template then click OK button we get a new project .
and we need to another one  Web API project . So we would create a new Web API project, those project are  displays it in the Solution Explorer.


























Step 2:Modify the Web API

Let's we open the Web API and  create a "LoveCal" class. This class contains simple algorithm to calculate a love percentage.


















Then we have to call the love algorithm  method in to "Values Controller"



















We finished the API related codes then we move to windows app project.


Step 3:Modify the Design page

 When we created our new project, Visual Studio created a default.html file for us.We run the app it displays the content of its start page.
Let's add some new content to our default.html file. Replace the existing contents then we add a new two text boxes one is male name text box another one is female name text box and we add a button that name is check your love.






















Step 4:Modify the JavaScript page

When we created our new project, Visual Studio created a /js/default.js  file for us.This file contains code for handling your app's life cycle, Let's we modify the default.js and add our own code.
We need to write one ajax method "loveCal()" to call Web API . Then we need to  register the event handler with the button. How to register an event handler? just we used addEventListener method it is do the  register an event handler. A good place to register the event handler is when our app is activated.
Fortunately, Visual Studio generated some code for us in our default.js file that handles our app's activation
the app.onactivated event handler. Let's take a look at this code.






















Step 4: Running the app 

We finished the coding sections.Let's we choose the running environment










and run the project finally we got a new Love calculator app.




















I Am a also newbie to Windows 8 apps weather anything wrong in my blog extremely sorry my dear friends

Thank you
Abiruban


Sunday, March 10, 2013

Performance analyze for loop and foreach


Hi, Friends
In this blog we discussed about Performance analyze of for loop and foreach
Which one gives better performance? for or foreach 
just right two simple methods  
public static void TestFor()
        {
 
            Stopwatch stopwatch = Stopwatch.StartNew();
            int[] myInterger = new int[1];
            int total = 0;
            for (int i = 0; i < myInterger.Length; i++)
            {
                total += myInterger[i];
            }
            stopwatch.Stop();
            Console.WriteLine("for loop Time Elapsed={0}", stopwatch.Elapsed);
        }
        public static void TestForeach()
        {
            Stopwatch stopwatchForeach = Stopwatch.StartNew();
            int[] myInterger1 = new int[1];
            int totall = 0;
            foreach (int i in myInterger1)
            {
                totall += i;
            }
 
            stopwatchForeach.Stop();
            Console.WriteLine("foreach loop Time Elapsed={0}", stopwatchForeach.Elapsed);
        }
 
Then i ran the above code the result was foreach loop Time Elapsed=00:00:00.0000003,
for loop Time Elapsed= 00:00:00.0001462. i think we want high performance code. we would use foreach