Lets say we are using DTO objects to transfer data between service layer and Presentation (MVC) layer.In this case the presentation layer can only access DTO objects. Therefore we can't use lazy loading functionality in Entity framework.
Am I right here? Please give your suggestions.
(My DTO are not the entities in EF and I have implemented repository and unit of work pattern)
You can use lazy loading but only on your service side when you are working with attached entities.
First put your definition right: Are your DTO objects also your entities in EF 4.1? Are they (also) your models and do they contain business logic?
If so, i would recommend turning off proxy creation ( myDbContext.Configuration.ProxyCreationEnabled = false; ) since they cant be serialized easily. Then use a repository for dataAccess where in the CRUD methods, you specify the right entity states like: http://blogs.msdn.com/b/adonet/archive/2011/01/29/using-dbcontext-in-ef-feature-ctp5-part-4-add-attach-and-entity-states.aspx
Related
Is it possible in Entity Framework using Code First to create a domain model of pure POCOs that are totally ignorant of the Entity Framework?, i.e. don't decorate any classes or properties with any attributes and annotations related to the EF, and don't use virtual keyword to be able to support lazy loading.
Can I achieve this? or do I have to make two models one for persistence model and one for domain model to achieve this.
In a code-first implementation of EF, used along with a Repository pattern, should the repository return business objects from the domain model, or simple entities from the data model?
From what I can tell, the point of Repository is to return business objects, not entities, so that you can do work with them. But most of the code examples I find are returning data models instead, which seems like a bad idea to me because what if the data source changes?
If you are using CodeFirst development you can easily use your business object as the data model as well. You can write Ef mappings in a separate DLL to remove the dependency of EF to business model. If you want to change the data source to another one instead of EF you can keep the same Business(domain) classes for that as well.
We are in a process of designing an application with approx 100 tables and complicated business logic. Windows Forms will be used on the client side and WCF services with MSSQL on the server.
Custom DTOs are used for client-server communication, business entities are not distributed.
Which variant of Entity Framework to use (and why):
EF 4.0 EntityObjects
EF 4.0 POCO
EF 4.1 DbContext
Something else
Database-first approach is a requirement.
Also, is it worth implementing a Repository pattern? It seems a bit redundant, as there is one level of abstraction in the mapping itself and another one in the use of DTOs. I'm currently leaned towards using auto-generated extendable repositories for each entity returning IQueryable, just to have a place to put common queries, but still allowing querying entity model directly from the Service Layer.
Which variant to use? Basically once you have custom DTO the only question is do you want to have control over entities code (their base class) and make them independent on EF? Do you want to use code first? If the answers to all questions are no then you can use EntityObjects. If you want to have entities persistence ignorant or use custom base class you should go to POCO. If you want to use code first or new DbContext API you will need EF 4.1. Some related topics:
EF 4.1 Code-first vs Model/Database-first
EF POCO code only VS EF POCO with Entity Data Model (this was related to CTP)
ADO.NET DbContext Generator vs. ADO.NET POCO Entity Generator
EF Model First or Code First Approach?
There are more things to consider when designing service layer. You should be aware of complications you will have to deal with when using EF in WCF. Your service will provide data to WinForms application and it will work with them in "detached mode". Once user will do all changes he wants to do he will post data back to the service. But here comes the problem - you must tell EF what has changed. If you for example allow user to change order with all its order items (change quantity in items, add new items, delete some items) you must say EF exactly what has changed, what was added and what was deleted. That is easy when you work with single entity but once you allow user to change object graph (especially many-to-many relations) then it is quite tough. The most common solution is loading the whole graph and merge the state from incoming DTOs to loaded and attached graph. Other solution is using Self tracking entities instead of EntityObjects/POCOs + DTOs.
When discussing repositories I would refer you to this answer which refers many other answers discussing repositories, their possible redundancy and possible mistakes when using them just to make your code testable. Generally each layer should be added only if there is real need for the layer - due to better separation of concerns.
The main advantage of POCOs is that those classes can be your DTOs, so if you've already got custom DTOs that you're using, POCO seems a bit redundant. However, there are some other advantages which may or may not have value to you, since you didn't mention unit testing as a requirement. If you plan to write unit tests, then POCO is still the way to go. You probably won't notice much difference between 4.0 POCO and 4.1 since you won't be using the code-first feature (disclaimer: I've only used 4.0 POCO, so I'm not intimately familiar with any minor differences between the two, but they seem to be more or less the same--basically I was already using POCO in 4.0 and haven't seen anything that's made me want to update everything to use 4.1).
Also, depending on whether you plan to unit-test this layer, there's still value in implementing the repository/unit of work patterns when using Entity Framework. It serves to abstract away the data access logic (the context), not the entities themselves, and allows you to do things like mocking your context in unit tests. What I do is copy the T4 template for my context and use it to create the interface, then edit the T4 template for the context and have it implement that interface and use IObjectSet<T> instead of ObjectSet<T>. So instead of:
public class MyEntitiesContext
{
public ObjectSet<MyClass> MyEntities
...
}
I end up with:
public interface IMyEntitiesContext
{
public IObjectSet<MyClass> MyEntities;
}
and
public class MyEntitiesContext : IMyEntitiesContext
{
public IObjectSet<MyClass> MyEntities
...
}
So I guess it really comes down to whether or not you plan to write unit tests for this layer. If you won't be doing anything that would require mocking out your context for testing, then the easiest thing to use would probably be 4.0 EntityObjects, since you aren't planning to pass your entities between layers and it would require the least effort to implement. If you plan to use mocking, then you'll probably want to use POCO and implement repository/unit of work.
I am new to Entity Framework 4, and I am wondering, what's the best way to organize my data layer--the code that accesses EF4?
At this point, my data layer is set up like this:
DataStore class: Holds a reference to the EF4 ObjectContext, and contains methods to open, close, and persist the ObjectContext to storage.
Repository classes: One class for each entity, with methods to create, fetch, and delete entity objects.
Is there a better way to organize this functionality for EF4? I've looked for articles/blogs on the subject, but I'm not finding much. Any suggestions?
I use a generic repository for Entity Framework that makes access very easy. No need to write a separate repository for each entity, just:
MyDataContext ctx = new MyDataContext();
Repository<MyEntity, MyDataContext > myEntityRep = new Repository<MyEntity, MyDataContext>(ctx);
myEntityRep.Add(new MyEntity() {//property settings});
This repository totally abstracts the Entity model, allowing for creating, modifying, and deleting entities.
I prefer the repository classes for each entity type (I know them as DataMapper). One class for all queries easily becomes too monolithic hard to maintain.
I'm developing a project using a layered architecture. I have a DAL in which i'm using Entity Framework, a business logic layer which consumes the objects returned by the DAL and an app layer.
I'm not entirely sure i'm thinking this right, so i'll just ask you what you think.
My DAL is based on mappers. I have types - mappers - that the BLL uses to operate on my data. These mappers return DTO's, because i did not want to expose to my BLL any EF objects so their implementations are not dependent on EF to work.
All these mappers do are CRUD actions on a single 'table', like:
using (var contex = new EFEntities()){
var obj = (from x in context.Table where x.ID == param select x).SingleOrDefault;
return Map(x.ToList());
}
The Map method maps the EF object to a DTO, which has only some properties to map the values i want to expose.
Is there a more elegant approach to this? I am just using EF to facilitate the access to my database - i don't have to write any ADO.NET code.
Any input on this would be welcome.
Thanks.
I have a DAL in which i'm using
Entity Framework, a business logic
layer which consumes the objects
returned by the DAL and an app layer.
That is a perversion, given that you are putting a complete object layer into your DAL. EF is a lot more than a DAL.
a business logic layer which consumes
the objects returned by the DAL and an
app layer.
The term you may want to look up for that is "anemic object model", and it is not a nice term.
Is there a more elegant approach to
this?
Don't fight EF. if you dont like it, don't use it - there are a LOT better frameworks around.