EAV within entity framework 4 and ddd - entity-framework-4

Some tables in my database is designed using EAV concept.
Then I use entities which are auto generated and represent "static" tables (not "EAV" tables) by ORM Entity Framework as DDD objects.
How can I use my "EAV" entities in object model (not in relational like in database) using Entity Framework?
For example,
in the database I have static table Report and EAV tables which help me store ReportProperty for Report.
In domain model I want have Report like that:
Report
{
ICollection<ReportProperty> ReportProperties{get;set;}
}
I can use Report entity which is generated by Entity Framework and in partial section
realise some logic in getter for retrieving data from my EAV tables to fill Collection ReportProperies. Then it begs the next question.
What can I do if I decide use NHibernate instead Entity Framework, because i can`t use my partial section which i already realize using Entity Framework?
If I will be use DDD objects, which i can use for Entity Framework or NHibernate, it will be hardly for me, because I will need call mapping procedures in each procedures in my DAO.

EAV is a concept of the data access layer, while DDD is a concept of the business logic layer. ORM like Entity Framework or NHibernate tempt us to mix this both layers, but in complex projects with complex logic (that is where DDD is needed) this should never happen. So divide your Dal and Bll. Use hand crafted classes for you DDD objects and use auto generated (or code first) classes for Entity Framework and provide a mapping layer between them. Then EAV will be just an implementation detail of your Dal. Also your Bll and DDD classes wil not have to change, if you switch to NHibernate. Just your mapping Layer would. And by the way, use Dependency Inversion. Make your Dal depend on you Bll and not the other way around. If you make the mapping layer physically separate from your Entity Framework parts, then use mediator pattern on assembly level (meaning your mapping layer depends on your Bll and your Dal and not any other way around).

Related

Using ViewModel with LLBLGen

I am using MVC 3 and LLBLGen. I am confused about approach on populating ViewModels that have data coming from multiple tables. How would i display the Foreign Key names instead of just ID? I don't have any virtual keywords like entity framework. Should i just resort to using SQL Views and then populate viewmodels through them? Just to elaborate more, let's say i want to display more than 100 results at a time and each result has multiple associated foreign keys then how would i display their relevant names rather just IDs?
IMHO, your choices are:
Fields Mapped onto related fields. This has the advantage that you are still using normal Entities.
TypedLists. This is like views, but the building blocks are entity relations and entity fields.
Use TypedViews, that are actually DB Views.
Use your own types, like DTO's or JSON projections. After all you are doing a kind of mapping between your business facade objects and the objects you use to show in your UI.

Inherit from SPManagedObject

In Simperiums iOS/OSX tutorial you say, each modeled object should inherit from SPManagedObject.
I didn't try it yet, but doesn't that lead to one big table in the SQLite database that contains a union of all fields of all modeled managed objects?
Yes, under the hood Core Data will tend to create a bigger table. Generally performance will suffer more from relations though, not inheritance:
Using Parent Entity in CoreData Models
We've done integrations with fairly complex inheritance hierarchies and didn't see any immediate issues with a fair amount of data.
Having said that, should you need more control over your table structure, you can avoid having a single parent for all your objects and instead either:
Manually add the ghostData and simperiumKey attributes to the objects you want to sync, and ensure their class is SPManagedObject (or ensure their custom class inherits from SPManagedObject), or
Create more than one parent entity with ghostData and simperiumKey attributes, and inherit from those for the parts of your model where it makes sense, depending on how you'd like the underlying tables to be structured.

When building a core data app, should I design the data structure as a SQL DB or an OO design?

I've read the manual and saw some tutorial, but still, I do not understand this point.
If I use Core Data in my app, lets say I have an object that one of its properties (column in DB) is a type field. In SQL i would do another table with types and do a many-to-one connection between them.
Now, In the tutorials they say that core data is not SQLite and i should regard it as a representation of an OOP objects.
So, should I do it the same why I would in SQL? types table and a field in the object table that is a foreign key for the types table or should I create a parent entity and inherit from it, creating a new entity (class) for each of the types I need?
SQL DB, as we all know, is a representation of a OOP schema, so I don't understand the benefit of doing it the hard way with inheritance. It is much nicer in my eyes to do it with a types entity, if possible...
Am i wrong about this?
Thank you,
Erez
In my opinion, these are the hard things. You'll always want to return to database designing. It can be better to use a separate 'table', but you can also use subclasses. Maybe you have an Item class, that can have different types. Then you can maybe create the subclasses CarItem, HouseItem... Each representing a certain type of the Item class.

How to Convert Existing MySQL Schema to Core Data

I have a MySQL database and would like to have a similar structure in Core Data. I am very new with using Core Data with Xcode. I have a few fundamental questions if I am doing the right thing.
My Mysql DB looks similar to this:
table.caveconditions
visibilityID
percolationID
xxxx
table.visibility
visibilityID
visibilityValue
...and so on. I would then connect the tables using JOINS
Now, I have done the Core Data modeling like this but I am not quite sure if this is the right approach.
Would be great if someone of you could tell me if this is the right way to do it. In the end I would like to use JSON strings to dump the mysql table into core data.
Thanks a lot
Chris
I have created the new schema. Is this right?
It looks good except for all the "xxxID" attributes e.g. caveID. You also need to follow the naming conventions.
You have the same attribute names with (presumably) the same values in two or more entities. This is necessary in SQL for joins but in Core Data, this is handled by objects and relationships.
Each object in Core Data is automatically universally unique. This means when you create a relationship from one object to another, that relationship concrete identifies on specific unique object.
This means you only need an attribute like caveID in the actual entity that caveID designates which in this case is (presumably) the Caves entity. You don't need the attribute in the CavesConditions entity or any other entity that has a relationship to the "Caves" entity.
(If the xxxID were just artifacts of SQL, you don't actually need them at in Core Data unless some external database your app interacts with requires them.)
A good rule of thumb to use is that any particular value should show up on only one side of a relationship and, ideally, only once in the entire data model.
Naming conventions are a little different than SQL. A Core Data entity isn't a table. An entity is more akin to a class. Each entity is supposed to describe a single instance of a managed object. How many of those instances end up in the object graph is irrelevant. Therefore, entity names are singular.
In this case, Caves should be Cave, Countries should be Country and so on.
Relationships are named after the entity they target. It is not immediate obvious but each reciprocal relationship (the default) on the visual data model editor is actually two relationships because there is one relationship description for each side. Each side has the name of the entity targeted. By convention to-one relationships have a singular name and a to-many relationship has a plural name.
So:
Caves.relConditions<-->>CaveConditons.getCave
...would become
Cave.conditons<-->>CaveConditon.cave
The naming conventions are important because Objective-C uses conventions names to generate and search for accessor methods.
CoreData is NOT a database. Remodel your data as simply as you can and in a way that suits how it will be used in your application and do not think about joins or structure based optimization. You do not have control over the backing schema of a CoreData object model. This is the hardest concept you must get over when starting to use CoreData, but once you do, you will be better off.

How to map class hierarchy (base class and inherited classes) to a database

I want to create a hierarchical object model in ASP.NET MVC, but I'm not sure what would be the best way to design database for this. I have a Product base class with certain properties like Title, Price, OnHandQty etc. I have several inherited classes like Book, which has extra properties like ISBN number, Author etc. Many of my products will fall under generic (base) Product class, but some products will fall under these derived classes (e.g. Book). I am not sure what is the best methodology to map this to database. Should I create separate tables for each product type (including one for generic product)? Or is there any better way?
Please note that I'm not really asking about OR mapping. I know how to create classes from DB tables using Entity Framework. But in this case I am confused about the database design itself.
If you are going to use Entity Framework then you should check out Inheritance with EF Code First by mortezam. He explains three strategies that can be used for representing an inheritance hierarchy:
Table per Hierarchy (TPH): Enable
polymorphism by denormalizing the
SQL schema, and utilize a type
discriminator column that holds type
information.
Table per Type (TPT): Represent "is
a" (inheritance) relationships as
"has a" (foreign key) relationships.
Table per Concrete class (TPC):
Discard polymorphism and inheritance
relationships completely from the
SQL schema.
The idea (with Code First) is that you define your classes and inheritance and let the framework create the database for you. That way you don't need to worry so much about the database design.
You might also want to think about using an Object Database or one of the NoSQL storage strategies like Mongo DB which work better than relational databases when you have these kind of 'jagged' classes.

Resources