Get Dataset returned from an ajax enabled wcf service - ajax

I call an ajax enabled wcf service method ,
<script type="text/javascript">
function GetEmployee() {
Service.GetEmployeeData('1','5',onGetDataSuccess);
}
function onGetDataSuccess(result) {
Iteratejsondata(result)
}
</script>
and my method is ,
[OperationContract]
public string GetEmployeeData(int currentPage,int pageSize)
{
DataSet ds = GetEmployeeViewData(currentPage,pageSize);
return GetJSONString(ds.Tables[0]);
}
My Dataset ds contains three datatable but i am using the first one for my records...
Other two datatables have values how can i get them in result...
function onGetDataSuccess(result) {
Iteratejsondata(result)
}
Any suggestion...

The only suggestion: do not use DataSets over WCF!
DataSets are evil - they're huge, they carry lots of overhead, the mix data with behavior - all things you should try to avoid like the plague when doing proper SOA. This is doubly true when you're doing Ajax calls asynchronously - you want to transfer as little data as possible using JSON - and having a DataSet with DataTables does not help you at all for your JSON calls...
So really : get yourself acquainted with some kind of an ORM, and grab objects and lists of objects and toss the DataSets onto the digital recycling heap.....
For a small project, if you're using SQL Server as your backend, why not use Linq-to-SQL? Or if that doesn't work for you, check out Subsonic

Related

How to add new chart(any charts, highcharts or d3 charts) in Serenity admin dashboard

I have created an application using Serenity framework. I have completed basic functionality for CRUD using serenity. Based on my tables I need to have graphical representations, any charts like high charts, D3 charts or any .
1. How can I get data from the tables using serenity framework ?
2. How can I customise the data into graphical representations ?
Finally I have found the answer for this. We can use sql queries as well as stored procedure to fetch data from DB. I have used stored procedure to get the data from db.
In repository page you can call stored procedure,
public ListResponse<MyRow> GetCustomData(IDbConnection connection)
{
var data = connection.Query<MyRow>("GetOrders",
param: new
{
startDate = request.nstartDate,
endDate = request.EndDate
},
commandType: System.Data.CommandType.StoredProcedure);
var response = new ListResponse<MyRow>();
response.Entities = (List<MyRow>)data;
return response;
}
I have already defined MyRow as OrderRow like this
using MyRow = Entities.OrderRow;.
You can call this method from your controller. You can pass the value to model and can use data for chart lik highcharts or d3 charts.
Hope this will help you.

In Meteor, where do I model my business rules?

