另一个数据访问层(c#.net 2.0)

发布于 2024-07-17 21:51:15 字数 433 浏览 4 评论 0原文

意识到我正在编写另一个代码生成器来为简单的 ORM 层执行 sps/类/接口等。 这次大部分内容都在 SQL 中。 我有一个通用的 C# DAL,用于调用 sps/获取结果等(花了我一个小时左右,而且很小)。

我想现在肯定会有一个更简单的方法......是吗?

我对 SQL 充满信心/有能力,并使用存储过程等 - 我不想躲避 SQL,只是将填充/持久对象的编码中的无聊重复去掉。 我不喜欢学习模板语言/复杂的应用程序,或者生成大型臃肿软件(或构建在臃肿的 MS 库上)的应用程序。 还必须能够从现有数据库开始。

仍然是自己卷的情况吗? 真的吗?

.Net 2.0(Winforms)

编辑: 我并不完全反对模板,如果它真的很简单/快速上手的话。 理想情况下,该解决方案应该是小型的、免费的并且对其他开发人员来说并不可怕。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

傲性难收 2024-07-24 21:51:15

看一下 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"));
    }
}

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"));
    }
}
魔法唧唧 2024-07-24 21:51:15

不直接回答你的问题。

阅读 Ayende 的这篇精彩文章: 不编写自己的对象关系的 25 个原因Mapper

推荐使用NHibernate。

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.

久随 2024-07-24 21:51:15

看看亚音速。

Take a look at SubSonic.

最初的梦 2024-07-24 21:51:15

我喜欢nHibernate,我对SQL非常精通; 然而,我喜欢不必为每个表编写至少四个过程。 对于我不知道如何让 nHibernate 做的事情。

您确实有很多代码生成器,但如果您不想学习如何构建模板,并且对构建/社区模板不满意,那么您唯一的其他选择就是推出自己的模板。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文