Using SimpleDB (with SimpleSavant) with POCO / existing objects, not attributes on my classes

I am trying to use Simple Savant in my application to use SimpleDB

I currently have (for example)

public class Person
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public DateTime DateOfBirth { get; set; }
}

To use this with Simple Savant, I would have to add attributes above the class declaration, and the property is [DomainName ("Person")] over the class and [ItemName] over the Id property.

I have all my entities in a separate assembly. I also have data access classes - a separate assembly, and the factory class selects based on the IRepository configuration (in this case, IRepository

I want to be able to use my existing simple class - without property attributes, etc. In case I move from simple db to something else - then I only need to create another implementation of IRepository.

Should I create a class like "DTO" to map the two together?

Is there a better way?

+3
source share
1 answer

You should check Savant's documentation for Typical Operations . Typical operations allow you to interact with Savant using dynamically constructed mappings rather than data / model objects. For example, you can create a dynamic mapping for your Person class as follows:

ItemMapping personMapping = ItemMapping.Create("Person", AttributeMapping.Create("Id", typeof (Guid)));
personMapping.AttributeMappings.Add(AttributeMapping.Create("Name", typeof (string)));
personMapping.AttributeMappings.Add(AttributeMapping.Create("Description", typeof(string)));
personMapping.AttributeMappings.Add(AttributeMapping.Create("DateOfBirth", typeof(DateTime)));

, ItemMappings - , Savant . .

Person :

Guid personId = Guid.NewGuid();
PropertyValues values = savant.GetAttributes(personMapping, personId);
Person p = PropertyValues.CreateItem(personMapping, typeof(Person), values);
+2

Source: https://habr.com/ru/post/1734320/


All Articles