Using CommerceBuilder API from standalone windows/consol app

For general questions and discussions specific to the AbleCommerce 7.0 Asp.Net product.
Post Reply
bataras
Ensign (ENS)
Ensign (ENS)
Posts: 16
Joined: Thu Apr 23, 2009 2:29 pm

Using CommerceBuilder API from standalone windows/consol app

Post by bataras » Thu May 07, 2009 10:52 am

I'd like to create a standalone windows or console application that I can use in testing/qa to manipulate data in the database.

This simplest thing I am trying to do is write a windows app that creates a new user and adds him to the admin group.

This is the code in general. It does not run successfully...

User newUser = UserDataSource.CreateUser(txtUsername.Text+"U", txtUsername.Text, txtPassword.Text, string.Empty, string.Empty, true, 0, out status);
User u = UserDataSource.CreateUser(txtUsername.Text, txtPassword.Text);
u.UserGroups.Add(new UserGroup(u.UserId, g.GroupId));
u.Save();

I have a default data source connection string set up in the app config. And it looks like AC is finding it if I inspect various objects in the debugger. For example if do:

Token tok = Token.Instance;

then inspect tok.Database... I can see my connection string in there.

I can't even do something super simple like:

Group g = GroupDataSource.Load(1);

That returns null. No exceptions are throw in any of this so I get no hint as to what the problem is.

How to I do this?

thanks

afm
Captain (CAPT)
Captain (CAPT)
Posts: 339
Joined: Thu Nov 03, 2005 11:52 pm
Location: Portland, OR
Contact:

Re: Using CommerceBuilder API from standalone windows/consol app

Post by afm » Thu May 07, 2009 11:28 am

I think CommerceBuilder uses HttpContext.
Andy Miller
Structured Solutions

Shipper 3 - High Velocity Shipment Processing

bataras
Ensign (ENS)
Ensign (ENS)
Posts: 16
Joined: Thu Apr 23, 2009 2:29 pm

Re: Using CommerceBuilder API from standalone windows/consol app

Post by bataras » Thu May 07, 2009 1:21 pm

I would really like to be able to develop standalone operations/maintenance applications that can be run periodically against the system using the CommerceBuilder API. The alternative is to code directly to the datamodel, but that would not be as good as using the same APIs the site uses to access our business data.

User avatar
nickc
Captain (CAPT)
Captain (CAPT)
Posts: 276
Joined: Thu Nov 29, 2007 3:48 pm

Re: Using CommerceBuilder API from standalone windows/consol app

Post by nickc » Thu May 07, 2009 2:03 pm

I do this using a webservice to expose the necessary AbleCommerce API objects and methods.

bataras
Ensign (ENS)
Ensign (ENS)
Posts: 16
Joined: Thu Apr 23, 2009 2:29 pm

Re: Using CommerceBuilder API from standalone windows/consol app

Post by bataras » Thu May 07, 2009 4:33 pm

I like that idea. I also just figured out how to do it natively. Code...

Code: Select all

            Store store = new Store(1);
//            store.Load(1);
            Token tok = Token.Instance;
            tok.InitStoreContext(store);

            Group g = GroupDataSource.LoadForName("Super Users");
            MembershipCreateStatus status;
            User u = UserDataSource.CreateUser(txtUsername.Text, txtUsername.Text, txtPassword.Text, string.Empty, string.Empty, true, 0, out status);
            u.UserGroups.Add(new UserGroup(u.UserId, g.GroupId));
            u.Save();
The Token Init sequence was what was needed. I'm guessing the Token.Instance is thread local.

Now, the next question is: how do I do the above in an atomic transaction? Ah.. i see... here is transactional code.

Code: Select all

            Store store = new Store();
            store.Load(1);
            Token tok = Token.Instance;
            tok.InitStoreContext(store);
            tok.Database.BeginTransaction(IsolationLevel.Serializable);

            Group g = GroupDataSource.LoadForName("Super Users");
            MembershipCreateStatus status;
            User u = UserDataSource.CreateUser(txtUsername.Text, txtUsername.Text, txtPassword.Text, string.Empty, string.Empty, true, 0, out status);
            u.UserGroups.Add(new UserGroup(u.UserId, g.GroupId));
            u.Save();

            tok.Database.RollbackTransaction();

Post Reply