C#数据库映射器 [英] C# Database Mapper

查看:84
本文介绍了C#数据库映射器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找将数据库查询结果映射为c#代码中强类型对象的方法.因此,我在SqlConnection类上编写了一个快速而肮脏的帮助程序方法,该方法在数据库上运行查询,并使用反射将记录列映射到对象属性.代码如下:

I was looking to map my database query results to strongly type objects in my c# code. So i wrote a quick and dirty helper method on the SqlConnection class which runs the query on the database and uses reflection to map the record columns to the object properties. The code is below:

 public static T Query<T>(this SqlConnection conn, string query) where T : new()
    {
        T obj = default(T);

        using (SqlCommand command = new SqlCommand(query, conn))
        {
            using (SqlDataReader reader = command.ExecuteReader())
            {
                while (reader.Read())
                {
                    obj = new T();

                    PropertyInfo[] propertyInfos;
                    propertyInfos = typeof(T).GetProperties();

                    for (int i = 0; i < reader.FieldCount; i++)
                    {
                        var name = reader.GetName(i);

                        foreach (var item in propertyInfos)
                        {
                            if (item.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase) && item.CanWrite)
                            {
                                item.SetValue(obj, reader[i], null);
                            }
                        }

                    }
                }
            }
        }

        return obj;
    }


  public class User
    {
        public int id { get; set; }
        public string firstname { get; set; }
        public string lastname { get; set; }
        public DateTime signupDate { get; set; }
        public int age { get; set; }
        public string gender { get; set; }
    }


   var user = conn.Query<User>("select id,firstname,lastname from users");      

我只是想对以上使用反射将值绑定在一起的方法提出第二点意见,如果有什么我可以在上面的代码中做的更好.或者,如果我可以采取其他完全不同的方法来获得相同的结果?

I just wanted a second opinion on my approach above of using reflection to tie the values together, if there's anything i can do better in the code above. Or if there's some other totally different approach i can take to get the same result?

我认为我可以通过删除propertyInfos循环并使用字典来改善helper方法中的代码.还有其他需要调整的地方吗?

I think i can probably improve the code in the helper method by removing the loop for propertyInfos and using a dictionary instead. Is there anything else that needs to be tweaked?

P.S:我知道Dapper,我只是想自行实现类似的功能,以帮助我更好地学习.

P.S: i'm aware of Dapper, i just wanted to implement something similar on my own to help me learn better.

推荐答案

基本上,您要做的就是linq-to-sql或其他OR-mapper进行的操作.要了解其工作原理的详细信息,从头开始写东西总是一个好主意.

What you've done is basically what linq-to-sql or other OR-mappers do under the hood. To learn the details of how it works it's always a good idea to write something from scratch.

如果您想获得更多灵感或希望有一些准备好用于生产的东西可以直接使用,那么我建议您阅读linq-to-sql.它轻巧但功能强大.

If you want more inspiration or want to have something that's ready for production use out-of-the-box I'd recommend reading up on linq-to-sql. It is lightweight, yet competent.

这篇关于C#数据库映射器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