This post only reflects some updates to my previous article: Putting Entity framework 4 to use in a business architecture.
I made a “minor” error in my EfDynamicProxyAssemblies that is consumed by my EfDataContractSerializer. The error which I know have corrected arised when I added an example to the client application, where I intended to read back the created wishlist(s) for a certain username. What happened was that the service used the overload of the EfEntityStore’s Query method that lets you specify string’s for memebers in the objectgraph to include in the select. The generated proxie than contained three entities: Wishlist, UserAccount and Wish, but my EfDynamicProxyAssemblies cleared the cached assemblies once the types were extracted. And the first time the dynamic proxie assembly was executed, the only contained type was UserAccount, hence WishList and Wish didn’t get extracted and registrered as a known type.
I have corrected the code and added some information about the custom EfDataContractSerializer attribute in the article.
The complete code example can be found here.
I have also added some information about “eager loading”. So there is some new code and two new sub chapters.
Enjoy!
//Daniel
[...] « WCF DataContractSerializer problems with dynamic proxies in Entity framework 4 Updates to – Putting Entity framework 4 to use in a business architecture [...]
Updates to – Putting Entity framework 4 to use in a business architecture « Daniel Wertheim…
Thank you for submitting this cool story – Trackback from DotNetShoutout…
[...] Putting Entity framework 4 to use in a business architecture [...]
Hi Daniel,
Thanks for the update. So far this is the best layered (or tier) architecture i have seen.
One thing I noticed when I choose IoC scope to Thread-Scope I get an exception if I try to access the EF context second time (even I acess in same function call). I get this execption : “The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.”.
It is caused by the following code (from your BasicApplicationService.cs)
using (var entityStore = ServiceEngine.Instance.IoC.Resolve())
{
// get something();
}
if I call again
using (var entityStore = ServiceEngine.Instance.IoC.Resolve())
{
// get or save something();
} // error….
why it happen?
continue the above comment..
but it will work if I remove using.
using (var entityStore = ServiceEngine.Instance.IoC.Resolve())
{
// get something();
}
TO
var entityStore = ServiceEngine.Instance.IoC.Resolve())
entityStore.getsomething();
Hi,
First of all, I think there are a lot of improvements to do to the architecture, especially how I handle the IoC concept. A better implementation for this could be found: http://daniel.wertheim.se/2010/03/14/linq-to-sql-how-to-separate-the-entities-and-the-datacontext/
Also note that there is a new version of the CTP (v3), and the article has not been updated for it.
As for the error you get, I have to look this up. Hopefully during the day. It seems like the Resolve method doesn’t give you a new EntityStore each time, hence the first using-statement will dispose it.
//Daniel
ThreadLocal – A single instance will be created for each requesting thread. Caches the instances with ThreadLocalStorage.
//Daniel
Hi Daniel,
It seems to me that if I use a IoC I should not use using() syntax.
Example if i set IoC scope as Singleton …
using (IEntityStore store = IoC.Invoke())
{
} // here store call parents dispose so it release its resources example connection.
Here dispose is the responsibility of IoC not the client. Since we use using .net call disposes method.
thanks for the project. it’s really wonderful.
[...] a comment » I haven’t got the time to update my previous article to the new CTP-addon for supporting Code-first (old name Code-only). My goal is to start covering [...]
How do you deal with many-to-many relationships in this architecture?
Hi,
It was I while since I sat down with this so I have no answer that comes directly out of my head. I guess it will go faster to google it or check the ADO.Net Team blog. I’m currently starting a project where I will use EF4 code-first so eventually I will have an answer for you.
//Daniel
You need to specifically map the relationships:
Relationship(c => c.Students)
.FromProperty(student => student.Classes)
.Map("dbo.ClassRegistrations", (student, class) => new
{
student.StudentId,
class.ClassId
});
The major drawback I found is that both navigation properties must exist in the Domain model i.e. Student.Classes and Class.Students. If Student.Classes doesn’t make sense in your domain, you still need to expose the property. Hopefully this will be fixed in later releases.
Code first is nice but there are actually quite a lot of constraints placed on the design of classes.
[...] Data of EMDS is stored in Microsoft SQL Server and XML files. For accessing XML files, I use Linq to XML. For accessing database, I use EF4 beta 2 and applying its new feature POCO support. I want to say thank you to I want to say thank you to Daniel Wertheim, the man providing us very helpful framework of EF4 POCO (http://daniel.wertheim.se/2009/12/20/updates-to-putting-entity-framework-4-to-use-in-a-business-arch...). [...]
[...] by Daniel Wertheim Leave a Comment I just started doing some architectural changes to my old code in my previous article for Entity framework 4 (EF4) and at the same time updating it to use the new CTP3 add-on as well as [...]
I am able to map protected properties using the ObjectAccessor class:
Property(ObjectAccessor.CreateExpression("StatusString"));
However, if I wish to specifically Map the table using a similar notation:
MapSingleType(i => new
{
i.ConcurrencyToken,
i.Id,
i.Name
StatusString = ObjectAccessor.CreateExpression("StatusString"),
});
I get a runtime exception. Is it possible to specifically use MapSingleType with protected properties?