MakoLab / RomanticWeb

RDF-Object Mapping for the Semantic Web
http://romanticweb.net/
Other
12 stars 9 forks source link

Shorthand syntax for creating entites #39

Open tpluscode opened 10 years ago

tpluscode commented 10 years ago

It would be very handy to use anonymous objects to create entities, especially ones with a blank id. Currently a blank node id must be explicitely created

var entity = context.Load<IPerson>("some URI");
var blankId = entity.CreateBlankId();
var address = context.Create<IAddress>(blankId);
address.City = "New York";
entity.Address = address;

Here's how this could work instead

var entity = context.Load<IPerson>("some URI");
entity.Address = new {
   City = "New York"
};

Internally a new blank identitifier would be generated for the anonymous object and each property would be translated to the destination type. In this case the properties would have to match those of IAddress.

tpluscode commented 10 years ago

Additionally it could be possible to declare an Id property. If present it would be used instead of the generated blank node identified.

var entity = context.Load<IPerson>("some URI");
entity.Mother = new {
   Id = new Uri("the mother's id"),
   Name = "Theresa"
};

The question is how should the Name property be treated? After all it is possible that an entity already exists for the given Uri. Should the supplied values replace whatever values the already existed.

tpluscode commented 10 years ago

Lastly the IEntityContext could be extended to enable creating new entities in a concise way.

dynamic myself = new {
   Id = "http://example.org/about#me",
   Name = "Tomasz",
   LastName = "Pluskiewicz"
};
context.Add<IPerson>(myself);

Similarly to my previous comment it remains to be decided what should happen if the entity already exists.