Tuesday, October 11, 2011

Extenend ORM generated class

Shout it kick it on DotNetKicks.com
In this post I am going to show how you can extend the class generated by the ORM tools. To demonstrate I am using Linq To Sql ORM.

When you make use of ORM tool like Linq to Sql or Entity-Framework it generate the classes from the database structure given as input. For example consider below example
Now I want to display the list of product in my grid but I have to display product name with categoryname for example productname(categoryname) or consider situation where I have ordertable and I have to display one more extra column in grid with display total = quantity * price.

To achieve this make look to the class generated by ORM tools, when you see the class definition you see its decorated with the partial keyword. C#2.0 partial class allow us to create class which expands in two different file and at the time of compile both file get compile in one class. So by making use of same rule I have added one more class file and its partial as below.
public partial class Product
{
   public string ProductWithCategory
   {
      get
      {
         return this.ProductName + "(" + this.Category +")";
      }
   }
}
Now to we can consume property in the presentation or businesslayer like as below.
var productlist = (from p in context.Products select p).ToList();
foreach (Product p in productlist)
{
   Console.WriteLine(p.ProductWithCategory);
}
So by above way adding property to partial class we can easily achieve the task.

Summary
By using partial class we can add the custom logic to the class created by the ORM tool(s).

20 comments:

  1. This detailed step-by-step explanation helps a lot of people. Databases are the key of new systems. I´m going to Argentina the next month and the only thing I know there is going to be in the apartment in buenos aires I booked is that there is a computer. I hope I will still use all of the programs I have installed!
    Amy

    ReplyDelete
  2. Have you ever thought about including a little bit more than just
    your articles? I mean, what you say is important and everything.
    But just imagine if you added some great pictures or video
    clips to give your posts more, "pop"! Your content is excellent but with pics and videos, this site could undeniably be one of the very best in its field.
    Awesome blog!

    Stop by my blog :: garcinia uses

    ReplyDelete
  3. It's nearly impossible to find educated people for this topic, but you sound like you know what you're talking about!

    Thanks

    my webpage ... green coffee beans extract uk

    ReplyDelete
  4. Link exchange is nothing else except it is simply placing the other person's website link on your page at appropriate place and other person will also do similar in support of you.

    my page green coffee bean extract scam

    ReplyDelete
  5. I was wondering if you ever considered changing the layout of your website?
    Its very well written; I love what youve got to say.
    But maybe you could a little more in the way of content so people could connect with it better.
    Youve got an awful lot of text for only having 1 or two pictures.
    Maybe you could space it out better?

    my site - benefits of moringa

    ReplyDelete
  6. If you desire to take a great deal from this post then you have to apply these techniques to
    your won website.

    Here is my site :: light teeth whitening

    ReplyDelete
  7. It's genuinely very complex in this busy life to listen news on TV, therefore I only use web for that purpose, and obtain the latest news.

    Here is my weblog; planta de moringa

    ReplyDelete
  8. Whoa! This blog looks just like my old one! It's on a totally different subject but it has pretty much the same page layout and design. Great choice of colors!

    my blog post: teeth whitening

    ReplyDelete
  9. Great beat ! I wish to apprentice while you amend your
    site, how can i subscribe for a blog website? The account helped me a acceptable deal.
    I had been tiny bit acquainted of this your broadcast provided bright clear concept

    Here is my web-site :: tanda pearl ionic teeth whitening system

    ReplyDelete
  10. Definitely consider that that you stated. Your favorite reason appeared
    to be at the web the simplest thing to be mindful of.
    I say to you, I certainly get irked while other folks consider worries that they plainly don't understand about. You controlled to hit the nail upon the top and outlined out the entire thing without having side effect , other people can take a signal. Will probably be back to get more. Thanks

    Feel free to visit my web page: raspberry ketones

    ReplyDelete
  11. Hey there just wanted to give you a quick heads up and let you know a few of the images aren't loading properly. I'm not sure why but I think its a linking issue.

    I've tried it in two different browsers and both show the same results.

    My site ... moringa olifera

    ReplyDelete
  12. Howdy would you mind stating which blog
    platform you're using? I'm planning to start my own
    blog soon but I'm having a tough time deciding between BlogEngine/Wordpress/B2evolution and Drupal. The reason I ask is because your layout seems different then most blogs and I'm looking for something completely
    unique. P.S My apologies for getting off-topic but
    I had to ask!

    Here is my website :: garcinia quaesita fruit

    ReplyDelete
  13. What i ԁon't realize is in reality how you're not асtuallу а lot more ωell-likеd than you mіght be now.
    You're so intelligent. You recognize therefore considerably on the subject of this matter, made me in my view believe it from so many various angles. Its like women and men aren't involveԁ еxcept it
    is one thing to accomplish with Womаn gagа!
    Υouг inԁividual stuffѕ outstаnding.
    All thе time care foг it up!

    Cheсk out my weblοg - green coffee

    ReplyDelete
  14. My family memberѕ all the timе ѕаy that I am killіng my time hеre at wеb,
    exсept I know I am getting knowledgе all the
    tіme by гeading such gοod contеnt.


    Heгe is my wеbpage; pure raspberry ketone

    ReplyDelete
  15. When ӏ originally commеnted Ι sеem to have clickеd the -Notifу me when new сommentѕ aге
    added- checkbοx and from now оn еаch time a сοmment is adԁеd I reсіeve 4 emaіls ωith the same comment.
    Is there аn easy methoԁ you arе аble to
    remove me fгom that servicе? Thаnks a
    lot!

    Also νіѕit my web-ѕite - pure raspberry ketones 500

    ReplyDelete
  16. Useful information. Fortunate me I found уοuг site аccіdentally, and I am ѕurprisеd why
    this twist of fаte didn't happened earlier! I bookmarked it.

    My web site: knigolub.sumy.ua

    ReplyDelete
  17. Hеllο! Do you know if thеy maκe аnу plugins to pгotect аgainst hacκers?
    І'm kinda paranoid about losing everything I'vе worκeԁ
    haгd on. Any tiрs?

    my website: pure green coffee bean extract capsules

    ReplyDelete
  18. Whу peοple still uѕе to read neωs papers whеn in thіs technological worlԁ all
    іs available on net?

    Hеre iѕ mу website: raspberry ketone reviews

    ReplyDelete
  19. Thank you fоr every other magnіficent poѕt.
    Whеre elѕe maу аnуbodу get thаt tуpe of infο in ѕuch a peгfeсt method οf writіng?
    Ι've a presentation subsequent week, and I am at the look for such info.

    my website ... raspberry ketones plus - allthebestraspberryketones.wordpress.com -

    ReplyDelete
  20. Niсe post. I was сheckіng constantly
    this blog anԁ I'm impressed! Very useful information specially the last part :) I care for such information much. I was seeking this particular info for a very long time. Thank you and best of luck.

    My web page - raspberry ketone slim

    ReplyDelete