I could have the da method return a new customer object, but I cannot
assign the response of that method to this if trying to set the
customer object in a constructor.

this = da.Load(this, customerID); //not allowed

In that case, I would be doing something like:
Customer c = CustomerFactory.Get(123)
instead of
Customer c = new Customer(123)

Either way work fine but I feel like the latter is a more natural way
of initializing a customer.  Perhaps there shouldn't be db code in the
constructor, but I think it's a nice option.

I have used this approach, however, when lazy loading up child
properties on an object.  Ex:

class School {
...
private List<Person> students = null;
public List<Person> Students
{
   get{
     if (students == null){ students =
schoolDataAccess.GetStudents(schoolId); }
     return students;
   }
}

The difference is obviously that I can assign a value to students.


On Feb 24, 12:15 pm, Patrick Steele <[email protected]> wrote:
> You could just stub out the method to return a new customer object:
>
> var da = MockRepository.GenerateStub<ICustomeDataAccess>();
> da.Stub(d => d.Load(null, 0)).IgnoreArguments().Return(new Customer {
> Id = 123, Balance = 22.0 });
>
> Is that what you want to do?
>
> ---
> Patrick Steelehttp://weblogs.asp.net/psteele
>
>
>
>
>
>
>
> On Thu, Feb 24, 2011 at 1:59 PM, Brett <[email protected]> wrote:
> > I should have been more specific in my example.  The Load actually
> > hydrates the customer, not a theme.  Copy/paste error.
>
> > public Customer(int customerID) { ...
> >  ...validation...
> > customerDataAccess.Load(this, customerID);
> > }
>
> > so then, after something like:
>
> > Customer c = new Customer(123);
>
> > I could then access c.BillingAddress, c.BalanceDue, whatever else,
> > etc...
>
> > On Feb 24, 11:21 am, Brett <[email protected]> wrote:
> >> I have a scenario that I want to mock and unit test, but I'm not sure
> >> of the best approach.  I have a class with a constructor that takes an
> >> ID, which then does some validation and hydrates itself from a
> >> mockable data access class.  Customer would be an example class name:
>
> >> public Customer(int customerID) { ...
> >>  ...validation...
> >> customerDataAccess.LoadTheme(this, customerID);
>
> >> }
>
> >> so that you could use this class like:
>
> >> Customer c = new Customer(123);
>
> >> The problem is with passing "this" to the mocked method.  At that
> >> point, the mock of ICustomerDataAccess does not update the current
> >> instance.  Under the hood, CustomerDataAccess uses a mockable
> >> IDataAccess, so I could use an actual CustomerDataAccess instance and
> >> instead mock IDataAccess.  That's fine, but I was wondering if anybody
> >> has worked with a scenario like this in the past.  I think I could use
> >> WhenCalled and set properties of the instance that is created, but I'm
> >> not sure that is really what I want to do.
>
> >> Thanks.
>
> > --
> > You received this message because you are subscribed to the Google Groups 
> > "Rhino.Mocks" group.
> > To post to this group, send email to [email protected].
> > To unsubscribe from this group, send email to 
> > [email protected].
> > For more options, visit this group 
> > athttp://groups.google.com/group/rhinomocks?hl=en.

-- 
You received this message because you are subscribed to the Google Groups 
"Rhino.Mocks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rhinomocks?hl=en.

Reply via email to