Beginner question : I've worked through the Try Meteor tutorial. I've got fields in my HTML doc, backed by helper functions that reference collections, and BOOM --> the fields are updated when the data changes in the DB.
With the "Hide completed" checkbox, I've also seen data-binding to a session variable. The state of the checkbox is stored in the Session object by an event handler and BOOM --> the list view is updated "automatically" by its helper when this value changes. It seems a little odd to be assigning to a session object in a single page application.
Through all this, my js assigns nothing in global scope, I've created no objects, and I've mostly seen just pipeline code, getting values from one spot to another. The little conditional logic is sprayed about wherever it is needed.
THE QUESTION... Now I want to construct a model of my business data in javascript, modelling my business rules, and then bind html fields to this model. For example, I want to model a user, giving it an isVeryBusy property, and a rule that sets isVeryBusy=true if noTasks > 5. I want the property and the rule to be isolated in a "pure" business object, away from helpers, events, and the meteor user object. I want these business objects available everywhere, so I could make a restriction, say, to not assign tasks to users who are very busy, enforced on the server. I might also want a display rule to only display the first 100 chars of other peoples tasks if a user isVeryBusy. Where is the right place to create this user object, and how do I bind to it from my HTML?
You can (and probably should) use any package which allows you to attach a Schema to your models.
Have a look at:
https://github.com/aldeed/meteor-collection2
https://github.com/aldeed/meteor-simple-schema
By using a schema you can define fields, which are calculated based on other fields, see the autoValue property: https://github.com/aldeed/meteor-collection2#autovalue
Then you can do something like this:
// Schema definition of User
{
...,
isVeryBusy: {
type: Boolean,
autoValue: function() {
return this.tasks.length > 5;
}
},
...
}
For all your basic questions, I can strongly recommend to read the DiscoverMeteor Book (https://www.discovermeteor.com/). You can read it in like 1-2 days and it will explain all those basic questions in a really comprehensible way.
Best Regards,
There is a very good package to implement the solution you are looking for. It is created by David Burles and it's called "meteor-collection-helper". Here it the atmosphere link:
You should check the link to see the examples presented there but according to the description you could implement some of the functionality you mentioned like this:
// Define the collections
Clients = new Mongo.Collection('clients');
Tasks = new Mongo.Collection('tasks');
// Define the Clients collection helpers
Clients.helpers({
isVeryBusy: function(){
return this.tasks.length > 5;
}
});
// Now we can call it either on the client or on the server
if (Meteor.isClient){
var client = Clients.findOne({_id: 123});
if ( client.isVeryBusy() ) runSomeCode();
}
// Of course you can use them inside a Meteor Method.
Meteor.methods({
addTaskToClient: function(id, task){
var client = Clients.findOne({_id: id});
if (!client.isVeryBusy()){
task._client = id;
Tasks.insert(task, function(err, _id){
Clients.update({_id: client._id}, { $addToSet: { tasks: _id } });
});
}
}
});
// You can also refer to other collections inside the helpers
Tasks.helpers({
client: function(){
return Clients.findOne({_id: this._client});
}
});
You can see that inside the helper the context is the document transformed with all the methods you provided. Since Collections are ussually available to both the client and the server, you can access this functionality everywhere.
I hope this helps.

Dataset conversion to Linq object

Our WCF services return Datasets to the webserver,
In the new .NET web site we are going to use MVC 5. Since MVC 5 framework works really well with known business objets (validation framework etc.), we need to convert Datasets to known business objects in the Model classes.
We tried following conversion,
public List<Category> GetCategories()
{
List<Category> cats = new List<Category>();
DataTable dt = //get data table from the dataset;
foreach (DataRow row in dt.Rows)
{
Category cat = new Category();
cat.CategoryID = int.Parse(row["CategoryID"].ToString());
cat.CategoryName = row["CategoryName"].ToString();
cat.Description = row["Description"].ToString();
cat.Picture = GetBytes(row["Picture"].ToString());
cats.Add(cat);
}
return cats;
}
Assume that we retrieve and unpack the data table.
Will this be an expensive conversion if there are 100s' of request per second accessing this code block?
What would be a better way to test the performance under load?
Or is there a better approach to solve this problem?
Really appreciate any help on this.
I would not worry about the time it takes to convert the DataSet to domain objects since it's going to be a fraction of the remote call to get the data through a remote service.
However, for result sets that rarely change I would recommend adding caching around the resolved domain objects to avoid the conversion, but more importantly avoid the WCF call and subsequent DB call.

Kendo Ui Grid - fetching only page number of rows on the inital request

I've read several posts here and also the tutorials on Telerik website, but they are lacking - and the documentation is off.
Hoping for a quick fix after hours of reading.
I'm trying to use a Kendo grid with a huge amount of rows (1M). In the examples on the site, I see that the view controller action is returning the whole data set.
Fetching all of the rows is very expensive process and the data set is huge.
My question is how can I configure the grid in such a way that every subsequent callback will return the next page and the initial call will not fetch all of rows at once?
My code is similar to:
//Main controller action
public ActionResult Index()
{
List<items> listItems = GetAllItems(); // very expensive call!
return View(listItems);
}
// my view for that action
#(Html.Kendo().Grid(Model)
.Name("grid")
.Columns(columns =>
{
//some columns...
})
.Pageable(page=>page.PageSizes(true)) //Enable paging - I suspect here I can fix
.DataSource(datasource =>datasource.Ajax().PageSize(20).Read(read => read.Action("MoreItems", "Index")).ServerOperation(true)) // tried all sorts of things here
.Sortable()
.Filterable()
)
// the callbacks for the ajax
public ActionResult MoreItems([DataSourceRequest] DataSourceRequest request)
{
return Json(GetAllItems().ToDataSourceResult(request));
}
//add some cache just to see what was holding the thing up
[OutputCache(Duration = 3600, VaryByParam = "none")]
private static List<items> GetAllItems()
{
//some code to retrieve items
}
(from the examples it looks like the initial call is returning the complete model - and subsequent calls to the Products_Read are on the filter object. How can the initial call be filtered as well but allow for future paging - in my case I have 100k+ rows and it is impossible to do "return View(model") ) Thanks!
Seems that have not been very lucky with Kendo information... What you are looking for is called serverPaging (documentation under framework DataSource in here).
For each request your server will receive:
take contains the number of records to retrieve
skip how many records from the front of the dataset to begin reading
page the index of the current page of data
pageSize the number of records per page
You might also consider using scrollable.virtual (documentation in here where the following pages are loaded while you scroll down in the grid.
This example (http://demos.kendoui.com/web/grid/remote-data.html) uses serverPaging.
It seems that you are not familiar with the LINQ expression engine. The whole collection is never retrieved. The ToDataSourceResult method is doing exactly this - applying paging/sorting/grouping on a database level (thanks to that expression engine).
You do not have to do anything - just pass the IQueryable collection (which holds all the records) to the DataSourceResult, do not call ToList before this(or anything similar) or the magic will be broken :)

Order By property from dynamic linq

I am using dynamic linq to make a generic class for processing a generic JqGrid from MVC all works fine (searching, pagination etc) except for sorting on code properties. Sorting works fine when I am hitting the DB to sort the data, but as soon as it is a property I have made the sorting does not work eg
public partial class tblStockOrder
{
public string approved
{
get
{
return approved_id == null ? "" : "Approved";
}
}
}
I am running the following Dynamic Linq
items = items
.OrderBy(string.Format("{0} {1}", sidx, sord))
.Skip(pageIndex * pageSize)
.Take(pageSize);
Where sidx etc are strings passed in by jquery.
So basically what is the best solution for handling a case where some properties will be from the db while others will be code properties (not sure of the correct naming). I can handle all this in code using reflection but would obviously like the DB to handle as much of the searching / sorting as possible without pulling in thousands of records and sorting through them in code using reflection.
Computed class will of course not work, as you're trying to create record which is part in memory, part in database.
You can, however, compute the same on database by specifying the function in linq query, example:
items = items
.OrderBy(x=> x.approved_id != null )
.Skip(pageIndex * pageSize)
.Take(pageSize);

Resources