Monday, January 18, 2010

Collaborating with Xaf and IO module

.NET provides a lot of ways to collaborate between systems. All of them are based on data serialization. XPO is missing that, and have a client project that could not live without some generic type of serialization. So I create a simple but powerful IO engine

  • It can serialize any object that inherits DevExpress.Xpo.XPBaseObject.
  • It will also serialize any object that is related to the root object of serialization.
  • To control the export engine A serialization graph can be provided .
  • Object's properties can be serialized according to Serialization Strategy enumeration 

    public enum SerializationStrategy

    {

        SerializeAsValue = 0,

        SerializeAsObject,

        DoNotSerialize

    }

  • Friendly keys support. For importing data from other RDBMS systems that do not have a GUID primary key

Scenario

  1. From a non xaf application I need to import customers based on some filtering taken from a xaf application.
  2. The design of Customer object differs between applications. In the Xaf application Customer inherits user but in the non Xaf does not. Property names also differ

The above problems can be solved with IO module with easy

2 apps communicating
Lets try to build a WCF service to enable communication. We are going to host it on IIS so create a new WCF Service Application

 image
reference the following assemblies take a not that I also reference Solution3.Web which is my client front end

 image

add an HttpModule to setup an application instance at HttpApplication init. We need to setup an application instance to be sure that the XPDictionary modifications through controllers take place

image

    public class InitXafAppModule:IHttpModule

    {

        public void Init(HttpApplication context) {

            WebApplication.SetInstance(context.Session, new Solution3AspNetApplication());

 

            if (ConfigurationManager.ConnectionStrings["ConnectionString"] != null) {

                WebApplication.Instance.ConnectionString =

                    ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

            }

 

            WebApplication.Instance.Setup();

            WebApplication.Instance.Start();

 

        }

 

        public void Dispose() {

 

        }

    }


Our service is going to have the following contract

    [ServiceContract]

    public interface IIOService {

        [OperationContract]

        string Export(string typeName, string criteria);

 

        [OperationContract]

        void Import(string xml);

    }

and implemented as

    public class IOService : IIOService

    {

        public string Export(string typeName, string criteria)

        {

            var session = WebApplication.Instance.ObjectSpaceProvider.CreateUpdatingSession();

            var xpBaseObjects = new XPCollection(session, ReflectionHelper.GetType(typeName), criteria).OfType<XPBaseObject>();

            XDocument xDocument = new ExportEngine().Export(xpBaseObjects);

            var stringWriter = new StringWriter();

            xDocument.Save(stringWriter);

            return stringWriter.GetStringBuilder().ToString();

        }

 

        public void Import(string xml)

        {

            var unitOfWork = new UnitOfWork(WebApplication.Instance.ObjectSpaceProvider.CreateUpdatingSession().DataLayer);

            var importEngine = new ImportEngine();

            importEngine.ImportObjects(XDocument.Parse(xml), unitOfWork);

            unitOfWork.CommitChanges();

        }

    }

Now we are able to call the WCF service to get the xml of Customers of the Xaf application and transform it using extension methods defined in IO module like

var xElement = XDocument.Parse(xml).Root;


IEnumerable<NonXafCustomer> customers =xElement.SerializedObjects("Customer").Select(element => new NonXafCustomer {Name = element.Property("UserName").Value});



ps:Import/Export action within a xaf application are inside export predefined category and they are available for all objects



image

Subscribe to XAF feed
Subscribe to community feed

DiggIt!
blog comments powered by Disqus