Open jeffward01 opened 2 years ago
You can use whatever you want as the primary key. The datasource.GetByKey
method explicitly accepts a string
, int
, long
, or guid
. For other types, you have use GetByKey<TObject, TKey>(TKey key)
.
var customer = dataSource.GetByKey<Customer>(myGuid).Execute();
var order = dataSource.GetByKey<Order, short>(myShort).Execute();
It even supports compound keys, but you'll need to use dataSource.From
for them.
Decompose
The Decompose
feature is rarely used. It was created for someone who wanted to be able to read a single row from the database and have that be translated into multiple objects. For example,
class Customer
{
public Guid CustomerKey {get;set;}
public String CustomerName {get;set;}
[Decompose] public AuditInfo AuditInfo {get;set;}
}
public class AuditInfo {
public DateTime CreatedDate {get;set;}
public DateTime UpdatedDate {get;set;}
}
The two (or more) decomposed objects always from the same row in the result set. They might come from the same table, or they might come from different tables joined in a view or stored procedure.
We don't have a great story for that. But as you saw, there is something in the backlog that might work out.
Basically it allows you to call Chain methods on a EF DBContext. So it shares a DbConnection
and optional DbTransaction
, but nothing else.
This was originally created when someone asked me, "How do I use NHibernate and Chain in the same transaction". Once I figured that out, I added information on doing it with EF as well.
I don't know if it will work with EF Core. It should, with minor tweaks, but I haven't dug into it.
Hello!
Very cool library! This is /u/cs_legend_93 from reddit btw, we talk sometimes in /r/csharp and survive the hive-mind devs.
I was not sure where I should post this, I hope this is ok here. I have (3) quick questions about your library, I have reviewed the documentation and can't seem to find an answer, I want to make sure that I am not missing anything in the documentation.
Question 1
Relationships and Include statements
Question: Is it possible to query and return multiple Tables mapped to multiple entities in a single 'call'?
EF Equivalent I have reviewed much of your documentation, and cookbook and I am unable to find if Chain supports the equivalent of
someEntity.Include(_ => _.SomeChild).ThenInclude(_ => _.SomeGrandChild);
My background is mostly in Entity Framework, we are building small microservices and do not need the heavy-weight that EF has, however its nice to get all related children in a single call.
I am used to configuring relations like this: https://docs.microsoft.com/en-us/ef/core/modeling/, when I looked at Chain, I didnt see any mapping
Chain I see the 'Include Foreign key entities' in your cookbook here
I have to run a demo to fully understand how it works, and 'knows' to include the child entities. It looks like it will call the child entities each and every time.
I also see this: https://github.com/TortugaResearch/Chain/issues/396 it looks like this feature is in the backlog for now.
I also see the
[Decompose]
method here, to be honest I have read the documentation and I am not quite sure how it would work. I am a bit unfamiliar with materializers and projections, I am sure once I play around with these features more, it will make more sense.I also have seen this here, where you talk about 'Multi-Mapping'. This sounds like its mapping a DB Table to one or more objects.
I am referring to mapping multiple DB Tables to a class hierarchy of entities, such as
Book Author --> AuthoredBooks --> Reviews & Comments --> CommentUpvoteCount
My guess is that for Tortuga chain the suggested solution will be to build some small 'repo methods' that called the DB a 2-3 times to fully build the entity such as:
Book Author --> AuthoredBooks --> ReviewCount & CommentCount
Or to structure the DB table with 'roll-ups' to minimize deep SQL calls.
What do you suggest is the recommended solution when you need to retrieve child entities of an object? In the EF world, you tend to try and limit the number of trips to the DB.
Question 2
Using GUID as a PK or FK
Question: Can I use a
GUID
as a PK or FK in Chain?I see that you can use
int
andobjects
as a FK, but I was not sure aboutguid
. We like to useguids
to prevent any mishaps on entities with many relationships (calling the wrong child entity for example).Question 3
EF Integration
Question: What is the extent of the EF Integration?
I see this here: Entity Framework Integration
I just have a quick question on 'what is integrated'.
Are configured relationships also integrated where I can 'call' child objects which have been mapped in EF?
Thanks for the great documentation!! Its a wealth of examples! Especially the cookbooks, very cool! Its nice to have much documentation and not resort to reading the
.cs
files and diving down that rabbit hole.Thanks again for the great library, I hope to replace use
Chain
as aEntityFramework
replacement within our microservice infrastructure.