Persist object to a database?
Ok, I was talking to a colleague of mine, and this conversation evolved from Stored Procedures into object persistence.
My colleague (who is EXTREMELY knowledgeable) is a Java guy, and I'm more of a .NET guy (but I'm learning several different languages).
Anyway, I was asking him recently about using stored procedures in a project, and he told me that using stored procedures is no longer a best practice, and that a "persistence framework" is now a best practice (something like NHibernate).
My thinking at that point was this "persistence" he spoke of was something akin to the way someone can update a database with ADO.NET, like the following (some code excluded for brevity):
// ######################### // UPDATE. // ######################### DataRow targetRow = dataTable.Rows[index]; Application.DoEvents(); targetRow.BeginEdit(); targetRow["MyField"] = txtSomeText.Text; targetRow.EndEdit(); DataSet DataSetChanged = DataSet.GetChanges(DataRowState.Modified); DS.Merge(DataSetChanged); DataAdapter.Update(DS, "TableName"); DS.AcceptChanges();
Apparently, according to my colleague, this is not the case.
He provided me the following (it's in Java):
EntityManager em;
public void createCustomers() { Customer c = new Customer(); c.setCustomerID(1); c.setFirstName("John"); c.setLastName("Doe"); em.persist(c); }
Then to load a customer you would do this:
EntityManager em;
public Customer getCustomer(int id) { Customer c = em.find(Customer.class, new Integer(id)); return c; }
The plumbing is handled by the framework, and no explicit DB IO occurs.
He followed that up with:
The call to EntityManager.persist(Object) stores the object in the DB. The call to EntityManager.find(Class, Key) retrieves it. So nothing in particular happens when the object is destructed - just when you make those persistence calls. The persistence framework builds a table on the back end that looks a lot like the object. Classname = tablename, attributeNames = rowNames, etc.
My question to him at that point was:
Ah, so it still hits a DB, it just does it in a different way?
To which he responded: Yes, it totally encapsulates DB stuff. All you have to do is create an object and tell it to persist. Everything else is done and you never see it. It's like your objects know how to store themselves.
-------------
So my question is this: Is there something like this in .NET (preferably C#)? If so, could someone point me to an example? I've got a couple of projects written down, and I want to start working on them, and I'd like to try this as an experiment.
Any help would be greatly appreciated. Started By Monte on Jun 8, 2010 at 10:14:46 AM |