Aufomaper queryable Extensions Projectto

Source: Internet
Author: User


WhenusingAn ORM such asNHibernate or Entity Framework with AutoMapper's standard mapper.map functions, which notice that the ORM would query all the fields of all the objects within a GRA ph when automapper are attempting to map the results to a destination type.If Your ORM exposes Iqueryables, you can use AutoMapper'S Queryableextensions helper methods to address this key pain.Using Entity Framework forAn example, say, which has an entity orderline with a relationship with an entity Item. If you want to map ThisTo a orderlinedto with the Item'S Name property, the standard Mapper.map call would result in Entity Framework querying the entire orderline and Item t Able. Use Thisapproach instead. Given the following entities: Public classOrderline { Public intId {Get;Set; } Public intOrderId {Get;Set; } PublicItem Item {Get;Set; } Public decimalQuantity {Get;Set; } } Public classItem { Public intId {Get;Set; } Public stringName {Get;Set; } }and the following DTO: Public classOrderlinedto { Public intId {Get;Set; } Public intOrderId {Get;Set; } Public stringItem {Get;Set; } Public decimalQuantity {Get;Set; } }you can use the queryable Extensions like So:Mapper.Initialize (CFG=CFG. Createmap<orderline, orderlinedto>() . Formember (DTO= Dto. Item, conf = Conf. Mapfrom (ol =ol. (Item.name))); PublicList<orderlinedto> Getlinesfororder (intorderId) { using(varContext =Neworderentities ()) { returnContext. Orderlines.where (ol = ol. OrderId = =orderId). Projectto<OrderLineDTO>(). ToList (); }}the. Projectto<OrderLineDTO> () 'll tell AutoMapper's mapping engine to emit a SELECT clause to the IQueryable that would inform Entity Framework that it's only needs to que RY the Name column of the Item table, same as if you manually projected your IQueryable to an orderlinedto with a Select c Lause.Note that for Thisfeature to work, all type conversions must be explicitly handledinchYour Mapping. For example, you can not be rely on the ToString ()Overrideof the ItemclassTo inform Entity Framework to onlySelect fromThe Name column, and any data type changes, such asDouble to Decimal must is explicitly handled asWell . Preventing Lazy loading/select n+1Problemsbecause the LINQ projection built by AutoMapper isTranslated directly to a SQL query by the query provider, the mapping occurs at the sql/ado.net level, and not touching yo ur entities. All data iseagerly fetched and loaded into your dtos.nested collections use a Select to Project child DTOs: fromIinchdb. Instructors byI.lastnameSelect Newinstructorindexdata.instructormodel{ID=i.id, Firstmidname=I.firstmidname, LastName=I.lastname, HireDate=i.hiredate, Officeassignmentlocation=i.officeassignment.location, Courses= I.courses.select (c + =NewInstructorindexdata.instructorcoursemodel {CourseID=C.courseid, Coursetitle=C.title}). ToList ()}; This map through AutoMapper would resultinchA SELECT n+1Problem, asEach child Course is queried one at a time, unless specified through your ORM to eagerly fetch. With LINQ projection, no special configuration or specification isneeded with your ORM. The ORM uses the LINQ projection to build the exact SQL query needed. Custom Projectionin the Case whereMembers names Don'T line up, or the want to create calculated property, you can use Mapfrom (and not resolveusing) to supply a custom ex Pression for a destination member:mapper.initialize (CFG= cfg. Createmap<customer, customerdto>() . Formember (d= = D.fullname, opt + = opt. Mapfrom (c = c.firstname +" "+c.lastname)) . Formember (d= = D.totalcontacts, opt + = opt. Mapfrom (c =C.contacts.count ())); AutoMapper passes the supplied expression with the built projection. asLong asyour query provider can interpret the supplied expression, everything would be passed down all the the-the-the- . If the expression isRejected fromYour query provider (Entity Framework, NHibernate, etc), you might need to tweak your expression until you find one isaccepted. Custom type conversionoccasionally, need to completely replace a type conversion fromA source to a destination type. In normal runtime mapping, This isAccomplished via the Convertusing method. To perform the analoginchLINQ projection, use the projectusing method:cfg. Createmap<source, Dest> (). projectusing (src =NewDest {Value =Ten }); Projectusing isSlightly more limited than convertusing asonly what isAllowedinchAn Expression and the underlying LINQ provider would work. Custom destination type Constructorsif your destination type have a custom constructor but you don't want to override the entire mapping and use the Constructprojectionusing method:CFG. Createmap<source, dest>() . Constructprojectionusing (src=NewDest (src. Value +Ten)); AutoMapper'll automatically match up destination constructor parameters to source members based on matching names Ly Use ThisMethodifAutoMapper can'T match up the destination constructor properly, or if you need extra customization during construction.String Conversionautomapper would automatically add ToString () when the destination member type isAstringAnd the source member type isNot . Public classOrder { PublicOrdertypeenum OrderType {Get;Set; }} Public classOrderdto { Public stringOrderType {Get;Set; }} varOrders = dbcontext.orders.projectto<orderdto>(). ToList (); orders[0]. Ordertype.shouldequal ("Online"); Explicit Expansionin Some scenarios, such asOData, a generic DTO isReturned through an IQueryable controller action. WithoutExplicitInstructions, AutoMapper'll expand all MembersinchThe result. To control which members is expanded during projection, passinchThe members of want to explicitly expand:dbContext.Orders.ProjectTo<OrderDto>(Parameters=NULL, Dest=Dest. Customer, Dest=Dest. LineItems);//or string-basedDbcontext.orders.projectto<orderdto>(Parameters=NULL, "Customer", "LineItems"); AGGREGATIONSLINQ can support aggregate queries, and AutoMapper supports LINQ extension methods. In the custom projection example,ifwe renamed the Totalcontacts property to Contactscount, AutoMapper would match to the Count () extension method and th E LINQ provider would translate the count into a correlated subquery to aggregate child records. AutoMapper can also support complex aggregations and nested restrictions,ifThe LINQ provider supports IT:CFG. Createmap<course, coursemodel>() . Formember (M=M.enrollmentsstartingwitha, opt= = Opt. Mapfrom (c = c.enrollments.where (E = E.student.lastname.startswith ("A")). Count ())); This query returns the total number of students, forEach course, whose last name starts and the letter'A'. Parameterizationoccasionally, projections need runtime parameters forTheir values. Consider a projection that needs to pullinchThe current username asPart of its data. Instead ofusingPost-mapping code, we can parameterize our Mapfrom configuration:stringCurrentUserName =NULL; cfg. Createmap<course, coursemodel>() . Formember (M= = M.currentusername, opt + = opt. Mapfrom (src =currentusername)); When we project, we'll substitute our parameter at runtime:DbContext.Courses.ProjectTo<CourseModel> (Config,New{CurrentUserName =Request.User.Name}); This works by capturing the name of the closure's field name in the original expression, then using a anonymous object/dictionary to apply the value to the parameter Value before the query is sent to the query provider.supported mapping Optionsnot all mapping options can be supported, asThe expression generated must is interpreted by a LINQ provider. only what isSupported by LINQ providers issupported by Automapper:mapfromignoreusevaluenullsubstitutenot supported: Conditiondonotusedestinationvaluesetmappingorderusedestinationvalueresolveusingbefore/aftermapcustom resolverscustom Type Convertersany calculated property on your domainObjectAdditionally, recursive or self-referencing destination types is not supported asLINQ providers DoNot support This. Typically hierarchical relational data models require common table expressions (CTEs) to correctly resolve a recursive Joi N.

Https://github.com/AutoMapper/AutoMapper/wiki/Queryable-Extensions

Aufomaper queryable Extensions Projectto

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.