PageRenderTime 22ms CodeModel.GetById 14ms app.highlight 4ms RepoModel.GetById 2ms app.codeStats 0ms

/V2.2/trunk/Quickstarts/EventAggregation/Desktop/EventAggregation.Tests.AcceptanceTests/TestInfrastructure/DataProvider/DataProviderBase.cs

#
C# | 81 lines | 54 code | 10 blank | 17 comment | 4 complexity | c719d827fabba1ce8219121dd365894c MD5 | raw file
 1//===================================================================================
 2// Microsoft patterns & practices
 3// Composite Application Guidance for Windows Presentation Foundation and Silverlight
 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//===================================================================================
17using System;
18using System.Collections.Generic;
19using System.Linq;
20using System.Text;
21using System.Xml.Serialization;
22using System.Xml;
23
24namespace EventAggregation.AcceptanceTests.TestInfrastructure
25{
26    public abstract class DataProviderBase<TEntity> : IDataProvider<TEntity>,IDisposable
27    {
28        XmlSerializer xmlSerializer = null;
29        XmlTextReader xmlReader = null;
30
31        protected DataProviderBase()
32        {
33            xmlSerializer = new XmlSerializer(typeof(List<TEntity>));
34            
35            string dataFilePath = GetDataFilePath();
36            if (!string.IsNullOrEmpty(dataFilePath))
37            {
38                xmlReader = new XmlTextReader(dataFilePath);
39            }
40        }
41
42        public virtual List<TEntity> GetData()
43        {
44            return (List<TEntity>)(xmlSerializer.Deserialize(xmlReader));
45        }
46
47        public virtual List<TEntity> GetDataForId(string id)
48        {
49            throw new NotImplementedException();
50        }
51
52        public virtual int GetCount()
53        {
54                return ((List<TEntity>)xmlSerializer.Deserialize(xmlReader)).Count;           
55        }
56
57        public abstract string GetDataFilePath();
58        #region IDisposable Members
59
60        public void Dispose()
61        {
62            Dispose(true);
63            GC.SuppressFinalize(this);
64        }
65
66        #endregion
67
68        protected virtual void Dispose(bool disposing)
69        {
70            if (disposing)
71            {
72                if (null != xmlReader)
73                {
74                    xmlReader = null;
75                    //Close the reader.
76                    xmlReader.Close();
77                }
78            }
79        }
80    }
81}