I am having trouble in understanding what is the difference between the service context and the object context.
I mean, where are they used? What is the basic difference?
ObjectContext is a class that is
generated inside the generated
DomainService class that you made.
while
Object Services is a component of the Entity Framework that enables you to query, insert, update, and delete data, expressed as strongly typed CLR objects that are instances of entity types. Object Services supports both Language-Integrated Query (LINQ) and Entity SQL queries against types that are defined in an Entity Data Model (EDM). Object Services materializes returned data as objects, and propagates object changes back to the data source. It also provides facilities for tracking changes, binding objects to controls, and handling concurrency. Object Services is implemented by classes in the System.Data.Objects and System.Data.Objects.DataClasses namespaces.
Related
What is the difference between a Container and Context in Entity Framework and how they are related? I cannot find the explanation. Someone talks like they are the same. Someone talks about first or second, but never in compare. Thanks.
Check the below link.
Container:
An entity container is a logical grouping of entity sets, association sets, and function imports.
Context:
The Entity Framework enables you to query, insert, update, and delete data, using common language runtime (CLR) objects (known as entities). The Entity Framework maps the entities and relationships that are defined in your model to a database. The Entity Framework provides facilities to do the following: materialize data returned from the database as entity objects; track changes that were made to the objects; handle concurrency; propagate object changes back to the database; and bind objects to controls.
Container VS Context
Here is my situation. I use Entity Framework 4 with the Web API
The structure of my code is quite simple, I have the Service layer where all my rest API is organized, I have my Business logic layer where I have business controllers to manage Transactions between the rest calls and the data layer. Finally, I have a data layer with generic repositories and a DAO to access the whole thing.
In my Business controllers, I use using to inject a non transactionnal (read only methods) OR a transactional (CRUD methods) DbContext.
When returning values to my REST API, I parse it into JSON.
The problem is that I keep having this exception: Newtonsoft.Json.JsonSerializationException
I return my entities / collections / lists outside of my using{} statement, which I think EF does not like by default.
In debug mode, sometimes, I will manage to retrieve all data, but not all the time. Since my entities come from a query within a DbContext, I think that the behavior is to remove loaded sub-properties after the context has been disposed of.
Fact is, I want to keep my structure as is, and I was wondering the following:
Is there a way of returning complete (not lazy-loaded) entities after leaving the using{} statement?
Thanks a lot
I actually read more about Entity Frameworks behavior. What I get is actually standard for EF. I have to force the context to Load() my refered entities in order to get them after leaving the context.
I would like to be able to inject some dependencies (by using an IoC container) into entities just after they are loaded and materialized by Entity Framework (as a result of a query for instance).
It is possible to do so by hooking on the ObjectMaterialized event but I'm wondering if there is no better manner to achieve this as I use EF 6 and code first.
Any advices or ideas ?
Thanks
Riana
Although Entity Framework can be configured to allow dependencies to be injected into entities, I think it's safe to say that the general consensus (take a look at the opinions of Jimmy Bogard, Mark Seemann and me) is to not do this at all.
For me the main point is that classes like entities, DTOs and messages are very different from service classes. Entities, DTOs and messages are short lived objects containing runtime data, while services contain behavior, are often long lives and simply process runtime data (such as entities).
That doesn't mean that you can't use services into your entities though. As Mark describes here, not letting your entities use services lead to an Anemic Domain Model. But what this means is that entities shouldn't be part of your object graph.
Instead, if you are practicing DDD, your entities can simply accept dependencies into the domain methods that you define on the entities. Those dependencies can than be supplied by the command handlers that execute the use case. In other words, dependencies are injected into the constructor of a command handler, and when calling an entity's domain method, the command handler will supply the dependencies that this method requires (usually just one or two) to that method (method injection).
So far i understand that i will have no more need to define an #version field in my entitites and no more need to use an entity locator. And for value proxies i will have to usenormal editors. Any other diffrences, advantages, disadvantages? What about in the context of using request factory in conjunction with spring
The main difference is that with EntityProxy, the client can send a diff of changes rather than the entire object graph. This is made possible because EntityProxys have an identity, so the server can fetch the identity from the datastore and then apply the diff/patch sent from the client, and only then the entity will be passed to your service methods.
With ValueProxy you basically have an equivalent of GWT-RPC: the object is reconstructed from scratch on the server, and not associated with your datastore (in the case of JPA for instance, it's not attached to the session). Depending on your datastore API, this can make things more complex to handle in your service methods.
Other than that, you'll also lose the EntityProxyChange events.
I have a class library with objects defined that I will use in an application. In a seperate class library I have my Entity Framework object model, where I add a function import, select my stored procedure, and generate a complex type. Rather than generate a complex type, is there any way to point it to the object I defined in my other class library? Essentially, I want my objects all in a seperate library than my data access layer. Do I need to write my own mapping logic?
Note, this is my first EF attempts. I guess you could say I am using a code first approach. I am building the objects (POCO) I want to use and they are in their own class library (My object model). In a seperate class library I am using EF and exposing methods that accept or return classes in my object model, that can be called by my appliation (data access layer).
In my database I have a set of stored procedures. Stored procedures are the only way my data access layer can interact with the database.
In my data access layer, I have created an edmx file, and add function imports to the edmx file. The edmx entity container access is set to internal, so it isn't exposed outside of the class library. Instead, I have a public class with public methods that can be called by my application. Those methods make calles to the object context class to fire the storedproceds, and return objects from my object model. The purpose here is that I am only using the entity framework to handle stored procedure calls. In essence the data access technology is completely hidden from the application. The application only knows how to call specific functions exposed from my data access library, and deal with objects exposed in my object model. Obviously I could write code that translates complex types defined in the edmx to types in my object model, but I'm just wondering if there is any way to avoid writing that code myself, and just have EF use my object model instead of its complex types...