CubeFoundation is a framework for building application level logic components for web applications.
At the core of CubeFoundation is a entity framework built on the ABSTRACT FACTORY pattern, a basic O/R mapper, a caching system (a wrapper around the ASP.NET caching system) , transparent caching of entities and a framework for managing modules that are started when the application starts.
CubeFoundation also provides a few basic modules for categorizing entities and attaching attributes to entities at runtime. It also provides its own basic system for managing users which integrates with the Membership model and a simple file system for usage in other modules.
Each of the above mentioned features lives in its own namespace and are described in more detail below.

CubeFoundation.Entity

The three most important interfaces and classes in the CubeFoundation.Entity namespace are the IEntity and IEntityProvider interfaces and the EntityManager class. They implement the ABSTRACT FACTORY pattern to make it possible to interchange both entity classes and their corresponding factories, even at runtime. These and some other important classes in the CubeFoundation.Entity namespace are described further below.

The IEntity and IEntityProvider interfaces

The CubeFoundation.Entity namespace contains, among other things, two interfaces named IEntity and IEntityProvider. For a class to implement IEntity it needs to have a nullable integer property named ID and be marked with the EntityProvider attribute in which an EntityProviderType must be set. The EntityProviderType must be set to a type that implements the IEntityProvider interface.
A class that implements the IEntityProvider interface must contain three methods:
  • IEntity Get(int id, Type type);
  • IEntity Save(IEntity entity);
  • void Remove(IEntity entity);
Note that in many cases you do not need to create a new EntityProvider for a new entity as you can use the BasicEntityProvider class in the CubeFoundation.Entity.GenericProviders namespace.
!!The EntityManager class
The static EntityManager class is responsible for keeping track of all entity types (concrete classes that implement IEntity). This means that is does three things:
  • When the application starts it finds all entity types and assigns each type an integer ID which is saved to the database. This information is made public by the two methods :
    • int GetEntityTypeID(Type entityType) and
    • Type GetEntityType(int entityTypeID).
  • It maintains a mapping between entity types and their corresponding EntityProviders. This mapping is made public by a public method:
    • IEntityProvider GetEntityProviderByEntityType(Type entityType)
  • It also contains events that are fired when entities are created, updated, being removed and have been removed. Note that if you build a custom EntityProvider it is your responsibility to tell the EntityManager when these events should be fired.

The GenericProviders.BasicEntityProvider class

The BasicEntityProvider class, which resides in the CubeFoundation.Entity.GenericProviders namespace, is a standard implementation of a EntityProvider that saves entities to, and reads entities from, a SQL Server database. It uses the O/R mapping provided by the CubeFoundation.Data namespace (specifically the EntityDatabaseMapping and DatabaseMapping attributes).

The EntityBase class

The abstract EntityBase class is a class that implements the IEntity, IFreezable and ICachable interfaces. For most entity types that are built from scratch (as opposed to entities from external systems which you inherit to implement the IEntity interface) the EntityBase class is a good base class.

The IFreezable interface

When a class implements the IFreezable interface it should not be possible to make changes to it, or save it, without creating a writable clone. Note that it is up to you to enforce these rules when creating custom entity types or custom entity providers that do not inherit from BasicEntityProvider.

The EntityFactoryBase class

The abstract EntityFactoryBase class is a good base class for creating factory classes. It contains generic methods for retrieving, saving and removing entities. These methods in turn ask the EntityManager class for the entity provider for the specified type and retrieves, saves or removes the entity. Often, when you create a custom module, your factory class has a central role as it not only provides the methods to retrieve, save and remove entities but also for fetching collections of entities and enforcing business logic.

CubeFoundation.Data

The database abstraction classes

The database abstraction classes are Database, DatabaseCommand, DatabaseManager and Transaction classes. These classes provide a layer of abstraction for working with databases. Their main purpose are to simplify common tasks such as building database commands and dealing with transactions. In theory they should also enable working with other databases than SQL Server. This has however not yet been fully implemented and it is not a prioritized feature.
The DatabaseManager.Databases property exposes a Dictionary containing instances of Database. The databases in the Dictionary are all the databases represented by connectionstrings in web.config with the provider set to System.Data.SqlClient. The Dictionary key is the name of the connectionstring.
A default database is exposed by CubeFoundation.FoundationCore.DefaultDatabase and is configured in web.config in the dataBaseConfiguration setting.

The EntityDatabaseMapping and AttributeDatabaseMapping attributes

Along with the DataItemHelper class the EntityDatabaseMapping and DatabaseMapping attributes make up the basic O/R mapping in CubeFoundation.
The EntitiyDatabaseMapping attribute specifies how an instance of a class should be saved, read, updated and removed in a database. At the moment only mapping to stored procedures are supported.
The AttributeDatabaseMapping maps a field or property in an instance of a class to a column name and a stored procedure parameter name. It also specifies whether the value of the field or property should be used when creating, updating and removing the instance.

The DataItemHelper class

The static DataItemHelper class contains a set of methods for reading the EntityDatabaseMapping and DatabaseMapping attributes set on entity classes. An example of one of these methods are public static object GetItemObjectFromReader(IDataReader reader, Type type) which returns an instance of the specified type constructed from the values in the current row in a reader.
Many of the methods in DataItemHelper are heavily used in the BasicEntityProvider class as it is a basic implementation of a entity provider for entities that utilize the O/R mapping provided by the EntityDatabaseMapping and DatabaseMapping attributes.

CubeFoundation.Cache

The CubeFoundation.Cache namespace most interesting class is the static CacheManager class. In its current implementation it wraps the ASP.NET cache and extends it’s with better ways to create dependencies between cached objects and/or cache keys. It also provides functionality (the GetObject method overloads that has a delegate parameter) for making sure that when several attempts to retrieve an object that will be cached upon the first attempt are made simultaneously only one at a time is actually executed.
When adding an object to the cache you may choose from three different cache times. It is possible to configure these in web.config by adding a cacheConfiguration section to the cubeFoundation sectiongroup.
As the CacheManager and other classes in the Cache namespace adds a layer of abstraction on top of the ASP.NET cache it is possible to change from using the ASP.NET cache to other forms of caching, such as memcached. At the moment this would however require changes to the source code of the CacheManager. Making this configurable is something we may add in the future.

CubeFoundation.Attribute

The classes in the CubeFoundation.Attribute namespace will make up a system for attaching custom attributes to any type that implements the IEntity interface. This functionality is not finished in the alpha (0.8) release of CubeFoundation.

CubeFoundation.Module

The classes in the CubeFoundation.Module namespace help you to create modules that will be started when CubeFoundation starts. This topic will soon be described in more detail.

CubeFoundation.Category

The classes in the CubeFoundation.Category namespace make up a system for categorizing any object that implements the IEntity interface. This topic will soon be described in more detail.

CubeFoundation.UserData

The classes in the CubeFoundation.UserData namespace make up CubeFoundations own user system which extends the Membership model in ASP.NET. This topic will soon be described in more detail.

CubeFoundation.FileSystem

The classes in the CubeFoundation.FileSystem namespace make up a very simple file system built on the provider model. This topic will soon be described in more detail.

Last edited Feb 8, 2009 at 6:51 PM by joelabrahamsson, version 6

Comments

No comments yet.