PageRenderTime 30ms CodeModel.GetById 17ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/V1/trunk/Source/QuickStarts/Commanding/Commanding.Tests.AcceptanceTests/TestInfrastructure/DataProviderBase.cs

#
C# | 82 lines | 54 code | 11 blank | 17 comment | 4 complexity | f77c3633fdd3033dfd03ffa1fedfd24f MD5 | raw file
 1//===============================================================================
 2// Microsoft patterns & practices
 3// Composite Application Guidance for Windows Presentation Foundation
 4//===============================================================================
 5// Copyright (c) Microsoft Corporation.  All rights reserved.
 6// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
 7// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
 8// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 9// FITNESS FOR A PARTICULAR PURPOSE.
10//===============================================================================
11// The example companies, organizations, products, domain names,
12// e-mail addresses, logos, people, places, and events depicted
13// herein are fictitious.  No association with any real company,
14// organization, product, domain name, email address, logo, person,
15// places, or events is intended or should be inferred.
16//===============================================================================
17
18using System;
19using System.Collections.Generic;
20using System.Linq;
21using System.Text;
22using System.Xml.Serialization;
23using System.Xml;
24
25namespace Commanding.AcceptanceTests.TestInfrastructure
26{
27    public abstract class DataProviderBase<TEntity> : IDataProvider<TEntity>,IDisposable
28    {
29        XmlSerializer xmlSerializer = null;
30        XmlTextReader xmlReader = null;
31
32        protected DataProviderBase()
33        {
34            xmlSerializer = new XmlSerializer(typeof(List<TEntity>));
35            
36            string dataFilePath = GetDataFilePath();
37            if (!(String.IsNullOrEmpty(dataFilePath)))
38            {
39                xmlReader = new XmlTextReader(dataFilePath);
40            }
41        }
42
43        public virtual List<TEntity> GetData()
44        {
45            return (List<TEntity>)(xmlSerializer.Deserialize(xmlReader));
46        }
47
48        public virtual List<TEntity> GetDataForId(string id)
49        {
50            throw new NotImplementedException();
51        }
52
53        public virtual int GetCount()
54        {
55                return ((List<TEntity>)xmlSerializer.Deserialize(xmlReader)).Count;            
56        }
57
58        public abstract string GetDataFilePath();
59        #region IDisposable Members
60
61        public void Dispose()
62        {
63            Dispose(true);
64            GC.SuppressFinalize(this);
65        }
66
67        #endregion
68
69        protected virtual void Dispose(bool disposing)
70        {
71            if (disposing)
72            {
73                if (null != xmlReader)
74                {
75                    xmlReader = null;
76                    //Close the reader.
77                    xmlReader.Close();
78                }
79            }
80        }
81    }
82}