Ramblings from the Creator of WilsonDotNet.com
CREATE PROCEDURE RetrieveCategoryList () AS SELECT CategoryId, CategoryName FROM Categories;CREATE PROCEDURE RetrieveCategoryById (@CategoryId INT) AS SELECT CategoryId, CategoryName FROM Categories WHERE CategoryId = @CategoryId;CREATE PROCEDURE InsertCategory (@CategoryName VARCHAR(50)) AS INSERT INTO Categories (CategoryName) VALUES (@CategoryName); SELECT CategoryId = SCOPE_IDENTITY();CREATE PROCEDURE UpdateCategory (@CategoryId INT, @CategoryName VARCHAR(50)) AS UPDATE Categories SET CategoryName = @CategoryName WHERE CategoryId = @CategoryId;CREATE PROCEDURE DeleteCategory (@CategoryId INT) AS DELETE FROM Categories WHERE CategoryId = @CategoryId;
<entity type="Demo.Category" table="Categories" keyMember="id" keyType="Auto"> <attribute member="id" field="CategoryId" alias="Id" /> <attribute member="name" field="CategoryName" alias="Name"/></entity>
namespace Demo { public class Category { private int id; private string name; public int Id { get { return this.id; } } public string Name { get { return this.name; } set { this.name = value; } } }}
ObjectSpace manager = new ObjectSpace(mappingFile, connectString, Provider.MsSql);CategoryGrid.DataSource = manager.GetObjectSet(typeof(Category), String.Empty);// .NET v2.0: CategoryGrid.DataSource = manager.GetObjectSet<Category>(String.Empty);
Category category = manager.GetObject(typeof(Category)) as Category;// .NET v2.0: Category category = manager.GetObject<Category>();category.Name = "Insert";manager.PersistChanges(category);
Category category = manager.GetObject(typeof(Category), id) as Category;// .NET v2.0: Category category = manager.GetObject<Category>(id);category.Name = "Update";manager.PersistChanges(category);
Category category = manager.GetObject(typeof(Category), id) as Category;// .NET v2.0: Category category = manager.GetObject<Category>(id);manager.MarkForDeletion(category);manager.PersistChanges(category);
manager.ExecuteDelete(typeof(Category). String.Empty);
Ok, from the "my LOC is smaller than your LOC" boasting department, with my O/R tool all I have to write is this in one file: table demo:category { fields { int id int notnull readonly [the ID]; string name nvarchar(30) notnull readwrite [the name]; }; pkey sequenced id; # your example had a get of all categories so I need to provide such a get: get multi {called all}; }; I know it's not XML, I wrote this before it was obvious that XML would be THE way to do these kind of things. To this day I haven't been able to find an XML syntax that can express all the same things that isn't painfully verbose in places. Anyway, equivalents to your C# code: // #1 CategoryGrid.DataSource = Category.GetByAll(); // #2 Category cat = new Category(); cat.Name = "Insert"; cat.Update(); // #3 Category cat = Category.GetById(id); cat.Name = "Update"; cat.Update(); (notice just as with your version, once you've done the create or get, the code is identical regardless of whether it's an insert or an update. This is nice in ASP.NET because you can do the get-or-create conditionally in your Page_Load and then just call .Update() in your _Click) // #4 Category cat = Category.GetById(id); cat.Delete(); // #5 (this one's less efficient, but easier to understand when reading it; also it's a corner case, at least in the kind of stuff I do) foreach (Category cat in Category.GetByAll) { cat.Delete(); } By the way, I'm guessing from the fact that your XML doesn't contain enough information to create the table itself that you have to do that by hand? You should also include a CREATE TABLE statement in your "necessary lines of code". Mine creates the table (and indexes it based on what gets you specify, and puts in foreign keys etc) for me. And updates it when the definition file changes. http://savannah.nongnu.org/projects/nrdo :) Ok, the downside (and it's a fairly big one) is that the total set of users of nrdo is pretty much me and my company. The documentation is limited and naturally it only gets testing in a limited number of configurations. It also has some quirks and its error messages aren't exactly clear most of the time. I just wish I could figure out how to convince people that it's actually kind of cool and it would be nice if they'd use it and contribute fixes to some of those problems...
nrdo seems nice, but you loose a lot of apeal at the door steps by choosing GPL license...
Note that the GPL only applies to the tools (in other words, if you fix or enhance the tools, I'd like the fixes back). The runtime libraries are licensed under the LGPL which allows them to be linked into any application, under any license, without any obligation except to make the source to the library available - which I'm already doing for you. My own company uses nrdo in a proprietary application - there's no obligation to make the source of your app available.
well, that is nice to know - thanks for the explanation... p.s. Paul, sorry for digresing