Click here to Skip to main content
15,908,775 members
Home / Discussions / Design and Architecture
   

Design and Architecture

 
QuestionStylesheet in VS2008 Pin
sarang_k30-Dec-08 19:01
sarang_k30-Dec-08 19:01 
AnswerRe: Stylesheet in VS2008 Pin
CodingYoshi4-Jan-09 15:45
CodingYoshi4-Jan-09 15:45 
QuestionException Hierarchy Pin
CodingYoshi29-Dec-08 11:27
CodingYoshi29-Dec-08 11:27 
GeneralRe: Exception Hierarchy Pin
Luc Pattyn29-Dec-08 15:12
sitebuilderLuc Pattyn29-Dec-08 15:12 
GeneralRe: Exception Hierarchy Pin
CodingYoshi30-Dec-08 3:18
CodingYoshi30-Dec-08 3:18 
AnswerRe: Exception Hierarchy Pin
Jon Rista6-Jan-09 8:48
Jon Rista6-Jan-09 8:48 
GeneralRe: Exception Hierarchy Pin
Leftyfarrell6-Jan-09 13:29
Leftyfarrell6-Jan-09 13:29 
GeneralRe: Exception Hierarchy Pin
Jon Rista6-Jan-09 17:31
Jon Rista6-Jan-09 17:31 
Lefty has some good suggestions, so to clarify with some code examples, here is an updated version of the snippet I posted before. I have made a couple other important changes that will further improve your decoupling and make your product more maintainable in the long run:

// API - This is shared between all layers
interface IRepository<t>
{
    T GetByID(int id);
    T Insert(T item);
    void Update(T item);
    void Delete(T item);
}

class DataAccessException: ApplicationException
{
    // ...
}

class BusinessException: ApplicationException
{
    // ...
}

class PresentationException: ApplicationException
{
    // ...
}

// Service layer - Primary exception handling here
class PersonService
{
    public PersonService(IRepository<person> repository)
    {
        _repository = repository;  // Inject the repository, so it can be mocked when you unit test PersonService
    }

    private IRepositoru<person> _repository;

    Person Load(int id)
    {
        // Always throw ArgumentExceptions for parameter validations, rather than BusinessExceptions
        if (id <= 0) throw new ArgumentException("The Person ID must be greater than zero."); 

        try
        {
            Person person = _repository.GetByID(id);
            return person;
        }
        catch (DataAccessException ex)
        {
            throw new BusinessException("An error occurred while accessing data.", ex);
        }
        catch (ArgumentNullException ex)
        {
            throw ex;
        }
        catch (ArgumentException ex)
        {
            throw ex;
        }
        catch (Exception ex)
        {
            throw new BusinessException("An error occurred while processing your request.", ex);
        }
    }

    void Save(Person person)
    {
        if (person == null) throw new ArgumentNullException("person");
        
        try
        {
            if (person.ID <= 0)
            {
                _repository.Insert(person);
            }
            else
            {
                _repository.Update(person);
            }
        }
        catch (DataAccessException ex)
        {
            throw new BusinessException("An error occurred while updating data.", ex);
        }
        catch (ArgumentNullException ex)
        {
            throw ex;
        }
        catch (ArgumentException ex)
        {
            throw ex;
        }
        catch (Exception ex)
        {
            throw new BusinessException("An error occurred while processing your request.", ex);
        }
    }
}

// Business Layer - Just data and basic behavior here
class Person
{
    int ID { get; set; }
    string Name { get; set; }
}

// Data Access Layer - Handle all data management here
class PersonRepository: IRepository<person> // Implements IRepository<t> from API
{
    Person GetByID(int id)
    {
        if (id <= 0) throw new ArgumentException("The ID must be greater than zero.");

        try
        {
            // Load a person object from the database
        }
        catch (Exception ex)
        {
            throw new DataAccessException(
                String.Format("An error occurred retrieving the requested Person: {0}", id), ex
            );
        }
    }

