|
isn't that a mean
Russell
|
|
|
|
|
Hello,
In our firm, we use DataSet, DataTable, and Collectoin as Datasource for a DataGrid. But in 3 tier architecture I found that its slower when data is returned as a Collection Class from Data Layer, simply calling the Data Access function in the Data layer (which returns a DataTable) from Presentation layer is much faster. But this comes at the expense of breaking the architecture.
So which is the best data access object, Dataset (strongly typed or normal one), DataTable, or a Collection class? Can anybody share their thoughts.
Regards,
Blumen
Blumen
|
|
|
|
|
Hi,
I think is better to use datasets instead of collection because you can fill it directly from the adapter in the data access layer... You simplify... and you can do selects to get specific data from the table...
At last, I prefer dataset instead of datatable because If I need I can return two tables (and if is necessary I can use relations).
Thats my point of view. There're persons who prefer work with collections because they think that more easy to maintain...
|
|
|
|
|
il_masacratore wrote: Thats my point of view. There're persons who prefer work with collections because they think that more easy to maintain...
Not easier to maintain, but easier to work with in an OO fashion. Here's a quick example:
public interface IValid
{
bool IsValid { get ; }
}
public void ItemBase
{
protected virtual string TrimString(string value, maxLength)
{
if (value == null || value.Trim().Length == 0)
return value;
value = value.Trim();
if (value.Length > maxLength)
{
value = value.Substring(0, maxLength)
}
return value;
}
}
public void Customer : ItemBase, IValid
{
private string _forename;
private string _surname;
public Customer(string forename, string surname)
{
_forename = TrimString(forename, 30);
_surname = TrimString(surname, 30);
}
public string Forename
{
get { return _forename; }
set { _forename = TrimString(value, 30); }
}
public string Surname
{
get { return _surname; }
set { _surname = TrimString(value, 30); }
}
public bool IsValid
{
get
{
bool valid = false;
if (_forename != null && _forename.Trim().Length > 0)
valid = true;
return valid;
}
}
}
public abstract class BaseCollection<T> where T : IValid
{
private List<T> _list;
public List<T> Collection
{
get { return _list ; }
set { _list = value; }
}
public void Add(T value)
{
if (value.IsValid)
{
_list.Add(T);
}
else
{
throw new Exception("Invalid object.");
}
}
public abstract void Fill();
protected abstract void AddRow(IDataReader dr);
protected void Add(IDataReader dr)
{
if (dr.HasRows)
{
while (dr.Read())
{
AddRow(dr);
}
}
}
}
public class Customers : BaseCollection<Customer>
{
public override void Fill()
{
using (SqlConnection conn = new SqlConnection(Utilities.ConnectionString))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.Connection = conn;
cmd.CommandText = "GetAllCustomers";
cmd.CommandType = CommandType.StoredProcedure;
conn.Open();
using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
{
Add(dr);
}
}
}
}
protected override void AddRow(IDataReader dr)
{
string forename = dr.GetString(dr.GetOrdinal("forename"));
string surname = dr.GetString(dr.GetOrdinal("surname"));
try
{
Add(new Customer(forename, surname));
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
Notice that you only have to implement the two methods in the inherited collection class to get the benefits of adding data from the database into your collection. It is trivial to extend this functionality to include the ability to search for records, save or delete them. Now, your client code will be a lot easier. Which of these do you think is easier to use?
customer.Surname = "Smith";
or
ds.Tables[0].Rows[0]["surname"] = "Smith";
What happens when you change the field name in the database from surname to cust_surname. In the implementation above, you only need to change this reference in the Customers class. If you are using a DataSet, then you have to hunt them down in every location that they are referred to and you could well end up missing them. Plus, what happens if you've exposed this as a web-service? The DataSet is a Microsoft implementation, so if a Java application is going to consume the DataSet then they are going to have to wrap up a heavy duty object. Plus, the renaming problem becomes much worse if you have changed the name of the column in the database. You are now asking everybody who consumes your DataSet to change their references to the field name. By the way, you can still bind the Collection property to items like a datagrid because this is a collection class.
I know that the abstraction seems to be a complete PITA, but once you have created a base object that you are happy with, you can easily reuse (and extend) this functionality. With the use of a tool like CodeSmith, creating the other classes becomes trivial.
the last thing I want to see is some pasty-faced geek with skin so pale that it's almost translucent trying to bump parts with a partner - John Simmons / outlaw programmer
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
Hi,
I'm agree with you. But, at all, we're always depending on timing... In some cases we will do the best practices and in others we will do a shoddy work...
PD: If we use constants to access fields, we unify and we only have to change its value...
|
|
|
|
|
I agree that time constraints are a problem, but this is where a tool like CodeSmith comes into its own. We have a base set of objects that we have created and fine-tuned (for things like the base collections, the base item objects and so on). We then generate our standard stored procedures implementation classes and unit tests from this. This means that, once we have a data model defined, we can generate the entire DataLayer in one go.
Anything like validation that needs to be performed is handled in a derived class, so that if we regenerate the code, we don't lose our custom logic.
Yes, getting the base objects and CodeSmith templates into a fashion that you are happy with does take some time. But once you are happy with the way your objects work, generating new classes takes almost no time.
the last thing I want to see is some pasty-faced geek with skin so pale that it's almost translucent trying to bump parts with a partner - John Simmons / outlaw programmer
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
Hi again,
I will try it... You know any published example of use??
Thanks
|
|
|
|
|
Well, you can get the freeware version of CodeSmith (with templates) here. http://www.codesmithtools.com/freeware.aspx[^]. We have our own templates that have been developed and tested over a couple of years of seriously heavy duty use, but the standard templates are pretty decent. There is a thriving community of CodeSmith template writers on the web.
the last thing I want to see is some pasty-faced geek with skin so pale that it's almost translucent trying to bump parts with a partner - John Simmons / outlaw programmer
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
|
In our company we return data from the data layer as lists of strongly typed objects, and testing has shown that this is around 20-30% faster than using data tables.
It's not really the performance that is most appealing, but the type safety. My experience is that it makes it much easier to write, debug and maintain the applications.
---
Year happy = new Year(2007);
|
|
|
|
|
Hi,
I'm sorry to say this, but all these information got me confused. Pardon me, i'm a person with less than an year experience in programming.
Does all these posts mean collection are better. We already have implemented a part of our product using collections (3 tier), but its very slow and client now wants old VB 6.0 applicatio, it was lot faster...
I'm assigned this job to make a report on the reason of poor performance of our .NET application. When i used a datatable to return values from data layer to presentation layer, application was 70% faster.. The SQL query is fine, I tested it in Oracle..
Any ideas?
Blumen
|
|
|
|
|
Unfortunately it gets down to a question of experience and profiling. You need to look at how much data you are returning - are you pulling back more than you need to? What are you using to populate the collection? Are you using a DataReader to populate the collection, or are you using a DataSet/DataTable? Are you using web-services and passing data backwards and forwards between layers?
This last one is important because you need to serialize the data to pass over the wire (and then deserialize at the other end), and this serialization takes time. One of the commonest problems I have seen is people architecting systems using web-services when they don't need to.
the last thing I want to see is some pasty-faced geek with skin so pale that it's almost translucent trying to bump parts with a partner - John Simmons / outlaw programmer
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
Hi,
Thanks for the reply, I rally appreciate it. This is what we currently do:
1. From Presentation layer, we call a function in Business Layer and pass the business object to it.
2. From the business layer we pass this businees object received from step 1 to Data Access Layer.
3. In the Data Access layer we call the Oracle Stored Procedure.
In Step 2, we receive a Data as DataTable from Data Access layer. Here we convert it to a collection and send it to Presentation layer and this data collection is given as DataSource to the DataGrid. We use .NET Framework 1.1 SP1 with VS 2003.
We are in to health care domain, and in the above example you can consider the business object to be "Patient" in a hospital or a list of doctors or medecal history of a patient(s). This is the scenario we have at office.
I hope I'm more descriptive this time
But data retreival is pretty slow, once we Click the Search or Save button in a Windows Form, its takes some time before data appears in the Grid. And we have to refresh the data in the grid with user interaction (say when user changes doctor from the combo box), so that makes the application slower!
Considering the above situation, which data object would be best - Typed DataSet, normal DataSet, Collection, or DataTable.
Regards,
Blumen
I want to become a software consultant one day, a person who can solve any issues with design, architecture, and the application..
|
|
|
|
|
Your problem is most likely in stage 2. You are retrieving data into a DataTable and then you are converting it into a collection. This is very expensive. If I were you, I would retrieve the output from the SP using a DataReader and then I would populate the collection directly from this.
A DataReader serves data as what we normally term a firehose cursor. In other words, the data can only be read in one direction and it is typically the fastest way to get data out. More importantly, you can put this retrieval onto a different thread so at some point, you could start displaying some of the information back in the grid.
I hope that this helps.
the last thing I want to see is some pasty-faced geek with skin so pale that it's almost translucent trying to bump parts with a partner - John Simmons / outlaw programmer
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
Hello Pete,
That was really helpful. On Monday I will try what you suggested, I will retrieve data using a DataReader and populate the collection directly. That sounds really good.
I have read in some online article that retrieving data using DataReader is not safe, don't remember what they meant, I read it long time back. Which is better way to retrieve data, using DataReader or DataAdapter, or is their another way?
I really appreciate that you have replied.
Regards,
Blumen
|
|
|
|
|
I've never found the DataReader to be unsafe. It's the method that I normally use. I suspect that what you have read is more to do with concurrency.
Anyway, good luck and I hope that it works out. And if you need more help, don't forget to post.
the last thing I want to see is some pasty-faced geek with skin so pale that it's almost translucent trying to bump parts with a partner - John Simmons / outlaw programmer
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
Hi Pete,
I believe what you are saying is correct, DataReader should be safe, after all Microsoft would not release it just like that!
But what was the concurrency issue you were referring to?
Regards,
Blumen
|
|
|
|
|
Blumen
The issue relates to the underlying data being changed as you iterate through the datareader. This normally occurs when somebody else changes a record that you are interested in. If you don't have your transaction isolation level set accordingly (take a look in the Oracle docs for this), you can get issues.
the last thing I want to see is some pasty-faced geek with skin so pale that it's almost translucent trying to bump parts with a partner - John Simmons / outlaw programmer
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
Pete, I think this is the reason why the guys here use Adapter to fill a DataTa
ble and then make a collection.
Have to investigate more.. Is there anyway I can contact u, any e-mail ids or chat ids, if you don't mind?
Blumen
blumenhause@yahoo.com
|
|
|
|
|
Guys,
I have a weird problem.
1.
We have a windows service running on the computer.
This service actually manages services that are related to our core application (We can call up a gui and add new services into this one).
2.
In order to customize our core application we can interact with it via a tlb file.
So I've written a little application that can interact with our core application (press buttons, select items, read/write from/to fields,...)
To do this I have a wrapper dll that calls CreateObject to create the object.
I built in a check to make sure the core application is running, because else it would be started. (We don't want to start it that way)
All works fine until I add my custom application to this service manager. It passes the check that our application is running, but will still fire up a new instance of our core application.
Apparently he can't attach to the existing application or something?
Why is this happening? What's the cause?
Is there a way around this?
I hope my explanation was clear, thanks in advance for your help !
V.
I found a living worth working for, but haven't found work worth living for.
|
|
|
|
|
hello all,
I am trying to save of importing a dataView to Excel file. dose anybody now how?
discription:
first I did a select to an Excel file and then I made a DataView of those selected rows and now I want to save that DataView as a Exel file.
-- modified at 8:29 Thursday 18th January, 2007
|
|
|
|
|
Hi
This is the code to Export data to excel file from table in vb.net
Public Sub XportToXcel(ByVal dt As DataTable)
Try
Dim XApp As New Excel.Application
Dim XBook As Excel.Workbook = XApp.Workbooks.Add
Dim XSheet As Excel.Worksheet = CType(XBook.Worksheets(1), Excel.Worksheet)
XApp.Visible = False
With XSheet
Dim c As Long = Asc("A")
For Each dc As DataColumn In dt.Columns
.Cells(1, c).Value = dc.ColumnName.ToString
.Cells(1, c).Font.Bold = True
c += 1
Next
Dim i As Long = 2
For Each dr As DataRow In dt.Rows
c = Asc("A")
For Each dc As DataColumn In dt.Columns
.Cells(i, i - 1).Value = dr.Item(dc.ColumnName)
c += 1
Next
i += 1
Next
XApp.Visible = True
End With
Catch ex As Exception
MessageBox.Show("Source [" & ex.Source & "] Description [" & ex.Message & "]")
End Try
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
XportToXcel(DataGrid1.DataSource)
I hope this will idea for u r problem
Have a Nice day
|
|
|
|
|
Hi All,
I am using HttpWebRequest and HttpWebResponse object to upload a file over HTTPS Channel.
My code works fine when I upload a small size file (Less than 2K). When I try to upload a bigger file, HttpWebRequest is Timing Out.(The operation has timed-out.)
Here is the snippet of the code I am using
HttpWebResponse Resp = (HttpWebResponse) Req.GetResponse(); Stream responseStream = Resp.GetResponseStream(); Encoding enc = Encoding.GetEncoding(1252); // Windows default Code Page
StreamReader loResponseStream = new StreamReader(responseStream,enc);
string stringResponse = loResponseStream.ReadToEnd();
LogFileStream.WriteLine("Http Response: " + Resp.StatusCode + " " + DateTime.Now.ToString().Trim()+"\n");
responseStream.Close();
oResponseStream.Close();
Does any body having clue what to do in such kind of scenario??
Thanks in advance.
Satya
Satya
|
|
|
|
|
I am using a calendar control in my application(Visual Studio 2005, C# .NET 2.0). As per the control, clicking on the calendar textbox, a calendar should popup just below the textbox. I am facing two problems with the control. First problem is, when I am clicking on the text box(calendar control) the calendar is popping up but it is displaying under the other controls below. Second problem is, if I am using more than one date control in the same page, one of the calendar is getting displayed in top left corner of the page permanently. The calendar control is inside a "div" tag. Is this the problem?
|
|
|
|
|
Using Globalisation for ASP.Net Can a Web appplication can take the user input in different laguages. Like for example my culture is fr-Fr french then Can a user enter the data in french in the textbox control.
sj
|
|
|
|
|