Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I've started working for a company that maintains a small/medium sized commercial website. The website is written in c# asp.net, and uses SQL Server as a database. The structure of the code is very messy at the moment (for instance, ~5000 line Utility.cs file) so I want to move towards a better design on the asp.net side of the application.

I'm trying to figure out the right way to do this. My basic plan is to create a class for every logically distinct item in the application (mostly mapping 1-1 to database tables, but sometimes N-1 or 1-N). Then, I plan to have some static factory methods to instantiate these classes by querying the database. The developer should be able to manipulate the resultant object, and then use a UpdateDatabase() method to save the changes to the database.

For example, here is how an Email object would work:

public class Email {
    Guid userId;
    Guid emailId;
    String emailValueInitial = null;
    Sting emailValueNew = null;

    private Email();

    public static Email RetrieveEmail(Guid UserId, Guid EmailId) {
        // .. query
        return new Email(UserId,EmailId,emailValueInitial);            
    }

    public String EmailValue { 
        get {
            if (emailValueNew == null) return emailValueInitial;
            else return emailValueNew;             
        }
        set {
            emailValueNew = value;
        }
    }

    public bool Update() {
       // .. update database with value of emailValueNew or do nothing if it hasn't 
       // been changed
    }

}

Notes:

  • I could update the database whenever a field is changed, but this is obviously inefficient.
  • I don't know what to do in the event that the developer wants to insert a null value into the database - I suppose I need different concepts for 'null as variable not yet used' vs 'null as database value null'.

The above is a simple case. I'd also need some way of knowing about many-one relationships in tables. For instance, a User could work like this:

public class User {
    Guid userId;

    public Emails emails = new Emails(userId);

    private User(); // etc..

    public RetrieveUser();
    public CreateUser();

    public Update();
}

public class Emails {
    List<Email> emails = new List<Email>();

    Guid userId;

    public Emails(Guid userId); // etc..

    public bool CreateEmail(); // its convinient to create/delete from here
    public bool DeleteEmail(); // as I can enforce, say, minimum 1 email per user
}

These above are my first thoughts on how to solve this problem, but I've never done anything like this before, and I don't know what problems I might come across. I want to make a system that will be scaleable and easy to develop as we take on more programmers, so its really important to get the initial design correct.

What kind of design should I use in an object-orientated language to best represent and link with a database? What are important things to consider? Is there existing literature on this subject, or important libraries/constructs I have neglected?

share|improve this question
6  
" I've never done anything like this before, and I don't know what problems I might come across" You need to stop right now. You need to learn about Object-Relational Mapping. You need to learn a lot. This has already been done, and is a first-class part of C# and .NET. But do not start writing code until you've done every tutorial on C# .NET ORM you can find. You might even want to pay for training on this. It's simple, well-understood, and common. But new to you. –  S.Lott Feb 23 '12 at 11:18
    
@S.Lott I'm reading up on this now, thanks. –  Oliver Feb 23 '12 at 11:33

1 Answer 1

up vote 4 down vote accepted

I would suggest taking a serious look at ORM solutions like entity framework and nhibernate.

Then I would suggest drawning up a vision of where you want the architecture to move to over time. stick that on the wall, and tell every developer about. Take their feedback and improve it. And make sure that every developer buys into the vision.

Then to move towards the vision I'd take an iterative and test-driven approach of bringing different parts of the system under test and improving it towards the vision incrementally. For much more on how to approach this part please read Michael Feahters' Working Effectively with Legacy Code.

share|improve this answer
    
+1 for make a suggestion how to deal with the legacy system, not just how the new system could be done "from scratch". IMHO it is most often more important to keep the old system alive (with a gradually rewrite, of course), than throwing any fancy ORM solution at it. –  Doc Brown Feb 23 '12 at 11:45
    
I like the legacy code book. Working out how to deal with code that works, but is basically unmaintainable, is one of those important problems I've never got the hang of. –  Oliver Feb 23 '12 at 11:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.