Ramesh Rajendran


C# dotnet solutions
Ramesh Rajendran
Provides solutions and information on ASP.NET,VB.NET,C#,JavaScript,XML,Web Services,SQL Server,WPF,WCF,Silverlight,MVC,Jqury,Ajax,Html.

Thursday, 20 March 2014

Dependency Injection in MVC , Web Api

Dependency Injection :


What is this? Why we use this?

feel free and learn from Google, Many peoples are write more articles for there

See,

Explains from Code project

Explains from MSDN


Now we'll see, How to implement this in our local application?

First create a class and interface and Implement the interface into the class

public class CompanyService : ICompanyService
    {
        public AppContext _salesAppContext = new AppContext();    // this is object reference   for your entity 

        public IList<Company> GetAll()

        {
            return _salesAppContext.Companies.ToList();
        }

        public Company Create(Company model)

        {
            throw new NotImplementedException();
        }

        public Company Update(Company model)

        {
            throw new NotImplementedException();
        }

        public Company Delete(Company model)

        {
            throw new NotImplementedException();
        }

        public Company Get(int modelId)

        {
            throw new NotImplementedException();
        }

        public Company Get(Guid modelId)

        {
            throw new NotImplementedException();
        }
    }

Interface


 public interface ICompanyService
    {
        TModel Create(TModel model);
        TModel Update(TModel model);
        TModel Delete(TModel model);
        TModel Get(int modelId);
        TModel Get(Guid modelId);
        IList<TModel> GetAll();
    }

Okay, once it's fine . Then you need to add a Nuget package named as  "NUnit" . and  add the below methods in your global.asax file

public static void Initialise()
        {
            var container = BuildUnityContainer();

            // DependencyResolver.SetResolver(new UnityDependencyResolver(container));

            // register dependency resolver for WebAPI RC
            GlobalConfiguration.Configuration.DependencyResolver = new Unity.WebApi.UnityDependencyResolver(container);
            // if you still use the beta version - change above line to:
            //GlobalConfiguration.Configuration.ServiceResolver.SetResolver(new Unity.WebApi.UnityDependencyResolver(container));
        }



        private static IUnityContainer BuildUnityContainer()

        {
            var container = new UnityContainer();
            container.RegisterType<ICompanyService, CompanyService>();
            return container;
        }

and also you must add this namespace

using Microsoft.Practices.Unity;


Now the sample configurations are done. Then you need to call the Initialise() method into the Application_Start()  Method 

That Look like 

  protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            //  Database.SetInitializer<SalesAppContext>(null);
            GlobalConfiguration.Configuration.MessageHandlers.Add(new CorsHandler());
            Initialise();

        }
Done.

Monday, 4 November 2013

AngularJS Simple Sample

I already gave some information about angularJs here :


First you surely need to read the above article , then come here ......


Okay cool ! Now we will go to build simple one angularJs Application .


My sample application created for Web-application , But you can do any type of application(windows,MVC Project) ,etc......

First add one index page in my application  and you need to just copy past the below code but , I will explain further details .

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Index.aspx.cs" Inherits="Index" %>

<!DOCTYPE html>

<html ng-app>
<head runat="server">
    <title></title>
    <script src="Scripts/angular.min.js"></script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <label>Name:</label>
            <input type="text" ng-model="Name" placeholder="Enter a name here">
            <label>{{Name}}</label>           
        </div>
    </form>
</body>
</html>




The Above Green color codes are must important ..

where..

ng-app  - This keyword use for  Define Angular Js .

 <script src="Scripts/angular.min.js"></script> - Call angularJs in the Current Page .

 ng-model="Name"  - This is AngularJS Model Property (Just assume MVC Model property)

{{Name}} - Binding the model property value in a label . (Just Assume pass the MVC Model Property values to view)


Results are :

If you type any value in the textbox , It's display the below label( Like OnTextChanged Event process)

Okay ! Now you can create simple  angularJS application on yourself ,


