in
Forums
Blogs
DevExpress.com
Client Center
Support Center
DevExpress Channel

Gary's Blog

XAF Cookbook #6 – Programmatically Add a New BO

Problem

You have defined a business object and XAF has generated the necessary UI to enable you to carry out CRUD functionality; however, you now wish to be able to add a new instance of the business object programmatically.

Solution

In your chosen module, add a controller to the module. Drag a SimpleAction onto the Controller’s design surface. Give the SimpleAction a suitable name and a caption then handle the <SimpleActionName>_Execute event in order to add a new instance of your business object to the View’s ObjectSpace.

Discussion

Given the following definition of a business object named Book:-

using System;

using DevExpress.Xpo;

using DevExpress.ExpressApp;
using DevExpress.Persistent.Base;
using DevExpress.Persistent.BaseImpl;
using DevExpress.Persistent.Validation;

namespace ObjectSpaceExample.Module {
    [DefaultClassOptions]
    public class Book : BaseObject {
        public Book(Session session) : base(session) { }

        private string _Author;
        public string Author {
            get {
                return _Author;
            }
            set {
                SetPropertyValue("Author", ref _Author, value);
            }
        }

        private string _Title;
        public string Title {
            get {
                return _Title;
            }
            set {
                SetPropertyValue("Title", ref _Title, value);
            }
        }

        private string _Publisher;
        public string Publisher {
            get {
                return _Publisher;
            }
            set {
                SetPropertyValue("Publisher", ref _Publisher, value);
            }
        }

    }

}

Assuming we now wish to add an instance of this business object programmatically, add a ViewController to the module and drag a SimpleAction onto the design surface. Handle the Execute event of the SimpleAction to add the new instance.

using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;

using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Actions;
using DevExpress.Persistent.Base;

namespace ObjectSpaceExample.Module {
    public partial class BookController : ViewController {
        public BookController() {
            InitializeComponent();
            RegisterActions(components);
        }

        private void AddBookAction_Execute(object sender, SimpleActionExecuteEventArgs e) {
            Book book = View.ObjectSpace.CreateObject<Book>();
            book.Author = "Me";
            book.Title = "A Book by Me";
            book.Publisher = "Me";
            View.ObjectSpace.CommitChanges();
            View.ObjectSpace.Refresh();
        }
    }
}

Points to note: we use the View’s ObjectSpace to create the new instance of our business object, committing changes when we have done so to persist the object and then calling Refresh() on the ObjectSpace to refresh the View. You can read more about ObjectSpace in our documentation.

Technorati tags: ,
Digg This
Published Sep 09 2008, 12:51 PM by Gary Short (Developer Express)
Filed under: ,
Technorati tags: XAF, XAF Cookbook

Comments

 

Dani Mora said:

Gary, how do you format the C# code in your posts? it's very readable and looks great!

September 9, 2008 5:23 PM
 

Gary Short (Developer Express) said:

I copy it straight out of Visual Studio and paste it into Windows Live Writer using a plugin.

September 10, 2008 4:51 AM
 

wolfgang hauer said:

It has only one problem: the line feeds disappear when i load it as a rss in outlook 2007

Wolfgang

September 10, 2008 4:53 PM
 

Yann Duran said:

Gary, if you were adding multiple objects, in say a loop, you'd only need to commit the changes AFTER you'd finished adding all the objects? Is that right?

Also, what code would you use, if you were adding objects that were not the same object as the objects in the view?

Like if you were looping through each of the objects in the current view & creating an object (obviously a different object) for each object in the view?

Pseudo Code:

For Each object1 in ListView.Objects

   CreateObject(object2)

Next object1

Thanks,

Yann

October 19, 2008 11:46 PM
 

Gary Short (Developer Express) said:

@Yann, Yes if you are creating many objects in a loop then you only have to call CommitChanges() at the end. If you are in a view for ObjectA and you are adding ObjectB and there is a relationship between ObjectA and ObjectB then you would create ObjectB, add it to ObjectA, repeat as required and then call CommitChanges() when finished.

If there is no relationship between ObjectA and ObjectB then you would add ObjectB from a view on ObjectB.

October 20, 2008 4:25 AM
 

Yann Duran said:

Thanks Gary. Assuming I had a SimpleAction that was enabled for the ListView of Object1 as you described above, how would I "add ObjectB from a view on ObjectB" (the scenario I had in mind did not have a relationship between Object1 & Object2)?

Yann

November 5, 2008 7:25 PM
 

HTY Produckt International said:

Hi Gary,

I have the same question as Duran. How do I add an Object2 type object from ListViewA, when no relationship exists ?

Thanks,

Costel.

March 30, 2009 3:29 AM

Leave a Comment

(required)  
(optional)
(required)  
Verification code: Required
   
Add
Copyright © 1998-2009 Developer Express Inc.
ALL RIGHTS RESERVED