具有处理序列化能力的集合存储库实现

发布于 2024-11-14 21:51:57 字数 643 浏览 3 评论 0原文

我有几个集合,它们看起来都一样,但对不同类型的集合项进行操作,如下所示:

[CollectionDataContract(ItemName = "Culture")]
public sealed class Cultures : List<LangCult>
{
}

这里重要的代码是 CollectionDataContract 属性,它允许我们将此集合序列化为 XML 文件DataContractSerializer。整个集合被序列化为一个 XML 文件,该文件以集合类类型的名称命名,例如 Cultures.xml

我想提出一个存储库,精确地带有存储库的接口,这适用于所有可能的集合,例如 public class Slugs : List

我尝试过这样的事情,但我不知道这是否是最好的选择:

public interface ICollectionsRepository<T> where T : IEnumerable

你的想法是什么?

请不要回复有关如何序列化的说明,因为这不是这里的问题,而且我已经可以正常工作了。

I have several collections that all look the same but operate on different types of collection items, like this:

[CollectionDataContract(ItemName = "Culture")]
public sealed class Cultures : List<LangCult>
{
}

The important piece of code here is the CollectionDataContract attribute which allows us to serialize this collection into an XML file with DataContractSerializer. The entire collection is serialized into one XML file named as the name of the type of collection class, for instance Cultures.xml

I would like to come up with a repository, precisely with an interface for a repository, that would work on all of the possible collections, for instance public class Slugs : List<string>.

I tried something like this, but I don't know whether it's the best option:

public interface ICollectionsRepository<T> where T : IEnumerable

Your thoughts?

Please do not respond with instructions on how to serialize because that is not the issue here and I have it working.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏至、离别 2024-11-21 21:51:57

也许我应该说我想要一个带有通用项目的通用集合的接口,其中项目具有公共基类。这就是我解决这个问题的方法,希望有一天有人会发现它有用。

/// <summary>
/// This repo enables us to work with serialisable collections. Collection class has
/// to inherit from IEnumerable and must be described with CollectionDataContract attribute
/// </summary>
/// <typeparam name="T"></typeparam>
public interface ICollectionsRepository<T, V> where T : Collection<V>
{
    /// <summary>
    ///     Get collection from datastore
    /// </summary>
    /// <returns>List of items</returns>
    T Load();

    /// <summary>
    ///     Add new collection item to datastore
    /// </summary>
    /// <param name="item">Item to be added to the collection</param>
    void Add(V item);
}

public class XmlCollectionsProvider<T, V> : ICollectionsRepository<T, V> where T: Collection<V>, new() where V: CollectionItem
{
    private readonly string _file = Path.Combine(XmlProvider.DataStorePhysicalPath, typeof(T).Name + ".xml");

    public T Load()
    {
        if (!DefaultsExist()) {
            CreateDefaults();
        }

        DataContractSerializer dcs = new DataContractSerializer(typeof(T));
        T obj = null;
        XmlDictionaryReader reader =
            XmlDictionaryReader.CreateTextReader(new FileStream(_file, FileMode.Open, FileAccess.Read),
                                                                                     new XmlDictionaryReaderQuotas());

        obj = (T)dcs.ReadObject(reader, true);
        reader.Close();
        return obj;
    }

    public void Add(V item)
    {
        T collection = Load();
        collection.Add(item);
        Save(collection);
    }
}

[CollectionDataContract(ItemName = "Culture")]
public sealed class Cultures : List<LangCult>   {   }

[DataContract]
public class LangCult : CollectionItem
{
    ...
}

[DataContract]
public abstract class CollectionItem
{
    [DataMember]
    public string Id
    {
        get;
        set;
    }
}

Maybe I should've said I wanted an interface for generic collection with generic items where items have a common base class. This is how I solved it and hopefully someone one day finds it useful.

/// <summary>
/// This repo enables us to work with serialisable collections. Collection class has
/// to inherit from IEnumerable and must be described with CollectionDataContract attribute
/// </summary>
/// <typeparam name="T"></typeparam>
public interface ICollectionsRepository<T, V> where T : Collection<V>
{
    /// <summary>
    ///     Get collection from datastore
    /// </summary>
    /// <returns>List of items</returns>
    T Load();

    /// <summary>
    ///     Add new collection item to datastore
    /// </summary>
    /// <param name="item">Item to be added to the collection</param>
    void Add(V item);
}

public class XmlCollectionsProvider<T, V> : ICollectionsRepository<T, V> where T: Collection<V>, new() where V: CollectionItem
{
    private readonly string _file = Path.Combine(XmlProvider.DataStorePhysicalPath, typeof(T).Name + ".xml");

    public T Load()
    {
        if (!DefaultsExist()) {
            CreateDefaults();
        }

        DataContractSerializer dcs = new DataContractSerializer(typeof(T));
        T obj = null;
        XmlDictionaryReader reader =
            XmlDictionaryReader.CreateTextReader(new FileStream(_file, FileMode.Open, FileAccess.Read),
                                                                                     new XmlDictionaryReaderQuotas());

        obj = (T)dcs.ReadObject(reader, true);
        reader.Close();
        return obj;
    }

    public void Add(V item)
    {
        T collection = Load();
        collection.Add(item);
        Save(collection);
    }
}

[CollectionDataContract(ItemName = "Culture")]
public sealed class Cultures : List<LangCult>   {   }

[DataContract]
public class LangCult : CollectionItem
{
    ...
}

[DataContract]
public abstract class CollectionItem
{
    [DataMember]
    public string Id
    {
        get;
        set;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文