Adding my own factory methods

Adding my own factory methods

Old forum URL: forums.lhotka.net/forums/t/275.aspx


Michael Hildner posted on Monday, June 05, 2006

Hi,

Newbie question here. Is there a best practice to add my own factory method?

I have an employee table in the DB, which has a primary key. I used CodeSmith to generate the business object. So now I have a Employee.GetEmployee(guid pk) method.

What I'd like to do is add a method something like GetEmployeeByUserNamePassword(string userName, string password). As I'm new to this, I'm a little lost on the proper way to implement.

Any thoughts appreciated. Thanks,

Mike

ajj3085 replied on Monday, June 05, 2006

Sure, you can do that.. although if you're in C# you may just want to call it GetEmployee( string, string ).

Andy

Michael Hildner replied on Monday, June 05, 2006

Hello Andy,

Thanks for the reply. I guess what I'm confused about is *how* do I go about implementing a new method?

Right now - and I used CodeSmith to generate the code - I have a method that looks like this:

public static Employee GetEmployee(Guid pk)

{

if (!CanGetObject())

throw new System.Security.SecurityException("User not authorized to view a Employee");

return DataPortal.Fetch<Employee>(new Criteria(pk));

}

This calls DataPortal.Fetch, which then in turn (I'm assuming right now - not an expert at CSLA yet) calls

private void DataPortal_Fetch(Criteria criteria)

I don't think I should be messing with the DataPortal class. Should I create another DataPortal_Fetch method? If I do, I'm going to need something besides my generated Criteria class, because that has a single member and one constructor:

private class Criteria

{

public Guid PK;

public Criteria(Guid pk)

{

this.PK = pk;

}

}

Hope that makes sense. Since I'd like to pass in two strings and not a Guid to get the Employee object, I guess I could add some members to Criteria, then check to see if those members are not null in the DataPortal_Fetch method. I'm not sure if this is the proper way to go about it.

Thaks,

Mike 

ajj3085 replied on Monday, June 05, 2006

Create a UserNamePasswordCriteria class which lives in your Employee class.  Create another DataPortal_Fetch method which takes the new criteria class.  The static method would be similar tot he one you have now, except it takes two strings and creates the new criteria class instead.

HTH
Andy

Michael Hildner replied on Monday, June 05, 2006

Thank you Andy, makes sense to me.

Regards,

Mike

Copyright (c) Marimer LLC