ModelDataSource Nuget Package

This package is designed for ASP.Net Web Forms 4.0. It contains ModelDataSource control, which allows the control of CRUD operations via Page events, which mimics the model binding features in ASP.Net 4.5. ModelDataSource control extends the QueryableDataSource, just like the LinqDataSource.

The control allows the separation of concerns, instade of wiring the data context in the markup, which ties the View to the Data Storage, you use events to make the proper data oprations, Select, Insert, Update, and Delete. It's better than ObjectDataSource as the select operation uses IQueryable collections where the data control do the rest of Filtering, Grouping, and Sorting.

The only required event is the Selecting event where the page returns an IQueryable<T> object, then the control will handle the rest of Filtering, Grouping, and Sorting. The ItemType Property defines the Model Type for Designer.

ModelDataSource control have the same properties as the LinqDataSource and the EntityDataSource, including:

  • EnableDelete, EnableInsert, EnableUpdate

  • Where, WhereParameters, and AutoGenerateWhereClause

  • OrderBy, OrderByParameters, and AutoGenerateOrderByClause

  • GroupBy and GroupBy Parameters

  • Select and SelectParameters

  • and more inherited from QueryableDataSource

The ModelDataSource Designer supports smart tag with quick options to set the ItemType, Enable Edit Operations, and to provide sample design time data for data bound controls.

Example, When Dragging the control and setting the events the markup will be as the follwing:

<cc1:modeldatasource id="MyDataSource" enableupdate="True" enableinsert="True" enabledelete="True" 
onupdating="MyDataSource_Updating" oninserting="MyDataSource_Inserting" ondeleting="MyDataSource_Deleting"
onselecting="MyDataSource_Selecting" itemtype="Models.Product" runat="server">
</cc1:modeldatasource>

You can add Where, GroupBy, OrderBy Expressions as in LinqDataSource. In code behind, we can use any code for data access in event handlers.

For example, assume we have a IProductRepository, with Select, Insert, Update, Delete methods. A variable of that interface can be initialized on Page_Load event or we can get it from an IoC Container. Page Events can be as the following:

    private IProductRepository pRep;
protected void Page_Load(object sender, EventArgs e)
{
pRep = new ProductRepository();
}
protected void MyDataSource_Selecting(object sender, ModelDataSourceSelectEventArgs e)
{
var products = pRep.Select();
e.Result = products;
}
protected void MyDataSource_Inserting(object sender, ModelDataSourceInsertEventArgs e)
{
var p = e.NewObject as Product;
pRep.Insert(p);
}
protected void MyDataSource_Deleting(object sender, ModelDataSourceDeleteEventArgs e)
{
var p = e.OriginalObject as Product;
pRep.Delete(p);
}
protected void MyDataSource_Updating(object sender, ModelDataSourceUpdateEventArgs e)
{
var p = e.NewObject as Product;
pRep.Update(p);
}

Now we can bind that Datasource to any Data-bound Control like ListView and Grid View.

Again, If you use ASP.Net 4.5, you will not need this data source control, as model binding is integrated in data controls, but if you find yourself targeting ASP.Net 4.0, your servers still in Windows Server 2003 for example, you still can do Model-Driven Design and separate data access from user interface.

Also, you can now refactor your application layers now for Model-Driven Design, then only change the Web From code when upgrading to 4.5.

For More Infromation, you can create a feedback request and our team will contact you.