    void Update(Person person)
    {
        if (person == null) throw new ArgumentNullException(person);

        try
        {
            // Update a person in the database
        }
        catch (Exception ex)
        {
            throw new DataAccessException(
                String.Format("An error occurred updating an existing Person: {0}", person.ID), ex
        }
    }

    Person Insert(Person person)
    {
        if (person == null) throw new ArgumentNullException(person);

        try
        {
            // Insert a person into the database, returning the person with an updated Key (ID in this case)
        }
        catch (Exception ex)
        {
            throw new DataAccessException(
                "An error occurred inserting a new Person.", ex 
            );
        }
    }

    void Delete(Person person)
    {
        if (person == null) throw new ArgumentNullException(person);

        try
        {
            // Delete a person from the database
        }
        catch (Exception ex)
        {
            throw new DataAccessException(
                String.Format("An error occurred deleting an existing Person: {0}", person.ID), ex
            );
        }
    }
}
</t></person></person></person></t>

GeneralRe: Exception Hierarchy Pin
Curtis Schlak.25-Feb-09 9:02
Curtis Schlak.25-Feb-09 9:02 
AnswerRe: Exception Hierarchy Pin
Curtis Schlak.25-Feb-09 9:20
Curtis Schlak.25-Feb-09 9:20 
QuestionWho can show me the process and method of Scrum? Pin
Tal Rasha's Guardianship18-Dec-08 21:32
Tal Rasha's Guardianship18-Dec-08 21:32 
AnswerRe: Who can show me the process and method of Scrum? Pin
Ashfield18-Dec-08 22:01
Ashfield18-Dec-08 22:01 
GeneralRe: Who can show me the process and method of Scrum? Pin
Tal Rasha's Guardianship22-Dec-08 17:17
Tal Rasha's Guardianship22-Dec-08 17:17 
GeneralRe: Who can show me the process and method of Scrum? Pin
Ashfield26-Dec-08 22:40
Ashfield26-Dec-08 22:40 
QuestionArchitecture that supports Unit Testing - Is there such a thing as Too Many Interfaces? Pin
Leftyfarrell18-Dec-08 4:42
Leftyfarrell18-Dec-08 4:42 
AnswerRe: Architecture that supports Unit Testing - Is there such a thing as Too Many Interfaces? Pin
led mike19-Dec-08 7:40
led mike19-Dec-08 7:40 
QuestionRe: Architecture that supports Unit Testing - Is there such a thing as Too Many Interfaces? Pin
Leftyfarrell19-Dec-08 8:08
Leftyfarrell19-Dec-08 8:08 
AnswerRe: Architecture that supports Unit Testing - Is there such a thing as Too Many Interfaces? Pin
Giorgi Dalakishvili19-Dec-08 8:19
mentorGiorgi Dalakishvili19-Dec-08 8:19 
AnswerRe: Architecture that supports Unit Testing - Is there such a thing as Too Many Interfaces? Pin
Mark Churchill22-Dec-08 18:42
Mark Churchill22-Dec-08 18:42 
AnswerRe: Architecture that supports Unit Testing - Is there such a thing as Too Many Interfaces? Pin
CodingYoshi25-Dec-08 21:03
CodingYoshi25-Dec-08 21:03 
AnswerRe: Architecture that supports Unit Testing - Is there such a thing as Too Many Interfaces? [modified] Pin
Jon Rista6-Jan-09 9:01
Jon Rista6-Jan-09 9:01 
GeneralRe: Architecture that supports Unit Testing - Is there such a thing as Too Many Interfaces? Pin
Leftyfarrell6-Jan-09 13:10
Leftyfarrell6-Jan-09 13:10 
GeneralRe: Architecture that supports Unit Testing - Is there such a thing as Too Many Interfaces? Pin
Jon Rista6-Jan-09 13:36
Jon Rista6-Jan-09 13:36 
QuestionRe: Architecture that supports Unit Testing - Is there such a thing as Too Many Interfaces? Pin
Leftyfarrell6-Jan-09 14:13
Leftyfarrell6-Jan-09 14:13 
AnswerRe: Architecture that supports Unit Testing - Is there such a thing as Too Many Interfaces? Pin
Jon Rista6-Jan-09 17:46
Jon Rista6-Jan-09 17:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.