Convert a POCO Object to a Business Object

I want to integrate the entity infrastructure as a data layer.

I followed articles and created poco objects using this tutorial: http://blogs.msdn.com/b/adonet/archive/2010/01/25/walkthrough-poco-template-for-the-entity-framework.aspx

I have my own business objects. Here is my Brach business object:

public class Branch { public long BranchId { get; private set; } public string BranchName { get; set; } public string BranchCode { get; set; } public Branch() { } public void InsertBranch(Guid companyId) { using (var ctx = new Entities.Entities()) { var branch = new T_STF_BRANCH() //This is generated POCO object { company_id = companyId, branch_name = BranchName, branch_code = BranchCode }; ctx.T_STF_BRANCH.AddObject(branch); ctx.SaveChanges(); } } public static IList<Branch> GetBranchesList(Guid companyId, long? branchId, string branchName) { using (var ctx = new Entities.Entities()) { var branchs = ctx.T_STF_BRANCH.Where(x => x.is_deleted == false && (branchId == null || x.branch_id == branchId) && (branchName == null || x.branch_name.Contains(branchName))).ToList(); } //HERE I NEED SOMEHOW CONVERT THE POCO ENTITIES INTO MY BUSINESS ENTITIES... } } 

I do not know how to convert a POCO object to my business entity.
Where should I put the conversion from POCO to POCO?

+1
source share
3 answers

IMHO this is too complicated. Why do you have a POCO object for saving and a separate object for working with data loaded into a POCO object? It looks like your application is still archived.

ORM stands for relational object mapping. This means comparing the world of relations and the object world. Usually this can also be translated as a mapping between a database and business objects. Therefore, you should use POCO objects as your business objects. This is the meaning of using POCOs. If you do not want to use them as business objects, you do not need them, and you can directly use the default object objects.

If you want to use POCOs as a business object, just let the EF generate these POCOs for you and add a partial class to each POCO that defines your methods.

Btw. your business object actually looks like an implementation of an Active Record pattern . If you want to use these templates, perhaps you should check out the Windsor Active Record , which is based on the top of NHibernate.

Edit:

Well. You can use your classes instead of the generated POCO objects.

One way is to abandon EFv4 and EDMX and check out the new EFv4.1 and its new free API (also code-based) for display. This is the whole for a separate question or just use the search here on SO.

You can do this with EDMX. There are some basic rules that you must follow in order to do this work, because all this is done using naming conventions. Since you already have classes, you must modify this in the EDMX file so that the conceptual model is the same as your business objects:

  • Each business object that must be saved or loaded must have an object in the conceptual model.
  • The entity must have the same name as the business object. You must also properly configure the object in the properties window (abstract, access level and base object must be the same as in your business object).
  • Each stored property in a business object must have a property in the object in the conceptual model. Again, you must properly configure each property (accessibility getter and setter, type, nullable, etc.).

EDMX consists of three layers:

  • SSDL - a description of the database. It is almost always created, and you cannot change it directly in the designer.
  • CSDL - A description of the objects that should be the same as your business objects. This is what you change in the designer. You can rename the fields as you wish.
  • MSL - mapping between SSDL and CSDL. If you open the context menu for any object in the designer, you will see a table display. It will open a window with the definition of the mapping between CSDL and SSDL.

These are basic rules, but since you already have business objects, you will most likely find situations where it will be difficult to match them. The best way is to simply ask for this particular problem. This will most likely be due to some complex properties, navigation properties, or inheritance.

+10
source

A couple of possibilities here, if I understand you correctly.

You have POCO objects that represent tables in your database, and you have some business classes, or perhaps even view models, and you want to move from one to another.

The first opportunity, in your business entities, is to create a constructor that takes a POCO object as a parameter, and then configure each property.

eg.

 public Branch (POCO poco) { Name = poco.Name; Zip = poco.Zip; } 

Another option is to use a tool like AutoMapper

This will help you automatically display (hence the name;)) two objects.

One thing I could suggest is not to put Branch.GetListOfBranches (), but rather come up with a class like DataLayer.cs or something else, putting most of your query logic there. Thus, you do not have separate objects that know about your data context, and when you need to make changes, there are less places to place them.

We have a database called Sales, and our class is called SalesDb. Then we use this class to retrieve the objects we need. Therefore, we can do SalesDb.GetLeads() or even SalesDb.GetLeads(Filter f) to filter out what we don’t need. SalesDb is now in control of the context, and my Leads class should not know anything about it.

+1
source

If you use POCOs as an external data contract, you might want your model to be different entities / classes for you to prevent free chaining between your external contracts and how your application works.

When searching for a data contract, you can get the corresponding object from the context, then enter the values ​​from the data contract into the object / entity using a tool such as ValueInjecter.

+1
source

All Articles