4

Possible Duplicate:
Why use partial classes?

I am new to partial classes and was wondering if someone could give me a big picture of why I would use them and what advantage I would gain in the process.

What i need to do is, i am saving my data from a windows form to multiple tables say 5 tables. And i want to reuse the code some where else.

I have made a DAL class, now how can i implement this partial class to save the data?

Thanks for the support in advance..:)

I have created something like this: is it good to use?

class clsDAL
    {
        public partial class SaveData
        {
            public void SpClient()
            {

            }
        }

        public partial class SaveData
        {
            public void SpCountry()
            {
            }
        }

        public partial class SaveData
        {
            public void SpDuration()
            {
            }
        }

        public partial class SaveData
        {
            public void SpProfile()
            {
            }
        }

        public partial class SaveData
        {
            public void SpWorkload()
            {
            }
        }
    }
Community
  • 1
  • 1
Paz
  • 694
  • 1
  • 5
  • 14
  • 1
    You are asking two questions here : why use partial classes and is your `DAL` design good. That should be two separate questions. For the second question there's hardly any info on it to answer. Prima facie it looks like a bad design to have a single function in separate partial classes. – nawfal Dec 29 '12 at 06:14

3 Answers3

5

Partial classes are meant for splitting a (large) class into two or more smaller classes often written in separate files. It's not necessary that they have to be in separate files. You can do it like:

public partial class SaveData
{
    public void SpClient()
    {

    }
}

public partial class SaveData
{
    public void SpCountry()
    {

    }
}

in the same file. But that will rarely make sense. Separating it out to multiple files will be better.

What for:

It can sometime help in categorizing your code from a single class into separate blocks even if they all basically form the part of the same class. Having SpClient functionality in SpClient.cs file will help you better to understand what code in that file will do (and similarly SpCountry in SpCountry.cs)..

This is rarely needed though. One particular case is where VS auto-generates designer code into separate designer.cs for Form classes in case of WinForms. This means that you dont have to see the bulk of unimportant code (which you wont be dealing with most probably) in your most important class where your application logic lies. So a Form class would look like:

public partial class Form1 : Form

which will be present in designer.cs too.

Another possible clever use will be:

public partial class SaveData : IEquatable<SaveData>
{
    //implement IEquatabale here 
}

public partial class SaveData : IComparable<SaveData>
{
     //implement IComparable here 
}

You can have partial classes for static classes too:

static partial class Extensions //in StringExtension.cs
{

}

static partial class Extensions //in MathExtension.cs
{

}

You only need static keyword on one of the class. Compiler assumes every other partial class of this type is static. In other words you can do:

static partial class Extensions //in StringExtension.cs
{

}

partial class Extensions //in MathExtension.cs
{
    //which will be static
}

In your case I see no special reason to separate concerns into different partial classes. Better off to write it in one class. And you must write code to make things work..

nawfal
  • 70,104
  • 56
  • 326
  • 368
1

All the 'partial' keyword does is spread your class code among multiple files. It's great for organizing. Otherwise, it's just like a class.

You can re-use a class. Assuming it is not static, you can instantiate it into distinct objects. If it's static and accessible, you can just call its methods.

Matt
  • 25,943
  • 66
  • 198
  • 303
  • Can you show me a example, how can i use it against saving data to multiple tables in mysql? I am confused doing the same. – Paz Dec 29 '12 at 05:24
1

Partial classes are useful to split the same class across multiple files, it has nothing to do with code reuse. The problem you are describing is a typical CRUD operation in which you need to map forms fields to fields in you data store (sql server, xml, document database (no sql), etc). The DAL is a great start. Just save your form data in a DTO (data transfer object) and pass it to your DAL to persist the data. Do not be concerned about partial classes, they are rarely useful. If your classes need to be split across multiple file and grow uncontrollably, it means they are doing too much. Respect the SRP (single responsibility principle) and keep your classes with a narrow focus. Good luck!

Diego
  • 998
  • 1
  • 11
  • 20