Using Entity Framework objects as business objects?

I use Microsoft's MapManager Entity Framework O / R and use entity classes (generated classes that map to database objects) as business objects. This is normal? Please indicate your cons or pros. What to do in case of WCF interaction between the business layer and the presentation, how to send these objects as data elements?

+53
architecture entity-framework
Oct 20 '08 at 6:34
source share
7 answers

I use EF in this way, and one nice feature is that the generated objects are partial classes, which allows them to expand in such a way that they are protected from regeneration problems.

Also check out this link on MSDN , which describes some common usage scenarios with EF regarding business logic.

+20
Oct. 20 '08 at 12:48
source share
— -

Firstly, with views of 11k questions at the time of writing this article, I am a little surprised at the lack of answers and with all due respect, the quality of the answers, given the rather simple question.

So, now that I have strayed a little, I would like to address this issue (s) because I think that today it also applies to the recent Entity Framework Code-First.

"Using Entity Framework Entities as Business Objects?"

A few clarification points before I started:

  • When you say “business objects”, I get the impression that the objects you refer to contain rules or logic, from, for example, simple validation (i.e. required fields) to more complex logic (i.e. e. processing tax on statement).

  • I do not think that I can answer your next question regarding WCF. The reason for this is that I am going to objectively answer your questions about EF as business objects, which would then subjectively force me to take a position that proves the inconsistency of my attempt to sincerely and objectively answer the first question.

    / li>

However, to your EF as a matter of business objects ...

"I use Microsoft's Entity Framework O / R object mapping module and using the class object (generated classes that are mapped to database objects) as business objects. Is this normal?"

Sorry, there is no right or wrong answer. It depends on your goal and what you consider the most reasonable project, at the same time fully understanding the advantages and disadvantages of this.

"Please indicate your minuses or pluses"

I'm glad you asked! I will be happy to answer, and I hope that, given the pros and cons, you can make an informed decision as to whether you think that using EF for your business objects is OK. As a rule, Id breaks the pros and cons, making it easy to “digest”, but I don’t think it is appropriate here because I think that we are injustice in relation to such a very interesting topic, which is also close and close to my heart.

First of all, let me speak technically for a moment ... You can use EF objects as your business objects, and nothing technically prevents you from doing this. Essentially, EF Code-First (CF) makes this incredibly simple, allowing you to create POCOs and give you the ability to apply data annotations for simple validation, as well as for implementing IValidatableObject for more complex validation. Pretty cool, huh?

This is the essence of the discussion.

EF, or any ORM, is designed to support data management. His main responsibility is data, and therefore the objects you create are data-oriented. So, if you try to also design your objects by behavior, you will have a little puzzle. In a nutshell, this puzzle is called impedance mismatch. Imagine it; You have two required use cases in your application:

  • User Editing Screen
  • A read-only display control of a subset of user information

If you use EF (any taste) or any ORM for that matter, you may be inclined to use the same User object to handle the ability to save the user, as well as to extract the user for a subset, only the field. Perhaps you are doing this for one of several reasons:

  • Like many developers, we have this seed planted in our brains during training. "Strengthening the code" is of paramount importance or perhaps better known as DRY - Dont Repeat Yourself, and therefore you can look at code duplication, for example, in a negative context.
  • ORMs such as EF 4.1 have technical limitations (and hacking workarounds), such as mapping multiple POCOs / objects to the same database table, thereby forcing you regardless of your beliefs.
  • This is a quick and easy way to launch and run the application.
  • It "feels" like the right thing.

There are advantages and disadvantages to this, and you can look at it either positively or negatively, depending on your opinion.

I suppose if you believe in normalizing your code compared to what you did significantly. You were able to limit the amount of code, potentially saving time by writing a single object for processing your data and business use cases for this object.

And I suppose, if you believe in normalizing behavior over code, than you failed. By saving the code, you sacrificed the design of objects with your responsibilities, which potentially complicated management and subsequently increased maintenance costs.

Regardless of your opinion, we can probably agree that this business object has taken on many responsibilities, and the behavior (and not data!) Of the object is secondary at best. His main responsibility is data management, and his secondary responsibilities are the processing of business rules, simple and complex, related to editing the user and displaying user information for read-only purposes. In object-oriented design (OOD), if the design of an object is characterized by its identity and behavior, this object can be one confused person, since it does not correspond to the very definition of OOD.

Something to keep in mind from a technical point of view, every time you request a user object, you inherit a significant portion of the overhead. This can include things like all properties and business rules when only a subset of read-only information is displayed.

So, what does all this have to do with whether I should use EF to represent my business objects?

Well ... Although it is technically possible, there are different philosophies (some good, some bad) about whether to use EF or any ORM to represent your business objects. I gave a review aimed at the heart of these philosophies above, but they are described in much more detail by people like Rocky Lhotka and Martin Fowler.

The direction you directed, most likely, will depend on the application and on the philosophical view, it may depend on how idealistic or pragmatic you are. However, I do not mean that one of the idealists or pragmatists correlates either with the use of EF for business objects or not - it will simply affect your approach to this.

At the time of this writing, Microsoft indicated that EFs are built to handle business logic and, right or wrong, they seem to be moving more in that direction. EF is constantly evolving, and some technical restrictions are removed so that EF can ultimately be used to satisfy the best of both worlds. In other words, you can eventually get your cake and eat it too.

Hope this helps.

Disable to answer the question of whether the lack of knowledge of persistence with ORM is ridiculous given the purpose behind it - to manage the data. :-) Sorry, I could not resist!

+30
Jul 01 2018-11-11T00:
source share

The Entity structure was designed for entity objects that will be used as business objects, but you should keep in mind that business objects will be tied to O / R technology, as well as to the EDM model. In EF 1.0 there was no Persistence-ignorance support, but support was added in 4.0. You can implement interfaces if you do not want to use any of their base classes.

Starting with .NET 3.5 SP1, they should also be used as paramater and return types in WCF service methods without any additional code.

+13
Oct. 20 '08 at 6:38
source share

In my experience, we used EF objects in the business layer of our application, but when we go to the presentation level through our WCF service level, we will create view objects from EF objects.

In our case, only the presentation is passed to the presentation level. We do this to control how the data is presented and to apply a security check for data coming from the presentation layer.

If you ueing EF objects in a WCF transaction, you lose the object context associated with the EF object. There are some efforts at CodePlex that try to help with this, but I did not support their efforts.

+4
Oct 20 '08 at 14:07
source share

Two limitations that should be in the know of the one I am facing are as follows:

  • Inherited objects cannot have Navigation Properties - i.e. if you have a person class, and then a customer and a provider, then these customers and suppliers do not have navigation properties.

  • Methods and computed fields (anything in partial classes) are not passed through ADO.Net data services - if you also use ADO.Net data services, whatever you extend, Entity Framework objects in partial classes will not be passed through ADO data services .Net.

As a rule, these are not show-stopper objects (we do not use inheritance in the entity infrastructure for navigation properties yet), but there may be something interesting for you. I hope that both of these elements will be available in a future release.

+4
Dec 02 '08 at 5:39
source share

Can't you just reattach the objects if they lose the original context of the object? You will need to handle concurrency - use yourself, though.

I would not recommend using EF objects as DataContract objects for WCF, since you would very strongly tie the implementation of entity objects to web service clients, it will be difficult to make a change in the future, the more difficult the more clients you plan for by availability.

+3
Nov 11 '08 at 7:33
source share

The WPL Application Framework (WAF) BookLibrary sample application shows how Model-View-ViewModel (MVVM) can be used in conjunction with the Entity Framework.

+2
Feb 20 '10 at 20:01
source share



All Articles