Blog Home  Home Feed your aggregator (RSS 2.0)  
A Random Walk Around .Net - Wiring It Up
 
 Wednesday, April 09, 2008

Quite shockingly, we have etched out an n-tier service oriented system without writing a single line of code.  Moreover, we are following best practices to decouple the data layer, business layer, service layer and presentation layer. Generally, we do this for two reasons: one, to promote the idea of separation of concerns and two, to allow for future flexibility when addressing change sets.

The wiring process can be summed up as adding business entities and logic to link the data layer to the service layer.  Business entities should be though of as objects that hold state.  Business logic should be thought of functions or processes that affect business entities.  This is gray definition, because in some cases business entities might have functionality.  An example of a business object is a person; who has a name and an age.  However, this person may have an internal process called heartbeat: thus the grayness.

We will define one business entity (for now) called “ConfigurationEntity”.  This entity will have two properties: “LookupName” and “Xml”.  The “LookupName” will be used to find the castle configuration in the database.  The “Xml” will hold the dynamically generated xml configuration from the database.

Before we proceed, generally I would unit test at this point.  Test Driven Development (TDD) states: we should write tests first then code.  However, because it would require the use of mocks, I will refrain until a later post.

            Next, we will use a factory pattern to generate the xml from the database.  Again, we are choosing a design that provides for flexibility during future change sets.  The factory uses an enumeration to decide which type of process to return.

            Finally, we have to generate translators to and from our service layer data contracts to our business layer business entities.  These translators will be used in our service implementation to allow access to our business logic.

            

Class Diagram:

Factory:

namespace DBinsor.Svc.WCF.BusinessLogic
{
    public static class ResourceFactory
    {
        public static IResource GenerateResouce(ResourceLocations location)
        {
            switch (location)
            {
                case ResourceLocations.Database:
                    return new ResourceFromDatabase();
                default:
                    throw new ArgumentException("Location Not Supported.", "location");
            }
        }
    }
}

Finally, we have to generate translators to and from our service layer data contracts to our business layer business entities. 

These translators will be used in our service implementation to allow access to our business logic.

 

namespace DBinsor.Svc.WCF.ServiceImplementation
{
    public static class TConfigurationEntityAndConfigurationResponse
    {
        public static ConfigurationResponse Convert(ConfigurationEntity from)
        {
            ConfigurationResponse to = new ConfigurationResponse();
            to.ConfigurationPart = new ConfigurationPart();
            to.ConfigurationPart.XmlConfiguration = new XmlConfiguration();
            to.ConfigurationPart.XmlConfiguration.Config = from.Xml.ToString();
            return to;
        }
    }
}
namespace DBinsor.Svc.WCF.ServiceImplementation
{
    public static class TConfigurationEntityAndConfigurationRequest
    {
        public static ConfigurationEntity Convert(ConfigurationRequest from)
        {
            return new ConfigurationEntity(from.CastleId.LookupName);
        }
    }
}
namespace DBinsor.Svc.WCF.ServiceImplementation
{
    public partial class ConfigurationService
    {
        public override ConfigurationResponse GetConfigurationDemand(ConfigurationRequest request)
        {
            IResource businessLogic = ResourceFactory.GenerateResouce(ResourceLocations.Database);
            return TConfigurationEntityAndConfigurationResponse.Convert(
                        businessLogic.GenerateXml(
                                TConfigurationEntityAndConfigurationRequest.Convert(
                                        request)));
        }
    }
}
Wednesday, April 09, 2008 2:44:50 PM (Central Standard Time, UTC-06:00)  #    Comments [0]    | 
Comments are closed.
Copyright © 2009 Yezdaan Baber. All rights reserved.
DasBlog 'Portal' theme by Johnny Hughes.
Pick a theme: