Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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.
Posted
Updated 15-Jun-17 11:40am
v2

1 solution

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.

C#
//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>
 
Share this answer
 
Comments
Uzoma Umekwe 11-Aug-13 10:55am    
David_Wimbley Your tutorial was really helpful. My confidence using XPO is gradually increasing.Anytime I need the touch of a pro on my code i'll always give you a shout. :) Thanks a lot.
Member 10339087 8-Jan-14 4:40am    
How we use for Joining two tables??
David_Wimbley 9-Jan-14 16:28pm    
You could just load the two tables into XPCollections and then using linq do the join that way.

Another option is a direct sql query using XPO.

But there is a way to go about this that is more than likely well documented on the devexpress message boards and forums.
RomanRapido 17-Feb-17 20:20pm    
How do you apply this code in XAF? Thanks!
David_Wimbley 22-Feb-17 23:32pm    
I'm not familiar with XAF and back when I answered this was the last time i really used XPO...i moved over to entity framework since so I'm afraid I can't be of much help.

Do you have the code behind in XAF you can share? I'm sure if you can access the code you can massage the answer into your XAF code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900