C# Design Questions

Posted by guazz on Stack Overflow See other posts from Stack Overflow or by guazz
Published on 2010-04-28T23:17:02Z Indexed on 2010/04/28 23:27 UTC
Read the original article Hit count: 560

Filed under:
|
|

How to approach unit testing of private methods?

I have a class that loads Employee data into a database. Here is a sample:

>

public class EmployeeFacade   
{
    public Employees EmployeeRepository = new Employees();  
    public TaxDatas TaxRepository = new TaxDatas();  
    public Accounts AccountRepository = new Accounts();  
    //and so on for about 20 more repositories etc.  

    public bool LoadAllEmployeeData(Employee employee)  
    {   
        if (employee == null)
            throw new Exception("...");

        EmployeeRepository emps = new EmployeeRepository();
        bool exists = emps.FetchExisting(emps.Id);
        if (!exists)
        {   
            emps.AddNew(); 
        }

        try
        {
            emps.Id = employee.Id;
            emps.Name = employee.EmployeeDetails.PersonalDetails.Active.Names.FirstName;
            emps.SomeOtherAttribute;
        }
        catch() {}

        try
        {
            emps.Save();
        }
        catch(){}   


        try
        {
            LoadorUpdateTaxData(employee.TaxData);
        }
        catch() {}

        try
        {
        LoadorUpdateAccountData(employee.AccountData);
        }
        catch() {}
        ... etc. for about 20 more other employee objects

    }  

    private bool LoadorUpdateTaxData(employeeId, TaxData taxData)
    {
        if (taxData == null)
           throw new Exception("...");

        ...same format as above but using AccountRepository 

    }

    private bool LoadorUpdateAccountData(employee.TaxData)
    {   
        ...same format as above but using TaxRepository 
    }
}

I am writing an application to take serialised objects(e.g. Employee above) and load the data to the database.

I have a few design question that I would like opinions on:

A - I am calling this class "EmployeeFacade" because I am (attempting?) to use the facade pattern. Is it good practace to name the pattern on the class name?

B - Is it good to call the concrete entities of my DAL layer classes "Repositories" e.g. "EmployeeRepository" ?

C - Is using the repositories in this way sensible or should I create a method on the repository itself to take, say, the Employee and then load the data from there e.g. EmployeeRepository.LoadAllEmployeeData(Employee employee)? I am aim for cohesive class and but this will requrie the repository to have knowledge of the Employee object which may not be good?

D - Is there any nice way around of not having to check if an object is null at the begining of each method?

E - I have a EmployeeRepository, TaxRepository, AccountRepository declared as public for unit testing purpose. These are really private enities but I need to be able to substitute these with stubs so that the won't write to my database(I overload the save() method to do nothing). Is there anyway around this or do I have to expose them?

F - How can I test the private methods - or is this done (something tells me it's not)?

G- "emps.Name = employee.EmployeeDetails.PersonalDetails.Active.Names.FirstName;" this breaks the Law of Demeter but how do I adjust my objects to abide by the law?

© Stack Overflow or respective owner

Related posts about c#

Related posts about design