Knowledge is power. We love to share it.

News related to Mono products, services and latest developments in our community.

pajo

MonoX API

04/04/2011Categories: MonoX
MonoX was built to be customization-friendly, but there are so many different real-world scenarios in which you need a custom solution quickly. Very often you can fine tune its functionality without programming, but some solutions do require a lot of background ASP.NET knowledge. To ease the learning curve in this type of scenarios, we have decided to introduce a fully fledged MonoX API that allows developers to easily access and modify MonoX objects and features. Our aim is to create a lightweight and easy to use interface that is going to meet the most common developer's  needs.

MonoX API is built on the top of existing MonoX code repository and adds another layer of abstraction between a developer and the core MonoX engine. At the core of the API are adapters: CRUD API access points used to access and update all MonoX objects. Note that these are POCO objects and are striped down of any unnecessary functionality usually found in more complex frameworks.  All adapters implements IQueryable interface, meaning that you can access MonoX objects using standard LINQ. Since our API is built on the top of the LLBLGen Pro ORM framework, all of our LINQ providers are basically wrappers around LLBLGen LINQ provider. They take care of the subtle details and all of the complexity behind the LLBLGen framework and MonoX architecture. 
Some restrictions were imposed on API LINQ calls. Most importantly, at this moment you cannot issue selects directly in the API query, and all API calls are expected to return MonoX API objects. We'll later learn how to work around this limitation. We have focused on the most common scenarios like filtering (where, first), sorting (orderby) and paging (take, skip). GroupBy statements are not supported in this version.

Now let's see how this work on some examples. We'll start with the most complex object: User. MonoX user objects are built upon ASP.NET membership framework extended by MonoX. The user data is stored accross several database tables, and you need to get familiar with it to load the full object graph. MonoX already comes with repositories that will take care of most of this stuff, but inexperienced users can find accessing user object data somewhat complex.  User API model comes to the rescue as it exposes "flattened" data that is easy to understand and use. Below are a few practical examples:
 
using MonoSoftware.MonoX.API;
 
// Get all users
var allUsers = UserAdapter.GetInstance().ToList();
  
// Prepare query to get common users that registreted in the last 30 days
var commonUsersQuery = UserAdapter.GetInstance()
                .Where(p => (p.Name.FirstName == "John"
                              || p.Email.EndsWith(".com"))
                     && p.DateEntered > DateTime.Now.AddDays(-30));
  
// ToList will execute linq expression and we'll get the list of common users
var commonUsers = commonUsersQuery.ToList();
  
// this will return a list of common users sorted by date they were created
var orderedCommonUsers = commonUsersQuery.OrderBy(p => p.DateEntered).ToList();
We can also use skip and take to control how many users are fetched from the data store. By combining these statements we can page our way through the user collection:

int pageSize = 10;
int pageNumber = 2;
var pagedcommonUsers = commonUsersQuery
                .Skip(pageNumber * pageSize)
                .Take(pageSize).ToList();
 
int recordCount = commonUsersQuery.Count()

This will fetch 10 users on page 2.

Earlier we mentioned that a select expression cannot be directly used on a query. Here is how you can still get the desired functionality:

List<Guid> allUsersId;
             
// This query will throw exception
allUsersId = UserAdapter.GetInstance().Select(p => p.Id).ToList();
 
// This query will return list of users id
allUsersId = UserAdapter.GetInstance().ToList().Select(p => p.Id).ToList();

As you can see the trick is to first call the API provider by calling GetEnumerator(ToArray and ToList will execute GetEnumerator) and then run select expression on an in-memory collection. It's not the most efficient way for getting large lists but in most cases it will work well. 

User adapter can similarly be used to fetch the "friends" collections for the specific user:
 
var user = UserAdapter.GetInstance().First();
var userFriends = UserAdapter.GetInstance().GetFriends(user.Id).ToList();

Calling GetFriends will return IQueryable object that can be used to enumerate the friends collection. You can access the friend requests in a similar fashion.

Here is how to create, update and delete portal users:

var user = UserAdapter.GetInstance().CreateUser("newuser", "newpassword", "email@domain.net");
             
user.MyStatus = "New user";
user = UserAdapter.GetInstance().UpdateUser(user);
 
UserAdapter.GetInstance().DeleteUser(user.Id);

All methods will throw exceptions on all errors. Note that user adapters hold several methods that can be used to manage user relations and requests - please check MonoX documentation for more details.

There are additional adapters for managing user roles, groups and events. You can go ahead and download the new version now to see how they work - please send us your feedback and let us know which functionality would you like to see in the forthcoming versions.
Rated 3.38, 8 vote(s). 
Some "using" statements would be useful to have