Click here to Skip to main content
15,891,746 members
Articles / Programming Languages / C#

Attribute-flavored Domain Driven Design

Rate me:
Please Sign up or sign in to vote.
4.00/5 (2 votes)
6 Apr 2008CPOL8 min read 39.2K   231   25  
A centralized business domain with a common base
using System;
using System.IO;
using System.Reflection;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Formatters.Soap;
using System.Windows.Forms;
using Codebasement.DomainDrivenDesign.Basement.Basement;

namespace Codebasement.DomainDrivenDesign.Basement.Serialization
{
    /// <summary>
    /// Summary description for RepositoryManager.
    /// </summary>
    public sealed class SoapSerializationProvider : ISerializationProvider
    {
        private BasementObject _basementObject;
        private string _fileName;

        #region ISerializationProvider Members

        /// <summary>
        /// Saves this instance.
        /// </summary>
        /// <param name="basementObject">The basement object.</param>
        /// <param name="fileName">Name of the file.</param>
        public void Save(BasementObject basementObject,
                         string fileName)
        {
            if (String.IsNullOrEmpty(fileName))
                return;

            if (basementObject != null)
            {
                basementObject.SetCleanState();

                _basementObject = basementObject;

                _fileName = fileName;
                Serialize();
            }
        }


        /// <summary>
        /// Loads the specified file name.
        /// </summary>
        /// <param name="type"></param>
        /// <param name="fileName">Name of the file.</param>
        /// <returns></returns>
        public BasementObject Load(Type type, string fileName)
        {
            if (String.IsNullOrEmpty(fileName))
                return null;

            _fileName = fileName;
            Deserialize();

            if (_basementObject != null)
            {
                _basementObject.SetCleanState();
            }

            return _basementObject;
        }

        #endregion

        private void Serialize()
        {
            FileStream fileStream = new FileStream(_fileName, FileMode.Create, FileAccess.Write);
            using (fileStream)
            {
                try
                {
                    SoapFormatter soapFormatter = new SoapFormatter();
                    soapFormatter.AssemblyFormat = FormatterAssemblyStyle.Simple;
                    soapFormatter.TypeFormat = FormatterTypeStyle.TypesWhenNeeded;
                    soapFormatter.Serialize(fileStream, _basementObject);
                }
                catch (SerializationException e)
                {
                    MessageBox.Show("Cannot save file. Reason: " + e.Message);
                    //throw;
                }
                finally
                {
                    fileStream.Close();
                }
            }
        }


        private void Deserialize()
        {
            if (File.Exists(_fileName))
            {
                FileStream fileStream = new FileStream(_fileName, FileMode.Open);
                using (fileStream)
                    try
                    {
                        SoapFormatter soapFormatter = new SoapFormatter();
                        soapFormatter.AssemblyFormat = FormatterAssemblyStyle.Simple;
                        soapFormatter.TypeFormat = FormatterTypeStyle.TypesWhenNeeded;
                        AppDomain.CurrentDomain.AssemblyResolve += OnCurrentDomainAssemblyResolve;
                        _basementObject = (BasementObject) soapFormatter.Deserialize(fileStream);
                        AppDomain.CurrentDomain.AssemblyResolve -= OnCurrentDomainAssemblyResolve;
                    }
                    catch (SerializationException e)
                    {
                        MessageBox.Show("Cannot load file. Reason: " + e.Message);
                        //throw;
                    }
                    finally
                    {
                        fileStream.Close();
                    }
            }
        }

        private static Assembly OnCurrentDomainAssemblyResolve(object sender, ResolveEventArgs e)
        {
            string[] assemblyToFind = e.Name.Split(',');
            string assemblyName = assemblyToFind[0];

            foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
            {
                if (assemblyName == assembly.GetName().Name)
                {
                    return assembly;
                }
            }

            return null;
        }
    }
}

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 Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Netherlands Netherlands
Software engineer & architect.

Comments and Discussions