AngularJS tutorial

What Is AngularJS ?

AngularJS is a Javascript MVC framework created by Google to build properly architectured and maintenable web applications.

Why AngularJS?

AngularJS is a MVC framework that defines numerous concepts to properly organize your web application. Your application is defined with modules that can depend from one to the others. It enhances HTML by attaching directives to your pages with new attributes or tags and expressions in order to define very powerful templates directly in your HTML. It also encapsulates the behavior of your application in controllers which are instanciated thanks to dependency injection. Thanks to the use of dependency injection, AngularJS helps you structure and test your Javascript code very easily. Finally, utility code can easily be factorized into services that can be injected in your controllers. Now let’s have a closer look at all those features.


Build Simple AngularJs Application :

First You need to Install AngularJs Package in your application .It's not a big work , Just simple go and right click the Project In Solution explorer>Click  Manage NuGet Package  >Select Online tag > type AngularJs In the Search box >Then Download and install the AngularJs .

See this below Image for install angularJs in the Project 


If you done this process then you can see some javascript files in your Script Folder 

See this below Image 


Okay now we are finished of angularJs installation in our application . 


Now We will go to Build simple angularJs application (click the link )....... 


Tuesday, 6 August 2013

Add values to SelectList in MVC C#

 SelectList adequateList = new SelectList(new[]
             {
                new { Id = "1", Name = "Yes" },
                new { Id = "0", Name = "No" },       
             }, "Id", "Name");

Tuesday, 25 June 2013

How to find single value from string in c#

         You can use this code for  find single value from string


        const string stringValue = "1,16,12,113";
            string result = "";
            const string yourValue = "1"; //Assiged the user input value
            string[] arr = stringValue.Split(new char[] { ',' });//Convert string to string array
            foreach (string value in arr)
            {
                if (value == "" + yourValue + "") // condition checked for matching your value  in string array
                {
                    result = value; //assign the matched value
                    break; // then go to out side of for each
                }
            }
            string finalResult = result; //finally assigend the matching value


My above code is working fine .but i used foreach ,it's not a good performance ,so we can use findall
see below code and try this

const string stringValue = "1,16,12,113";
string finalValue = "";
const string yourValue = "1"; //Assiged the user input value
string[] arr = stringValue.Split(new char[] { ',' });//Convert string to string array
var results = Array.FindAll(arr, s => s.Equals(yourValue));//if your value there ,then we assigned that value in array
if (results.Length != 0)
    {
      finalValue = results[0];//finay we got a value
    }

Convert string to string array in c#

here is a simple example for convert string to string array


            string values= "1,16,12,113";
            string[] arr = values.Split(new char[] { ',' }); //Convert string to string array using split 

Monday, 24 June 2013

How to create a simple WPF application

In Visual Studio 2008 OR any 


  • Open Visual Studio 2008 and choose "File", "New", "Project..." in the main menu. Choose "WPF Application" as project type.
  • Choose a folder for your project and give it a name. Then press "OK"


  • Visual Studio creates the project and automatically adds some files to the solution. A Window1.xaml and an App.xaml. The structure looks quite similar to WinForms, except that the Window1.designer.cs file is no longer code but it's now declared in XAML as Window1.xaml


  • Select the Button and switch to the event view in the properties window (click on the little yellow lightning icon). Doubleclick on the "Click" event to create a method in the codebehind that is called, when the user clicks on the button.

Note: If you do not find a yellow lightning icon, you need to install the Service Pack 1 for VisualStudio on your machine. Alternatively you can doubleclick on the button in the designer to achieve the same result.

  • Visual Studio automatically creates a method in the code-behind file that gets called when the button is clicked.
private void button1_Click(object sender, RoutedEventArgs e)
{
    textBox1.Text = "Hello WPF!";
}


  • The textbox has automatically become assigned the name textBox1 by the WPF designer. Set text Text to "Hello WPF!" when the button gets clicked and we are done! Start the application by hit [F5] on your keyboard.