如何使用XmlSerializer创建XmlRepository?

时间:2012-04-05 17:37:46

标签: c# xml xml-serialization repository repository-pattern

我很难尝试创建XmlRepository。这里的问题我有唯一的选择,使用XmlSerializer。

请检查一下。它真的是我的代码混乱和令人沮丧。我想知道如何改进这段代码,我正在考虑创建一个单例,但我不确定如何继续。

 public interface IRepository<T>
    where T : class
{
    T GetById(object id);
    IEnumerable<T> All();
    void Insert(T entity);
    void Remove(T entity);
    void SaveChanges();
} 

public class XmlRepository : IRepository<Configuration>
{
    public XmlRepository(string filename)
    {
        FileName = filename;
    }

    public XmlRepository(string filename)
    {
        FileName = filename;
    }

    internal string FileName { get; private set; }

    private Configuration GetById(object id)
    {
        throw new NotImplementedException();
    }

    public IEnumerable<Configuration> All()
    {
        return Get();
    }

    public void Insert(Configuration entity)
    {
        var configurations = Get();
        configurations.Add(entity);
        Save(configurations);
    }

    public void Remove(Configuration entity)
    {
        var configurations = Get();
        configurations.Remove(entity);
        Save(configurations);
    }

    private List<Configuration> Get()
    {
        try
        {
            XmlSerializer serializer = new XmlSerializer(typeof(List<Configuration>), null, new Type[] { typeof(BinaryConfiguration) }, new XmlRootAttribute("Configurations"), "http://ofimbres.wordpress.com/");
            StreamReader myWriter = new StreamReader(FileName);
            var list = serializer.Deserialize(myWriter);
            myWriter.Close();

            return (List<Configuration>)list;
        }
        catch (InvalidOperationException ex)
        {
            throw ex;
        }
    }

    public void Save(object configurations)
    {
        try
        {
            XmlSerializer serializer = new XmlSerializer(configurations.GetType(), null, new Type[] { typeof(BinaryConfiguration) }, new XmlRootAttribute("Configurations"), "http://ofimbres.wordpress.com/");
            StreamWriter myWriter = new StreamWriter(FileName);
            serializer.Serialize(myWriter, configurations);
            myWriter.Close();
        }
        catch (XmlException ex)
        {
            throw ex;
        } 
    }
}

有任何疑问,请告诉我。 非常感谢

1 个答案:

答案 0 :(得分:3)

每次调用存储库时,我都会执行以下操作,而不是读取和写入文件:

在构造函数中,您将文件读入Configuration个对象的列表中。就像你现在使用Get方法一样。您将此列表保存在类的字段中,并将其用于所有其他方法(添加等)。

您的存储库确实有SaveChanges方法,因此这是将配置序列化回磁盘的理想位置。

这应该比当前方法更高效,更简单,因此也更不容易出错。

修改:这是一个开始:

public class XmlRepository : IRepository<Configuration>
{
    private readonly List<Configuration> configurations;

    public XmlRepository(string filename)
    {
        FileName = filename;

        XmlSerializer serializer = new XmlSerializer(typeof(List<Configuration>), null, new Type[] { typeof(BinaryConfiguration) }, new XmlRootAttribute("Configurations"), "http://ofimbres.wordpress.com/");
        using (StreamReader myWriter = new StreamReader(FileName))
        {
            configurations = (List<Configuration>)serializer.Deserialize(myWriter);
            myWriter.Close();
        }
    }

    internal string FileName { get; private set; }

    public Configuration GetById(object id)
    {
        return (from c in configurations where c.Id == id select c).Single();
    }

    public IEnumerable<Configuration> All()
    {
        return configurations;
    }

    public void Insert(Configuration entity)
    {
        configurations.Add(entity);
    }

    public void Remove(Configuration entity)
    {
        configurations.Remove(entity);
    }

    public void SaveChanges()
    {
        XmlSerializer serializer = new XmlSerializer(configurations.GetType(), null, new Type[] { typeof(BinaryConfiguration) }, new XmlRootAttribute("Configurations"), "http://ofimbres.wordpress.com/");
        using (StreamWriter myWriter = new StreamWriter(FileName))
        {
            serializer.Serialize(myWriter, configurations);
            myWriter.Close();
        }
    }
}

也是一些一般性建议

  • 使用using处理文件/流和其他需要处置的资源(本例中为StreamReaderStreamWriter)。这可以保证即使出现异常也会关闭文件。
  • 不要捕获并重新抛出异常,或者如果你这样做,至少使用throw而不是throw ex来保留完整的堆栈跟踪。

希望这有帮助!