使用Dev express XPO进行C#数据访问 [英] C# data access using Dev express XPO

查看:257
本文介绍了使用Dev express XPO进行C#数据访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始使用C#和Visual Studio 2012作为初级开发人员的新角色。我发现我的公司在上周加入时使用DevExpress Xpress持久对象(ORM)进行数据访问。这对我来说是新的,我一直在教自己如何使用devExpress文档网站

这里



问题是文档中包含了我目前不需要的大量信息,因为我需要一些东西让我尽快运行。我花了很多时间在谷歌搜索教程,但没有什么好的出现。有没有人知道任何可以让我使用DevExpress XPO立即构建项目的教程链接?



Anyhelp或建议将受到高度赞赏。

I have just started a new role as Junior developer using C# and Visual Studio 2012. I only found that my company use DevExpress Xpress Persistent Object (ORM) for Data Access when I joined them last week. This is new to me and I have been teaching myself how to use it using the devExpress documentation website
here.

The problem is the documentation contains loads of information that I do not need at the moment because I need something to get me running ASAP. I have spent a lot of time searching for tutorials on google but nothing good shows up. Does any one know of any tutorial link that can get me building projects rightaway using DevExpress XPO?.

Anyhelp or suggestions will be highly appreciated.

推荐答案

有一段时间我曾想过为XPO做一个快速入门指南,因为我也做了同样的事情。 DevExpress文档很不错,但是需要筛选一大堆信息。 XPO没有任何快速入门指南...也许我最后会写下我的第一篇文章。



我打算做几个这里的假设,就像你使用sql server作为你的后端。



要记住一件事,如果你正在这样做/将永远使用XPO与web应用程序,你想确保你使用XPO进行每一次单独调用包含在一个using(var uow = new UnitOfWork()中,否则你会遇到一些讨厌的交叉线程错误。



我在我的例子中使用YourClassName。这是将成为您使用DevExpress ORM向导或您手动创建的XPO对象。



如果您对xpo有任何其他疑问请随时联系我。



For a while i had thought about doing a quick start guide for XPO as i went through the same thing you did as well. DevExpress documentation is nice but it is a TON of information to sift through. There isn't really any quick start guide to XPO per-say...maybe i'll write my first article finally.

I am going to make a few assumptions here, like you are using sql server as your backend.

One thing to keep in mind, if you are doing this/will ever get into using XPO with web applications you want to make sure EVERY SINGLE CALL you make using XPO is wrapped in a using(var uow = new UnitOfWork(), otherwise you get some nasty cross threading errors.

I use "YourClassName" in my examples. That is going to be your XPO objects that you either generate using the DevExpress ORM wizard or whatever it is you create by hand.

If you've got any other questions about xpo feel free to reach out to me.

//Initialize your data layer.
            //By default if you don't do this, XPO will try and use an access databse (jet)
            Session session = new Session();
            XpoDefault.DataLayer = XpoDefault.GetDataLayer(MSSqlConnectionProvider.GetConnectionString("YourServerHostnameOrIP", "DB Username", "DB Password", "DB Name"), AutoCreateOption.None);
            XpoDefault.Session = session;

            //Equivalent of SELECT * FROM TableName in SQL
            // YourClassName would be your XPO object (your persistent object)
            using (var uow = new UnitOfWork())
            {
                XPCollection<yourclassname> getRecords = new XPCollection<yourclassname>(uow);

                foreach (var item in getRecords)
                {
                    //Do something with what you got in your XPCollection
                }
            }

            //Equivalent of SELECT * FROM TableName WHERE Id = 1
            using (var uow = new UnitOfWork())
            {
                XPCollection<yourclassname> getSpecificRecord = new XPCollection<yourclassname>(uow, CriteriaOperator.Parse("Id = '1'"));
                //OR

                YourClassName getSingleRec =
                    new XPCollection<yourclassname>(uow, CriteriaOperator.Parse("Id = '1'")).FirstOrDefault();

                //do something with single record from DB
            }

            //Equivalent of SELECT * FROM Table WHERE FileName LIKE '%User%'
            using (var uow = new UnitOfWork())
            {
                XPCollection<yourclassname> getRecords = new XPCollection<yourclassname>(uow, CriteriaOperator.Parse("FileName LIKE '%user%'"));

                foreach (var item in getRecords)
                {
                    //do something with code
                }
            }

            //Equivalent of an Insert Statement in SQL
            using (var uow = new UnitOfWork())
            {
                YourClassName save = new YourClassName(uow);
                save.FieldName = "I am a value to save to Database";
                save.Save();
                uow.CommitChanges();

                //.Save() will persistent your changes to the object but uow.CommitChanges() will save those changes back to the database
            }

            //Equivalent of an Update statement in SQL

            using (var uow = new UnitOfWork())
            {
                YourClassName update = uow.FindObject<yourclassname>(CriteriaOperator.Parse("Id = '1'"));
                update.FieldName = "Update To New Value";
                update.Save();
                uow.CommitChanges();
            }
</yourclassname></yourclassname></yourclassname></yourclassname></yourclassname></yourclassname></yourclassname></yourclassname>


这篇关于使用Dev express XPO进行C#数据访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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