Index: DamEngine/trunk/src/Deltares.DamEngine.Calculators/KernelWrappers/Assemblers/DtoAssembler.cs =================================================================== diff -u -r4000 -r4052 --- DamEngine/trunk/src/Deltares.DamEngine.Calculators/KernelWrappers/Assemblers/DtoAssembler.cs (.../DtoAssembler.cs) (revision 4000) +++ DamEngine/trunk/src/Deltares.DamEngine.Calculators/KernelWrappers/Assemblers/DtoAssembler.cs (.../DtoAssembler.cs) (revision 4052) @@ -26,247 +26,246 @@ using System.Reflection; using System.Xml.Linq; -namespace Deltares.DamEngine.Calculators.KernelWrappers.Assemblers +namespace Deltares.DamEngine.Calculators.KernelWrappers.Assemblers; + +#region DtoAssembler definition + +/// +/// Default implementation for the interface +/// +/// The domain object type +public abstract class DtoAssembler : IDtoAssembler + where TDomainObject : new() { - #region DtoAssembler definition + /// + /// Holds the property mappings + /// + private readonly DtoPropertyAttributeMap map = new DtoPropertyAttributeMap(); /// - /// Default implementation for the interface + /// Gets the property mappings collection /// - /// The domain object type - public abstract class DtoAssembler : IDtoAssembler - where TDomainObject : new() + protected DtoPropertyAttributeMap Mappings { - /// - /// Holds the property mappings - /// - private readonly DtoPropertyAttributeMap map = new DtoPropertyAttributeMap(); - - /// - /// Gets the property mappings collection - /// - protected DtoPropertyAttributeMap Mappings + get { - get - { - return map; - } + return map; } + } - /// - /// - /// - protected void InitializeMappings() - { - var tmp = new TDomainObject(); - map.AddOrUpdateRange(tmp.ToPropertyMap()); - } + /// + /// + /// + protected void InitializeMappings() + { + var tmp = new TDomainObject(); + map.AddOrUpdateRange(tmp.ToPropertyMap()); + } - /// - /// - /// - protected void InitializeMappings(string elementName, XDocument xsdReferenceDocument) + /// + /// + /// + protected void InitializeMappings(string elementName, XDocument xsdReferenceDocument) + { + var tmp = new TDomainObject(); + IEnumerable tmpMap = tmp.ToPropertyMap(elementName, xsdReferenceDocument); + map.AddOrUpdateRange(tmpMap); + } + + /// + /// Adds or updates a property-to-attribute mapping item into the property map + /// + /// The name of the property + /// The name of the attribute to map to + protected void AddOrUpdateMapping(string propertyName, string attributeName) + { + AddOrUpdateMapping(propertyName, attributeName, DtoPropertyImportance.Optional); + } + + /// + /// Adds or updates a property-to-attribute mapping item into the property map + /// + /// The name of the property + /// The name of the attribute to map to + /// The usage or importance of the value i.e. reguired or optional + protected void AddOrUpdateMapping(string propertyName, string attributeName, DtoPropertyImportance importance) + { + if (!propertyName.HasValidStringValue()) { - var tmp = new TDomainObject(); - IEnumerable tmpMap = tmp.ToPropertyMap(elementName, xsdReferenceDocument); - map.AddOrUpdateRange(tmpMap); + throw new DtoPropertyMapException("propertyName is not valid (empty or null)"); } - /// - /// Adds or updates a property-to-attribute mapping item into the property map - /// - /// The name of the property - /// The name of the attribute to map to - protected void AddOrUpdateMapping(string propertyName, string attributeName) + bool q = (from x in new TDomainObject().GetType().GetProperties() + where x.Name == propertyName + select x).Any(); + + if (!q) { - AddOrUpdateMapping(propertyName, attributeName, DtoPropertyImportance.Optional); + throw new DtoPropertyMapException("The property you want to add or update doesnt exist in the property list of the domain object."); } - /// - /// Adds or updates a property-to-attribute mapping item into the property map - /// - /// The name of the property - /// The name of the attribute to map to - /// The usage or importance of the value i.e. reguired or optional - protected void AddOrUpdateMapping(string propertyName, string attributeName, DtoPropertyImportance importance) - { - if (!propertyName.HasValidStringValue()) - { - throw new DtoPropertyMapException("propertyName is not valid (empty or null)"); - } + Mappings.AddOrUpdate(propertyName, attributeName, importance); + } - bool q = (from x in new TDomainObject().GetType().GetProperties() - where x.Name == propertyName - select x).Any(); + #region IDtoAssembler Members - if (!q) - { - throw new DtoPropertyMapException("The property you want to add or update doesnt exist in the property list of the domain object."); - } + /// + /// + /// + /// + /// + public abstract TDomainObject CreateDomainObject(TDataTransferObject dtoObj); - Mappings.AddOrUpdate(propertyName, attributeName, importance); - } + /// + /// + /// + /// + /// + public abstract TDataTransferObject CreateDataTransferObject(TDomainObject domainObj); - #region IDtoAssembler Members + #endregion +} - /// - /// - /// - /// - /// - public abstract TDomainObject CreateDomainObject(TDataTransferObject dtoObj); +#endregion - /// - /// - /// - /// - /// - public abstract TDataTransferObject CreateDataTransferObject(TDomainObject domainObj); +#region DtoAssembler - #endregion - } +/// +/// Default implementation for the interface +/// +/// +/// This base class inherits from this class just fixes +/// the dto type to XElement +/// +/// The domain object type +public abstract class DtoAssembler : DtoAssembler, IDtoAssembler + where TDomainObject : new() +{ + /// + /// Initializes a new instance of the DtoAssembler class + /// + /// Sets the element name + /// The text reader stream for reading in ther xsd file which is embedded as an resource + public DtoAssembler(string elementName, Stream xsdStream) + : this(elementName, null, xsdStream) {} - #endregion + /// + /// Initializes a new instance of the DtoAssembler class + /// + /// Sets the element name + /// The text reader stream for reading in ther xsd file which is embedded as an resource + public DtoAssembler(string elementName, string elementNamespace) + : this(elementName, elementNamespace, null) {} - #region DtoAssembler - /// - /// Default implementation for the interface + /// Initializes a new instance of the DtoAssembler class /// - /// - /// This base class inherits from this class just fixes - /// the dto type to XElement - /// - /// The domain object type - public abstract class DtoAssembler : DtoAssembler, IDtoAssembler - where TDomainObject : new() + /// Sets the element name + /// The text reader stream for reading in ther xsd file which is embedded as an resource + public DtoAssembler(string elementName, string elementNamespace, Stream xsdStream) { - /// - /// Initializes a new instance of the DtoAssembler class - /// - /// Sets the element name - /// The text reader stream for reading in ther xsd file which is embedded as an resource - public DtoAssembler(string elementName, Stream xsdStream) - : this(elementName, null, xsdStream) {} + ElementName = elementName; + ElementNamespace = elementNamespace; - /// - /// Initializes a new instance of the DtoAssembler class - /// - /// Sets the element name - /// The text reader stream for reading in ther xsd file which is embedded as an resource - public DtoAssembler(string elementName, string elementNamespace) - : this(elementName, elementNamespace, null) {} - - /// - /// Initializes a new instance of the DtoAssembler class - /// - /// Sets the element name - /// The text reader stream for reading in ther xsd file which is embedded as an resource - public DtoAssembler(string elementName, string elementNamespace, Stream xsdStream) + if (xsdStream == null) { - ElementName = elementName; - ElementNamespace = elementNamespace; - - if (xsdStream == null) - { - xsdStream = GetXsdStreamFromEmbeddedResource(); - } - - XDocument doc = XDocument.Load(new StreamReader(xsdStream)); - Schema = doc; - InitializeMappings(elementName, doc); + xsdStream = GetXsdStreamFromEmbeddedResource(); } - /// - /// Gets or sets the namespace value for the element - /// - public string ElementNamespace { get; } + XDocument doc = XDocument.Load(new StreamReader(xsdStream)); + Schema = doc; + InitializeMappings(elementName, doc); + } - /// - /// Gets the xml schema (if any) - /// - public XDocument Schema { get; } + /// + /// Gets or sets the namespace value for the element + /// + public string ElementNamespace { get; } - public Stream GetXsdStreamFromEmbeddedResource() + /// + /// Gets the xml schema (if any) + /// + public XDocument Schema { get; } + + public Stream GetXsdStreamFromEmbeddedResource() + { + if (string.IsNullOrEmpty(ElementNamespace)) { - if (string.IsNullOrEmpty(ElementNamespace)) - { - return null; - } + return null; + } - Assembly assembly = GetType().Assembly; - string schemaName = ElementNamespace.Substring(ElementNamespace.LastIndexOf('/') + 1); - schemaName += schemaName.EndsWith(".xsd") ? "" : ".xsd"; + Assembly assembly = GetType().Assembly; + string schemaName = ElementNamespace.Substring(ElementNamespace.LastIndexOf('/') + 1); + schemaName += schemaName.EndsWith(".xsd") ? "" : ".xsd"; - string resourceName = (from string name in assembly.GetManifestResourceNames() - where name.EndsWith(schemaName) - select name).Single(); + string resourceName = (from string name in assembly.GetManifestResourceNames() + where name.EndsWith(schemaName) + select name).Single(); - return assembly.GetEmbeddedFile(resourceName); - } + return assembly.GetEmbeddedFile(resourceName); + } - public virtual TDomainObject CreateDomainObject(XDocument dtoDocument) + public virtual TDomainObject CreateDomainObject(XDocument dtoDocument) + { + if (dtoDocument == null) { - if (dtoDocument == null) - { - throw new ArgumentNullException("dtoDocument"); - } + throw new ArgumentNullException("dtoDocument"); + } - XElement el = null; - el = (from x in dtoDocument.Descendants() - where x.Name.LocalName == ElementName - select x).Single(); + XElement el = null; + el = (from x in dtoDocument.Descendants() + where x.Name.LocalName == ElementName + select x).Single(); - return CreateDomainObject(el); - } + return CreateDomainObject(el); + } - #region IDtoAssembler Members + #region IDtoAssembler Members - /// - /// Gets the xml element name - /// - public string ElementName { get; } + /// + /// Gets the xml element name + /// + public string ElementName { get; } - /// - /// Creates and materialized a data tranfer object (dto) from a domain object - /// - /// The domain object - /// An XElement instance - public override XElement CreateDataTransferObject(TDomainObject domainObj) + /// + /// Creates and materialized a data tranfer object (dto) from a domain object + /// + /// The domain object + /// An XElement instance + public override XElement CreateDataTransferObject(TDomainObject domainObj) + { + if (domainObj == null) { - if (domainObj == null) - { - return null; - } + return null; + } - if (!ElementName.HasValidStringValue()) - { - throw new DtoAssemblerException("There is no element name specified"); - } - - if (ElementNamespace.HasValidStringValue()) - { - XNamespace ns = ElementNamespace; - return new XElement(ns + ElementName) - .Materialize(domainObj, Mappings); - } - - return new XElement(ElementName) - .Materialize(domainObj, Mappings); + if (!ElementName.HasValidStringValue()) + { + throw new DtoAssemblerException("There is no element name specified"); } - /// - /// Creates and materializes a domain object from a data tranfer object (dto) - /// - /// The dto object - /// The materialized domain object - public override TDomainObject CreateDomainObject(XElement dto) + if (ElementNamespace.HasValidStringValue()) { - return new TDomainObject().Materialize(dto, Mappings); + XNamespace ns = ElementNamespace; + return new XElement(ns + ElementName) + .Materialize(domainObj, Mappings); } - #endregion + return new XElement(ElementName) + .Materialize(domainObj, Mappings); } + /// + /// Creates and materializes a domain object from a data tranfer object (dto) + /// + /// The dto object + /// The materialized domain object + public override TDomainObject CreateDomainObject(XElement dto) + { + return new TDomainObject().Materialize(dto, Mappings); + } + #endregion -} \ No newline at end of file +} + +#endregion \ No newline at end of file