vendredi 11 décembre 2015

Is there a design pattern for a repository that always returns a single object?

I'm developing an application which translates objects from another API into my own types and back. I've exposed this functionality through a number of repository classes which are passed a Document object from the API I'm working with. All the repository classes are based off the following interface.

public interface IRepository<T> where T : INamed
{
    void Add(T toAdd);
    T Get(string name);
    IDictionary<string, T> GetAll();
    ICollection<string> GetNames();
    void Update(T toUpdate);
    void Delete(string name);
}

Where INamed just forces entities to have a Name property, as each is identified by a unique name in a Document.

public interface INamed
{
    string Name { get; set; }
}

However, a Document also contains a number of settings/attribute objects for which there will be only one instance per document. These types thus do not have a name in the document to identify them. Yet, I still want to create some kind of generic interface for getting/setting these objects from/to a Document.

The simple interface I want to use is below. But I don't like that I used the word "Repository" in the name, as that implies a collection of items in my mind.

public interface ISingleItemRepository<T>
{
    T Get();
    void Set(T item);
}

Does this look like some other common design pattern with a different name?

Aucun commentaire:

Enregistrer un commentaire