I have some questions regarding the entity framework context instance management.
My code base is composed of many independent components/agents all running in one process that can only do work by reading messages from their corresponding queues at their convenience(whenever the component is ready, it will pick up the next message from the queue so no concurrency issue at the component level)
Each of the components need to interact with the database independent from other components. I am wondering what is the better way to setup the context instance/s for each component. Here are some of the options
1> Have one instance of the the context used by all components. --> I think this is the worst as it creates many concurrency issues?
2> Give every component an independent instance of the context. --> This looks fine but
is it ok to have many context independent instances in one process while all components are running concurrently in this process?
should I create a new instance of context for every new message that the component will process or keep one context instance for the life of the component? I think the last one makes more sense but I am more used to use context in a using{} bracket and I am not sure if keeping one context live for the life of each component has any complications in the way I am using it?
can I rely on optimistic concrruency so that two different independent components won't put same record in the database given all contexts are in one process?
BTW, I am using entity framework 4.1.
Definitely use one context per component / agent - if component is multithreaded use one context per thread. If each message processing is executed as separate "logical transaction" then use one context per message processing.
Why:
Context internally uses two very important design patterns - Identity map and Unit of work. This answer describes the behaviour enforced by these patterns.
Context and anything else in EF is not thread safe.
Optimistic concurrency doesn't mean that different contexts will not put the same record in the database. Optimistic concurrency means that update statements compare current state in the database with last known state by the context (there is a delay between loading record and saving new values and another context can change the record). If record changed you will get an exception and you must handle it somehow.
Related
How can I only load an actor if it has previously been formally created following domain rules? Everything I've seen using actorOf and persistence always creates an empty instance and then applies the [possibly empty] set of events that have occurred, whereas I would prefer to make empty instances unrepresentable. Is there a standard approach for requiring a "created" event and otherwise triggering failure via "not found"?
My thinking is to have one actor instance for each entity in the system, but my expectation is that less than 10% of the entities will be actively needed at any given time so I'm trying to avoid loading the entire set just to check validity when handling a request.
I'm new to Akka and event sourcing in general so if I'm thinking about things from an unhelpful perspective I'm open to any alternatives as well.
I guess you are talking about PersistentActors.
There is no api for pre persisting state outside of the PersistentActor. The idea is that actor keeps track of his state for himself. There is no profit of empty actors, actors should keep some state so when you initialise it via actorOf it should have kind of state Created. On the other hand you should not think it has saved its state until it reports to creator that it was persisted.
Persistent actors are loaded passively into the memory only when you initialise them explicitly, so all the entities won't be loaded at startup. If you want to passivate them you can stop the actors or invoke setReceiveTimeout method on actor's context.
I'm just trying to get my head around how one goes about updating an entity using CQS. Say the UI allows a user to update several properties of a particular entity, and on submit, in the back-end, an update command is created and dispatched.
The part I'm not quite understanding is:
does the cmd handler receiving the message from the dispatcher then retrieve the existing entity from the DB to then map the received stock item properties to then save? Or
is the retrieval of the existing item done prior to the dispatching of the cmd msg, to which it is then attached (the retrieved entity attached to cmd that is then dispatched)?
My understanding is that CQS allows for a more easier transition to CQRS later on (if necessary)? Is that correct?
If that is the case, the problem with 2 above is that queries could be retrieved from a schema looking very different to that from the command/write schema. Am I missing something?
does the cmd handler receiving the message from the dispatcher then retrieve the existing entity from the DB to then map the received stock item properties to then save
Yes.
If you want to understand cqrs, it will help a lot to read up on ddd -- not that they are necessarily coupled, but because a lot of the literature on CQRS assumes that you are familiar with the DDD vocabulary.
But a rough outline of the responsibility of the command handler is
Load the current state of the target of the command
Invoke the command on the target
Persist the changes to the book of record
My understanding is that CQS allows for a more easier transition to CQRS later on (if necessary)?
That's not quite right -- understanding Meyer's distinction between command and queries make the CQRS pattern easier to think about, but I'm not convinced that actually helps in the transition all that much.
If that is the case, the problem with 2 above is that queries could be retrieved from a schema looking very different to that from the command/write schema. Am I missing something?
Maybe - queries typically run off of a schema that is optimized for query; another way of thinking about it is that the queries are returning different representations of the same entities.
Where things can get tricky is when the command representation and the query representation are decoupled -- aka eventual consistency. In a sense, you are always querying state in the past, but dispatching commands to state in the present. So you will need to have some mechanism to deal with commands that incorrectly assume the target is still in some previous state.
I'm using Prism framework with EF in WPF application.
ViewModel:
keeps service references (passed by unity container).
Services:
are providing "high level" operations with data
keeps reference of Repository, which provides basic CRUD operations with database (single table per repository).
Repository:
every method in repository uses the "using" pattern, where I work with short lived object context.
This is where i got stuck: after object context is disposed, I can not work with mapped properties no longer. My database model is complex (many related tables) and many .Include() calls when retrieving data keeps code dirty.
After reading several threads, I discovered that "unit of work" pattern is probably what I need.
Here comes my question:
Who keeps reference of unit of work (and therefore context)?
If I choose context per view approach, viewModel should have context reference.
How can I inject unit of work to my services then? Or should I create new instance of Service in ViewModel and pass context in constructor parameter?
We're using a similar architecture in a project:
Each ViewModel gets its own Service object which gets injected in the constructor (at least the top level ones that directly correspond to a View. Some hierarchical ViewModels may reuse their parent's Service, but let's keep it simple here).
By default, each Service operation creates a new context, but...
Services have BeginContext and EndContext methods which can be called
by the ViewModels to keep the context open over multiple operations.
This worked pretty well for us. Most of the time we call BeginContext when a View is opened and EndContext when it is closed.
In my MVC controllers, I'm using an IoC container (Ninject), but am not sure how to best use when it comes to the Entity Framework ObjectContext.
Currently, I'm doing something like:
using(var context = new MyObjectContext())
{
var stuff = m_repository.GetStuff(context);
}
This is the best way to manage from the point of view of keeping the database connection open for the shortest time possible.
If I were to create the ObjectContext via Ninject on a per request basis, this obviously keeps the database connection open for too long.
Also the above code would become...
var stuff = m_repository.GetStuff(m_myObjectContext);
(And when would I dispose of the context...?)
Should I be creating a factory for the ObjectContext and pass that in via DI? This would loosen the coupling, but does this really help with testability if there is no easy means of maintaining an interface for the ObjectContext (that I know of)?.
Is there a better way? Thanks
This is the best way to manage from the point of view of keeping the
database connection open for the shortest time possible.
If I were to create the ObjectContext via Ninject on a per request
basis, this obviously keeps the database connection open for too long.
Entity Framework will close the connection directly after the execution of each query (except when supplying an open connection from the outside), so your argument for doing things like this does not hold.
In the past I used to have by business logic (my command handlers to be precise) have control over the context (create, commit, and dispose it), but the downside is that you need to pass on this context to all other methods and all dependencies. When the application logic gets more complex, this results in less readable, less maintainable code.
For that reason I moved to a model where the unit of work (your MyObjectContext) is created, committed, and disposed outside the control of the business logic. This allows you to inject the unit of work into all dependencies and reuse the same unit of work for all objects. Downside is that this makes your DI configuration a bit harder. Some things your need to make sure of:
The unit of work must be created as per web request or within a certain scope.
The unit of work must be disposed at the end of the request or scope (although it is probably not a problem when the DbContext is not disposed, since the underlighing connection is closed and DbContext does not implemente a finalizer).
You need to explicitly commit the unit of work, but you can't do this at the end of the web request, since at that point you have no idea whether it is safe to commit (since you don't want to commit when your business logic threw an exception, but at the end of the request there is no way to correctly detect if this actually happened).
One tip I can give you is to model the business logic in the system around command handlers, since this allows you do define a single decorator that handles the transactional behavior (committing the unit of work and perhaps even running everything in a database transaction) at a single point. This decorator can be wrapped around each handler in the system.
I must admit that I have no idea how to register generic types and generic decorators with Ninject, but you'll probably get an answer quickly when asking here at Stackoverflow.
I have an abnormal serialization/de-serialization behavior in my ASP.NET MVC application.
Setup and architecture are as follows:
.Net 3.5, MVC 2, Unity 2.0 for dependency injection, IIS 7. State is stored in ASPNET state process. Application apart from models, views and controllers also contains a number of application specific services and model mappers (for interfacing with existing code). Two of the services have per Session based lifetime, for which I implemented custom PerSessionLifetimeManager (as Unity does not have one out of the box). This lifetime manager uses HttpContext.Current.Session to store objects. There are fair bit of dependencies between controllers, mappers, services and between services as well.
Problem:
One of session lifetime services contains a private boolean field which changes value under some internal logic conditions, however this value changes by outside of my code. After some investigation I found that problem relates to this object being serialized/de-serialized twice every time and values of the field are different during serializations.
Investigation done so far:
I have a break points/logging on object constructor and I wrapped field into a property and put breakpoint/logging on setter. Object is definitely constructed only once and there are no calls to change property value except those which should be called. I have made object to implement ISerializable and put logging of System.Threading.Thread.CurrentThread.ManagedThreadId and object hash (constructed from all fields of the object). From what I can see if value of the property/field changes object gets serialized twice once with new value and immediately after with original value. Next time object is used it de-serializes in LIFO order, so the original (unchanged) value object is pulled out first and then the changed one. I tried to log a full stack trace for these serialization calls, but it seem to be all .Net internal calls.
Questions:
Why is there multiple serializations with different values? How to avoid this?
Current work around:
I used Session instead of private field/property and it works fine, however use of session on heavily loaded websites is not the best thing and I am hoping to for some other solution.
Thank you.
Are you sure the value is becoming corrupted or are you simply loosing the value due to the session being purged. Do you have any other values in the session that disappear at the same time? I was a bit confused by your "State is stored in ASPNET state process. " statement. Is your session management set to InProc or are you using a StateServer? If it is InProc, you definitely will experience loss of session values (especially in shared hosting environments), and should switch to an ASP State Server Service or MSSQL State Server solution.