Partial classes in C #

Are partial classes well used outside of scripting scripts created with webforms / winforms? Or does this feature mainly support this?

+14
c # partial-classes
Oct 02 '08 at 1:44
source share
20 answers

Partly to support scripts (WebForms, WinForms, LINQ-to-SQL, etc.) mixing the generated code with the programmer's code.

There are more reasons to use it. For example, if you have large classes in large, bulky files, but classes have groups of logically related methods, partial classes may be able to make your file sizes more manageable.

+11
Oct 02 '08 at 1:49
source share

Code generation has been the driving force behind partial classes. The need is based on the fact that the class generated by the code is constantly changing, but allows developers to provide their own code as part of the class, which will not be redefined every time there are changes that cause the class to be restored.

Take WinForms or Typed-DataSets, for example (or any designer, for that matter). Each time you make changes to the constructor, it serializes the corresponding code to the file. Let's say you need to provide some additional methods that the generator knows nothing about. If you added it to the generated file, your changes will be lost the next time you create it.

The project I'm working on uses code generation for all DALs, BLLs, and business objects. However, the generator receives only 75% of the information. The rest should be hand-encoded (e.g., custom business logic). I can assume that every BLL class has a SelectAll method, so it's easy to generate. However, my BLL client should also have a SelectAllByLocation method. I cannot put this in my generator because it is not common to all BLL classes. Therefore, I generate all my classes as partial classes, and then in a separate file I define my own methods. Now along the way, when my structure is changing, or I need to restore my BLL for some reason, my user code will not be destroyed.

+7
Oct 02 '08 at 2:33
source share

I use partial classes as a means of separating the various sub-elements of the user controls that I write. In addition, when used with entity creation software, it allows products such as LLBLGen to create generated versions of classes, as well as a custom user-edited version that will not be replaced if objects need to be restored.

+6
02 Oct '08 at 1:51
source share

I often use partial classes to give each nested class its own file. There were some architectures that I worked on, where most of the implementation is required by only one class, and therefore we have nested these classes in one class. It was understood that files are easier to maintain using the ability of a partial class and breaking each into its own file.

We also used them to group stock overrides or to hide a set of properties. Such things. This is a convenient way to mix in changing stocks (just copy the file and change the name of the partial class to the target class) if the target class is also made partial).

+5
Oct 02 '08 at 2:18
source share

Another possible use for partial classes would be to use partial methods to selectively remove methods using conditional compilation — this would be great for debugging diagnostic scripts or specialized test scripts.

You can declare a partial method like an abstract method, and then in another partial class, when you enter the partial keyword, you can use Intellisense to create an implementation of this method.

If you surround one part using conditional assembly statements, you can easily disable only debug or test code. In the example below, in the DEBUG mode, the LogSomethingDebugOnly method is called, but in the release assembly it, like the method, does not exist at all - a good way to remove the diagnostic code from the production code without a branch heap or several conditional compilation blocks.

// Main Part public partial class Class1 { private partial void LogSomethingDebugOnly(); public void SomeMethod() { LogSomethingDebugOnly(); // do the real work } } // Debug Part - probably in a different file public partial class Class1 { #if DEBUG private partial void LogSomethingDebugOnly() { // Do the logging or diagnostic work } #endif } 
+4
Oct 23 '08 at 3:31
source share

LINQ to SQL makes good use of partial classes to extend the code created by the developer. I think you will usually find that this partial class template is used by the code created by the constructor.

+3
Oct 02 '08 at 1:46
source share

I find partial classes extremely useful. They are usually used to expand auto-generated classes. I used them in one project with heavy unit tests. My UT classes had complex dependencies, and it wasn’t very convenient to split the code into several classes. Of course, it is better to use inheritance / composition, but in some cases partial classes can be useful for rallying.

+3
02 Oct '08 at 1:55
source share

As mentioned earlier, I also think this is the smell of code.

If the class is so large that it needs to be divided into more files, this means that it violates the principle of uniform responsibility and does too many things. A large class can be divided into smaller classes that interact with each other.

If you need to use partial classes or regions to organize your code, consider whether they should be in their classes. This improves readability and you get more code reuse.

+2
Oct 02 '08 at 10:59
source share

