Friday, 29 August 2014

Mapping DataSet with ASP.Net MVC Model

In ASP.Net MVC we suppose to use an Entity framework to deal with database part.

During same there is no issue with mapping a model with database table. Will use DbSet which map the appropriate columns a with model property.

But when we suppose to use plan ADO.Net classes or like a ORM lite libraries then there is a problem with mapping a database table with your model.

To deal with this specific situation I came with a following generic class which will map a DataSet with given model.

For the same use add following class into you project.

public static class CollectionExtensions
    {
        public static IList<T> ConvertTo<T>(this DataTable table)
        {
            if (table == null)
            {
                return null;
            }

            List<DataRow> rows = new List<DataRow>();

            foreach (DataRow row in table.Rows)
            {
                rows.Add(row);
            }

            return ConvertTo<T>(rows);
        }


        public static IList<T> ConvertTo<T>(IList<DataRow> rows)
        {
            IList<T> list = null;

            if (rows != null)
            {
                list = new List<T>();

                foreach (DataRow row in rows)
                {
                    T item = CreateItem<T>(row);
                    list.Add(item);
                }
            }

            return list;
        }

        public static T CreateItem<T>(this DataRow row)
        {
            T obj = default(T);
            if (row != null)
            {
                obj = Activator.CreateInstance<T>();

                foreach (DataColumn column in row.Table.Columns)
                {
                    PropertyInfo prop = obj.GetType().GetProperty(column.ColumnName);
                    try
                    {
                        object value = row[column.ColumnName];
                        if (value != DBNull.Value)
                        {
                            prop.SetValue(obj, value, null);
                        }
                    }
                    catch
                    {
                        // You can log something here
                        throw;
                    }
                }
            }
            return obj;
        }
    }

To access above method into you controller you need to write following line of code which return your model type.

e.g: To map a single row

     DataSet dbSet = "select * from customer  where custmername = 'abc' ";

     Customer objCustomer = dbSet.Table[0].Rows[0].CreateItem<Customer>();

     This code will take a datarow and map it with your Customer model.


e.g: To map a multiple row

     DataSet dbSet = "select * from customer";

     List<Customer> objCustomer = dbSet.Table[0].ConvertTo<Customer>();

     This code will take a List of datarow and map it to your list of Customer model.



Monday, 18 March 2013

A Brief Introduction to the Android

Hello Friends,
This is my first post In the blog "Beginning of Android Development", Here I will tell you some introductory part of Android.
1.What Android Is..?
Android defined as a software stack, this stack includes Operating System(modified version of Linux kernel),
Middleware(software that connects the low-level operating system to high-level apps), and key Apps(written in Java) such as web browser and contact  manager.
2. What kind of Architecture it has..?
The Android software stack consist of Apps at the top, Middlware(consisting of application framework, libraries, and Android run-time) in the middle, and Linux kernel with various drivers at the bottom

3. What Activity Is..?
In Android every thing is done in Activity. Activity is a component that present a user interface screen with which the user interact.
4.Example-
Here I given the simple example of Hello Android, It has two important file one with the extension of .xml which contains code of layout(user interface), and other is .java contains the code implementation. 
//main_activity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello Android" 
        android:textSize="20sp"
        android:gravity="center"/>
</RelativeLayout>
//MainActivity.java
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
//Output

Next post will release in the next week.

Thank You,