Why don't we have a simple data persistence interface?

Status
Not open for further replies.

andrewbb

Member
Joined
Jun 6, 2017
Messages
15
Programming Experience
10+
For example:

Database.Store(object), Load & Delete.
 
I suspect we do, what exactly are you looking for?

A utility to save an object's properties to a table or a stored procedure. No config.

Database.Store(customer)
Database.Load(customer)
Database.Delete(customer)

It should read the database connection from App.config/Web.config.
The table or name for the stored procedure should be an attribute on the Customer class.
 
A utility to save an object's properties to a table or a stored procedure. No config.

Database.Store(customer)
Database.Load(customer)
Database.Delete(customer)

It should read the database connection from App.config/Web.config.
The table or name for the stored procedure should be an attribute on the Customer class.
You mean like serializing an object the storing the data in a VarBinary field in your database?
 
No. Each property maps to a SP parameter OR a table column.
So a DataTable then and all the controls are Bound to a field in the table and your .Net code simply does the Loading/Inserting/Updating/Deleting from that DataTable and reflects the changes back to the database?
 
It would load a Customer object with properties, methods, and events. You wouldn't need a DataTable or a DataReader.

Customer c = new Customer();
c.ID =1;
Database.Load(c);
 
I'm going to take a guess that you're not familiar with the DataSet (which comes with a visual designer) they added back in 2005/.Net 2.0 that allows you to do just that in a similar manor.
To use it, add a new DataSet file to your solution (right click the project, go to Add, select "New Item", then click on Data from the left, and select "DataSet", you have the chance to give the .xsd file a meaningful name), then open the DataSet file, right click on it and a TableAdapter (it'll create a DataTable for you in the wizard), the first time doing this you will need to specify the connetionstring by selecting the data source. Anywho after all that is done, select the table you want to bring in, it has a query designer so you can do things like join tables, etc and once done you can attach queries to the TableAdapter, here's more info: Working with Datasets in Visual Studio

Also note, each "query" you make on a TableAdapter you can simply call in your .Net code to do the DB interaction, including passing parameters, so you can make a CustomersTableAdapter and have one of the Fill() methods allow you to pass in a CustomerID value and viola, you have a specific row (or specific set of data).
You can make other queries for things like Inserts, Updates, & Deletes that you can call from the same TableAdapter object in your code.
 
You wouldn't need a DataSet. It's not table replication from the database into the application.

It's mapping object properties to table columns or SP parms. No config:

Customer c = new Customer();
c.Name = 'Alyson';
Database.Store(c);
 
You wouldn't need a DataSet. It's not table replication from the database into the application.

It's mapping object properties to table columns or SP parms. No config:

Customer c = new Customer();
c.Name = 'Alyson';
Database.Store(c);
You're basically talking about wanting what the DataSet let's you already do, in your name "Alyson" example you would have a function on the TableAdapter where you can pass it a name and it would do an Insert on the database setting Alyson as the data in the Name column and return the CustomerID of the new record.
If you're wanting to take the DataSet to a new level where you simply create a new Customer, set the Name to "Alyson" and call a Store() method then you'll need to make a wrapper that does that in which case you can either use a DataSet object in the solution that the wrapper functions simply call, or you can create the SqlConnection, SqlDataAdapter, etc that does the DB calls yourself.
 
Get rid of all of that stuff. None of those objects are necessary. Total code:

CustomerList list = new CustomerList();
list.ZipCode = '33810';
Database.Load(list, "ByZipCode");

That's how simple it can be.
 
A utility to save an object's properties to a table or a stored procedure. No config.

Database.Store(customer)
Database.Load(customer)
Database.Delete(customer)

It should read the database connection from App.config/Web.config.
The table or name for the stored procedure should be an attribute on the Customer class.
This is also very similar to Entity Framework, see for example Entity Framework (EF) Documentation
From one of the introduction examples:
        using (var db = new BloggingContext()) 
        { 
            // Create and save a new Blog 
            Console.Write("Enter a name for a new Blog: "); 
            var name = Console.ReadLine(); 
 
            var blog = new Blog { Name = name }; 
            db.Blogs.Add(blog); 
            db.SaveChanges();
 
Status
Not open for further replies.
Back
Top Bottom