views:

1367

answers:

6

Realised I am writing yet another code generator to do the sps/classes/interfaces etc for a simple ORM layer. This time most of the guts are in SQL. I have a general-purpose C# DAL for calling sps/getting results etc. (took me all of an hour or so and is tiny).

I thought for sure there'd be an easier way by now... is there?

I am confident/competent with SQL and use stored procs etc - I'm not looking to hide from SQL, just take the boring repetition out of coding for populating/persisting objects. I'm not into learning a templating language/complex app, or apps that produce mega-bloatware (or build on bloaty MS libraries). Also must be able to start with an existing database.

Is it still a case of roll-your-own? Really?

.Net 2.0 (Winforms)

EDIT: I am not entirely anti-template, if it's really simple/quick to pick up. Ideally, the solution would be small, free and unscary to other developers.

A: 

I like nHibernate, I am very proficient in SQL; however, I like not having to write a minimum of four procs for each table. For things which I can't figure out how to make nHibernate do.

You do have a lot of code generators out there but if you don't want to learn how to build a template, and aren't satisfied with the built / community templates then your only other option would be roll your own.

JoshBerke
+1  A: 

Take a look at SubSonic.

+2  A: 

Not directly answering you question so.

Read this great post by Ayende: 25 Reasons Not To Write Your Own Object Relational Mapper

A recommend using NHibernate.

Stefan Steinegger
+1 for the link, not seen that article before.
Steve Haigh
+4  A: 

Take a look at BLToolkit:

[TestFixture]
public class ExecuteObject
{
    public abstract class PersonAccessor : DataAccessor<Person>
    {
        // Here we explicitly specify a stored procedure name.
        //
        [SprocName("Person_SelectByKey")]
        public abstract Person GetByID(int @id);

        // SQL query text.
        //
        [SqlQuery("SELECT * FROM Person WHERE PersonID = @id")]
        public abstract Person GetPersonByID(int @id);

        // Specify action name.
        // Stored procedure name is generated based on convention
        // defined by DataAccessor.GetDefaultSpName method.
        //
        [ActionName("SelectByName")]
        public abstract Person GetPersonByName(string @firstName, string @lastName);

        // By default method name defines an action name
        // which is converted to a stored procedure name.
        // Default conversion rule is ObjectName_MethodName.
        // This method calls the Person_SelectByName stored procedure.
        //
        public abstract Person SelectByName(string @firstName, string @lastName);
    }

    [Test]
    public void Test()
    {
        PersonAccessor pa = DataAccessor.CreateInstance<PersonAccessor>();

        // ExecuteObject.
        //
        Assert.IsNotNull(pa.GetByID        (1));
        Assert.IsNotNull(pa.GetPersonByID  (2));
        Assert.IsNotNull(pa.GetPersonByName("Tester", "Testerson"));
        Assert.IsNotNull(pa.SelectByName   ("Tester", "Testerson"));
    }
}
Anton Gogolev
A: 

Thanks guys - I will look into the options you suggested.

kpollock
try to select an answer, why selecting your reply as an answer?
Amr ElGarhy
Because the answer, as I see it, is to thank people for their suggestions and for me to go away and think about it. I am not going to vote for a particular solution if I haven't tried the recommendations out. This is not the type of question that has a definitive answer. I intended to pretty much stop the thread.
kpollock