Page 19 - MSDN Magazine, September 2017
P. 19
multicolor
hit-
highlighting
easy
options
®
This forces the properties of PersonName to resolve as properties of Samurai. While your code will use the Samurai.SecretIdentity type and navigate through that to the First and Last properties, those two properties will resolve as columns in the Samurais data- base table. EF Core convention will name them with the name of the property in Samurai (SecretIdentity) and the name of the owned entity property, as shown in Figure 2.
Now I can identify a Samurai’s secret name and save it with code similar to this:
using (var context = new SamuraiContext()) { var samurai = new Samurai { Name = "HubbieSan" samurai.Identify ("Late", "Todinner"); context.Samurais.Add (samurai); context.SaveChanges ();
}
In the data store, “Late” gets persisted into the SecretIdentity_First field and “Todinner” into the SecretIdentity_Last field.
Then I can simply query for a Samurai:
var samurai=context.Samurais .FirstOrDefaultAsync (s => s.Name == "HubbieSan")
EF Core will assure that the resulting Samurai’s SecretIdentity property is populated and I can then see the identity by requesting:
samurai.SecretIdentity.FullName
EF Core requires that properties that are owned entities are populated. In the sample download, you’ll see how I designed the PersonName type to accommodate that.
Simple Classes for Simple Lessons
What I’ve shown you here are simple classes that leverage some of the core concepts of a DDD implementation in the most minimal way that lets you see how EF Core responds to those constructs. You’ve seen that EF Core 2.0 is able to comprehend one-to-one uni-directional relationships. It can persist data from entities where scalar, navigation and collection properties are fully encapsulated. It also allows you to use value objects in your domain model and is able to persist those, as well.
For this article, I’ve kept the classes simple and lacking in additional logic that more correctly constrains the entities and value objects using DDD patterns. This simplicity is reflected in the download sam- ple, which is also on GitHub at bit.ly/2tDRXwi. There you’ll find both the simple version and an advanced branch where I’ve tightened down this domain model and applied some additional DDD practices to the aggregate root (Samurai), its related entity (Entrance) and the value object (PersonName) so you can see how EF Core 2.0 handles a more realistic expression of a DDD aggregate. In an upcoming column, I’ll discuss the advanced patterns applied in that branch.
Please keep in mind that I’m using a version of EF Core 2.0 shortly before its final release. While most of the behaviors I’ve laid out are sol- id, there’s still the possibility of minor tweaks before 2.0.0 is released.n
Julie lerman is a Microsoft Regional Director, Microsoft MVP, software team mentor and consultant who lives in the hills of Vermont. You can find her presenting on data access and other topics at user groups and conferences around the world. She blogs at thedatafarm.com/blog and is the author of “Programming Entity Framework,” as well as a Code First and a DbContext edition, all from O’Reilly Media. Follow her on Twitter: @julielerman and see her Pluralsight courses at juliel.me/PS-Videos.
Thanks to the following Microsoft technical expert for reviewing this article: Arthur Vickers
msdnmagazine.com
Instantly Search Terabytes of Data
across a desktop, network, Internet or Intranet site with dtSearch enterprise and developer products
Over 25 search features, with
dtSearch’s document filters support popular file types, emails with multilevel attachments, databases, web data
Developers:
• APIs for .NET, Java and C++
• SDKs for Windows, UWP, Linux, Mac and Android
• See dtSearch.com for articles on faceted search, advanced data classification, working with SQL, NoSQL & other DBs, MS Azure, etc.
Visit dtSearch.com for
• hundreds of reviews and case studies • fully-functional evaluations
The Smart Choice for Text Retrieval® since 1991
dtSearch.com 1-800-IT-FINDS