views:

220

answers:

6

Hey SO,

I just started programming with objects recently and am trying to learn good habits early on.

The way I plan to structure my application is to have two files:

1: Program.cs - This file will contain the main logic for the application
2: Class.cs - This file will contain all of the class definitions

Pretty simple. What I'm wondering if I should have any more files for ... well, you tell me.

Any help would be appreciated.

+5  A: 

It's generally accepted that each Class should have it's own file.

Program.cs - This file will contain the main logic for the application

I am assuming when you say this that you mean that the main class is in this file. (The class with the entry point to the application). The various parts of the logic should be separated out and placed in the classes that make the most sense to have them.

Links to object oriented design:
http://www.csharphelp.com/2006/05/designing-object-oriented-programs-in-c/

http://www.informit.com/articles/article.aspx?p=101373

Links to namespaces:
http://www.csharphelp.com/2006/02/namespaces-in-c/

http://msdn.microsoft.com/en-us/library/dfb3cx8s.aspx

Kevin
Agreed, although if there are small helper classes that are *only used with a particular class,* I will generally put those in the same file as the class they are helping.
Robert Harvey
@Robert, I at times have a habit of doing that too. It depends on the scenario, but generally I follow the rule of each class has it's own file.
Kevin
Should each class file have a different namespace? Can I even use the same namespace across different files?
Soo
@Soo: All of the classes go into the same namespace unless you're building an external library to link to, or attempting to section off some *large* piece of functionality (which is unlikely). The namespace can be something like `MyProgramName`
Robert Harvey
Would it be ok to split it up like:ProgramName.MainProgramName.ClassesIt seems like a good idea to me, is it too much?
Soo
For the name space you'll just want to keep it all the same. Just make sure that all the namespaces are the same. Like Robert said something like: "namespace MyProgramName" Don't bother splitting the namespace up between the main class in the program and the classes.
Kevin
@Soo: Nope. Put everything in the same namespace. Doing what you suggest will require you to put another `Using` statement in every one of your classes.
Robert Harvey
Got it. Hey, I'm learning good and new stuff every day. Thanks SO :)
Soo
+2  A: 

My only suggestion would be to break each class in Class.cs into its own file named ClassName.cs.

It'll make finding and fixing bugs easier down the road.

Less code in each file = less searching to find the offending code.

Justin Niessner
+2  A: 

Each class should have its own file - not one .cs file containing many classes. I'm not sure, not having tried it, but your IDE may enforce this.

jwismar
+2  A: 

The generally accepted principal to follow is to have one file for each class (or partial class, in the case of 2.0+ apps). For any non-trivial application, you certainly would not want all of your class definitions in a single file.

Anthony Pegram
+1  A: 

You should take a look on these guidelines

esylvestre
That would be a better link, if it pointed directly to the page that says put each class in its own file. :)
Robert Harvey
+2  A: 

Here are some basics to help you get started. =)

  1. .Net Naming Conventions and Programming Standards and Best Practices;
  2. Object-Oriented Concepts;
  3. Object-oriented design;
  4. C# Coding Style Guide;
  5. File Organization
  6. Code Convention C#;
  7. Design Guidelines for Class Library Developers;

The architecture of your solution might look like this:

  1. One project for your classes (One class per file);
  2. One project for your data access;
  3. One project for your GUI;
  4. One project for your integration layer (Such as NHibernate, EntityFramework, etc.)

Bear in mind that you must make each piece of code as reusable as possible. Doing so by writing your business objects (your classes) into an independant project will allow you to reference this project into another one later on, so you won't have to recode all of your business logic (methods, etc.) and your business objects (classes, enumerations, interfaces, etc.)

The object-oriented design is trying to generalize every practical aspect of an object and bringing it to the top most general class for your business objects. For instance:

// File: Person.cs
public class Person {
    public string Name { get; set; }
    public string Number { get; set; }
    // Some other general properties...
}

// File: Customer.cs
public class Customer : Person {
    public Customer() {
        Orders = new List<Order>();
    }
    public string CreditTerm { get; set; }
    public IList<Order> Orders { get; }
}

// File: Contact.cs
public class Contact : Person {
    public long PhoneNumber { get; set; }
    public long FaxNumber { get; set; }
}

// File: Supplier.cs
public class Supplier : Person {
    public Supplier() {
        Salesperson = new Contact();
    }
    public Contact Salesperson { get; }
}

It is also recommended to specify what each of your projects stand for. Let's take for instance an application for Customer Management:

MyCompany.MyCustomerMgmtSoftware.Domain <= This project shall contain your business classes definitions

MyCompany.MyCustomerMgmtSoftware.Data <= This project shall contain classes for data accessing your DBRM.

MyCompany.MyCustomerMgmtSoftware <= This project normally contain your GUI

MyCompany.MyCustomerMgmtsoftware.Mappings <= This project should contain your mapping files (while using NHibernate, for instance.

Does this help?

Will Marcouiller
Chris L
This is a nice reference, but it's probably TMI for someone who's just building their first program. And for pretty much everyone else, unless they're building an ERP system.
Robert Harvey
@Chris L: I agree. I stated so for small projects. In my opinion, Soo won't be ready for ginormous projects such as you're mentioning before a while. For a small company home project this structure could suffice.
Will Marcouiller
@Robert Harvey: I agree with you too. Despite, I cannot judge of Soo's knowledge. Perhaps he knows a bunch of stuff on ERP systems, or can he make simple associations with what he knows among the links I provided him. He is the one who knows what is useful for him, not me. I provided links and details as I saw fit for some general purpose. Once Soo will read the articles, he'll for sure be able to make up his mind. Furthermore, this can happen to be useful for some further reference when he perhaps will remember having read about something like he's doing (in the future). =)
Will Marcouiller