locked
Classes v Partial Classes RRS feed

  • Question

  • User2074931137 posted

    Trying to figure out why the Genre.cs table (below) is a "partial class" and the others "classes"...

    Research on web tells me that partial classes allow you to break up a class accross two models - which you can kind of intuitively understand from the name "partial," but that doesn't really help me to really understand how this is used practically.

    1. In the example below - why would Genres.cs be partial. 

    2. Partial with what - with what other model has it split up data? 

    3. How can you tell?
    _______________

    Models / Album.cs

    namespace MvcMusicStore.Models
    {
        public class Album
        {
            public int AlbumId { get; set; }
            public int GenreId { get; set; }
            public int ArtistId { get; set; }
            public string Title { get; set; }
            public decimal Price { get; set; }
            public string AlbumArtUrl { get; set; }
            public Genre Genre { get; set; }
            public Artist Artist { get; set; }
        }
    }
    _______________

    Models / Artist.cs

    namespace MvcMusicStore.Models
    {
        public class Artist
        {
            public int ArtistId { get; set; }
            public string Name { get; set; }
        }
    }
    _______________

    Models / Genre.cs

    namespace MvcMusicStore.Models
    {
        public partial class Genre
        {
            public int GenreId { get; set; }
            public string Name { get; set; }
            public string Description { get; set; }
            public List<Album> Albums { get; set; }
        }
    }

    Monday, December 3, 2012 6:35 PM

Answers

  • User1904378495 posted

    Partial classes and regular classes are both the same thing under the hood. The compiler combines the partial classes into one.

    Partial classes are especially helpful when you have code that is generated and likely to change. You can split up the class into a file that is auto generated or likely to changes and a stable class that won't.

    For example using ef database first, classes are generated based on database tables. If I added any cusotmizations they would be lost if I regenerated my class. In this case I can put my custom code in another file with a partial class. This way if the table code is regenerated, I won't lose my customisations and ultimately when compiled it's 1 class. I hope that makes sense.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 3, 2012 6:56 PM

All replies

  • User1904378495 posted

    Partial classes and regular classes are both the same thing under the hood. The compiler combines the partial classes into one.

    Partial classes are especially helpful when you have code that is generated and likely to change. You can split up the class into a file that is auto generated or likely to changes and a stable class that won't.

    For example using ef database first, classes are generated based on database tables. If I added any cusotmizations they would be lost if I regenerated my class. In this case I can put my custom code in another file with a partial class. This way if the table code is regenerated, I won't lose my customisations and ultimately when compiled it's 1 class. I hope that makes sense.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 3, 2012 6:56 PM
  • User2074931137 posted

    That is more useful information than I had before.  Thank you Hattan.

    My trouble comes because I need to apply these concepts to the most basic examples e.g. the Music Store Tutorail.  My question becomes: why does the Music Store tutorial have Album and Artists as classes and Genre as a partial class.

    I'll make a new post for this specific question. 

    Thanks again.

    Dean

    Tuesday, December 4, 2012 9:28 AM