maybe too late, but please let me add my 2 cents too:

*. When working on large projects, distributing the class to separate files allows you to work with several programmers at the same time.

*. You can easily write your own code (for extended functionality) for the generated VS.NET class. This will allow you to write the code of your own need without delving into the code generated by the system.

+2
Jul 15 2018-11-11T00:
source share

Where I am, we have a program that processes incoming files from clients. It is configured in such a way that each client code is in its own class library project, which knows how to handle any format that the client wants to use.

The main code uses libraries, defining a fairly extensive interface that the class should implement in the library (maybe it should be several different interfaces, but too late to change it now). Sometimes this involves a lot more code in the same class than we usually think is reasonable. Partial classes allow us to break them down a bit.

+1
Oct 02 '08 at 1:48
source share

In UserControls, which are relatively complex, I put the event processing material in one file, as well as the picture and properties in another file. Partial classes are great for this. Usually these parts of the class are relatively independent, and it's nice to have the ability to edit images and handle events side by side.

+1
02 Oct '08 at 1:56
source share

I worked on a project a couple of years ago when we had a typed DataSet class that had tons of code: methods in DataTables, methods in TableAdapters, declarations of instances of TableAdapter that you name. This was a huge central point of the project, in which everyone had to work often, and there were many conflicts with source control over a partial class code file.

So, I divided the code file into a patch or six files with a partial class, grouped by function, so that we could work with smaller parts and did not have to lock the entire file every time we had to change some little thing.

(Of course, we could also solve the problem without using an exceptionally-blocking source control system, but this is another problem.)

+1
Oct 02 '08 at 3:45
source share

As a rule, I consider it a code smell.

If your class is so complex, it can probably be broken down into smaller reusable components.

Or it means that theres no inheritance hierarchy where there should be one.

For code generation scenarios, this is good, but I think code generation is another smell of code.

+1
02 Oct '08 at 4:10
source share

I'm late in the game ... but only my 2 cents ...

You can use the refactoring of an existing god class in an existing database of obsolete codes for several partial classes. This can improve code detection - if the correct naming convention is followed for file names containing partial classes. It can also reduce the source code repository - allow and merge to the extent.

Ideally, the class of gods should be divided into several small classes - each of which has a separate responsibility. It is sometimes difficult to respond from medium to large. In such cases, partial classes may provide temporary relief.

+1
Mar 02 '11 at 7:01
source share

Correction, as Matt noted, both parts of a partial need to be in the same assembly. my bad.

0
Oct 02 '08 at 2:00
source share

I use it at the data access level. Generated classes such as mapper and partial requests. If I need to add a matching method, for example, to make an invisible load that is not generated, I add it to the user class.

At the end, the programmer that uses the data layer in the business layer sees only one class with all the functions it needs. And if the data source changes, the common parts can be easily generated without overwriting custom materials.

0
Oct 02 '08 at 2:01
source share

I just found use for partial classes. I have a class [DataContract], which I use to transfer data to the client. I wanted the client to be able to display the class in a specific way (text output). so I created a partial class and redefined the ToString method.

0
Oct 02 '08 at 2:07
source share

Sometimes you may find a terribly old code at work that can make it almost impossible to reorganize into separate elements without breaking existing code.

Unless you have the option or time to create a more authentic architecture, partial classes make it incredibly easy to separate logic where necessary. This allows existing code to continue to use the same architecture while you approach a more specific architecture.

0
Oct 02 '08 at 2:45
source share

Anywhere where you would use #region sections before, it probably makes more sense as separate files in partial classes.

I personally use partial classes for large classes, where static members are in one file and instance members are in another.

0
Oct 02 '08 at 3:51
source share

EDIT: DSL Tools for Visual Studio uses partial classes.

Thus, it is a function used by many automatic generated codes. Instead of using #region, the automatically generated code goes to one file, and the user code (also called user code) goes to another and even to different directories, so the developer does not get confused with so many meaningless files.

It’s good to have this choice, which you can combine, but not forcefully use - with inheritance

In addition, it may be convenient to divide the logic of some classes among several directories. Of course, this is the same for machines, but it improves user readability.

0
Oct 02 '08 at
source share



All Articles