Click here to Skip to main content
15,892,298 members
Articles / Web Development / ASP.NET

How to Build Flexible and Reusable WCF Services

Rate me:
Please Sign up or sign in to vote.
4.85/5 (13 votes)
6 May 2013GPL313 min read 77.1K   1.9K   126  
Design Patterns and best practices for building flexible and reusable WCF services.
using System;
using System.Collections.Generic;
using System.Linq;
using Xomega.Framework;
using Xomega.Framework.Properties;
using Xomega.Framework.Lookup;
using System.ServiceModel;
using AdventureWorks.Services;
using System.Collections;

namespace AdventureWorks.Client.Objects
{
    public class StateProperty : EnumIntProperty
    {
        public const string EnumTypeStates = "states";
        public const string AttributeCountry = "country";
        public const string AttributeAbbrev = "abbrev";

        public StateProperty(DataObject parent, string name)
            : base(parent, name)
        {
            EnumType = EnumTypeStates;
        }

        protected override LookupTable GetLookupTable()
        {
            LookupTable res = base.GetLookupTable();
            if (res == null)
            {
                List<Header> data = new List<Header>();
                foreach (State st in GetStates())
                {
                    Header h = new Header(EnumType, "" + st.StateProvinceId, st.Name);
                    h[AttributeCountry] = st.CountryRegionCode;
                    h[AttributeAbbrev] = st.StateProvinceCode;
                    data.Add(h);
                }
                res = new LookupTable(EnumType, data, true);
                LookupCache cache = LookupCache.Get(CacheType);
                if (cache != null) cache.CacheLookupTable(res);
            }
            return res;
        }

        protected virtual IEnumerable<State> GetStates()
        {
            ChannelFactory<IStateProvinceService> factory = new ChannelFactory<IStateProvinceService>(typeof(IStateProvinceService).Name);
            IStateProvinceService svc = factory.CreateChannel();
            List<State> output = svc.ReadList();
            factory.Close();

            return output;
        }
    }

    public class EmployeeStateProperty : StateProperty
    {
        public const string EnumTypeEmpStates = "employee states";

        public EmployeeStateProperty(DataObject parent, string name)
            : base(parent, name)
        {
            EnumType = EnumTypeEmpStates;
        }

        protected override IEnumerable<State> GetStates()
        {
            ChannelFactory<IEmployeeService> factory = new ChannelFactory<IEmployeeService>(typeof(IEmployeeService).Name);
            IEmployeeService svc = factory.CreateChannel();
            List<State> output = svc.GetStates();
            factory.Close();

            return output;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Architect Xomega.Net
United States United States
Xomega Team is striving to increase productivity and development quality by utilizing Model Driven Development coupled with Code Generation and the best design practices for application development.
We provide MDD tools, code generators and frameworks for Visual Studio and .Net development.
Visit us at http://www.xomega.net
This is a Organisation

1 members

Comments and Discussions