2

我很难尝试创建 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;
        } 
    }
}

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

4

1 回答 1

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保留完整的堆栈跟踪。

希望这可以帮助!

于 2012-04-05T17:48:45.247 回答