how would *you* do this?

K

Kevin Blount

I have several pages that will contain forms used for registering with
our website. These form won't always show the same information, as
their primary purposes are different, but part of the processing will
be the registration.

I'd like to have just one method for the entire site that handles the
creation of new account, referenced by all these forms.

So, assuming that form 'a' contains 8 fields required for registration,
form 'b' contains the min of 4, form 'c' contains 7 and form 'd' has
all 11, how would you create the method and then use it?

I'm new to C#, so bear with me. I've heard of overloading, but I think
I'd basically need 4 copies of the same method, which seems somewhat
over the top. I wondered about putting the form fields into an object
of some kind and then passing that object to the registration method.
Would that work?

What do you think would be the best way to get varying ammount of data
into a single method? Any examples and/or tutorial sites would be
greatly appreciated.

Thanks

Kevin
 
N

Nicholas Paldino [.NET/C# MVP]

Kevin,

I think that your second suggestion is the better one. Have a class
that exposes the fields from the form as properties, then pass that to your
registration method. I would also recommend implementing that method in a
library, then reference the library from the pages/project.

Hope this helps.
 
K

Kevin Blount

OK...

That part I wrote about being new though.. I don't get what you mean.
Any sample code or URL's you can offer? At the very least what keywords
should I Goggle for?

Thanks

Kevin
 
N

Nicholas Paldino [.NET/C# MVP]

Kevin,

It's simple. You have a class that acts as your parameter list, which
has the parameters from the form:

public class RegistrationParameters
{
private string username;

private string password;

public string UserName
{
get
{
return username;
}
set
{
username = value;
}
}

public string Password
{
get
{
return password;
}
set
{
password = value;
}
}

// And so on...
}

Then, you have a method on another class:

public void Register(RegistrationParameters parameters)
{
// Use the fields that are set on the parameters instance
// of RegistrationParameters.
}
 
K

Kevin Blount

Thanks for the sample code. I guess it is simple once you know how..
until then however...

;)
 
S

Saad Rehmani

This is how I would do this:

1. Create a base interface for all the information is required to create
a user. (IUserCreationRequest)

2. Create extension interfaces for other criteria that can be used to create
a user. (IAddress2Source, ITitleSource)

3. Create one method/service that takes in the base request and returns a
reference to the user.

4. Have every aspect create its own value object that implements at least
the base interface and also any other interfaces for data it can provide:

e.g:
public class CreateGuestUser : IUserCreationRequest ...
public class CreateInternalUser : IUserCreationRequest, IAddress2Source,
ITitleSource

5. Have the method/service create the basic user object and then populate
any extra data on it based on the interfaces the incoming request implements.

public IUser CreateUser(IUserCreationRequest userRequest)
{
IUser user = CreateUser(userRequest);

if (userRequest is IAddress2Source)
ApplyAddress2Data(user);

if (userRequest is ITitleSource)
ApplyTitleData(user);

...

return user;
}


The implementation for CreateUser can be made more dynamic but I think you
get the idea.

Good luck!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top