I've got the need for a class that identifies what type of network join to be performed.
The network join can either be a Domain
join a Workgroup
join.
In the case of a Workgroup
join I only need to know the Name
of the workgroup to join.
In the case of a Domain
join I need to know the Name
of the domain to join as well as the Username
and Password
to use (let's ignore all concerns about security here, this is a scenario).
I then want to create a WPF UI for it similar to this:
http://documents.weber.edu/ctctools/sccm/images/osdnotes/9.png
Where the credentials portion of the GUI becomes disabled if the user picks a Workgroup
join and enabled when they pick a Domain
join (with the same being said regarding the name of the actual Workgroup/Domain to join).
And I want to be able to serialize/deserialize this data (again ignore security concerns, this is a scenario).
The way I see it I have two options:
Option 1
Create a solution similar to:
enum JoinType
{
Domain,
Workgroup
}
class NetworkJoin
{
JoinType JoinType {get; set;}
string Name {get;set;}
string Username {get;set;}
SecureString Password {get;set;}
void Join()
{
// Join code for domain + workgroup
}
}
This would allow me to easily do TextBoxUsername.IsEnabled = ViewModel.NetworkJoin.JoinType == JoinType.Domain
.
However, because the class instance is serialized/deserialized it allows for an instance of this class to have JoinType = JoinType.Workgroup
as well as having a Username
/Password
and it's an assumption (although a logical one) that what network join to do is based off of a check on the JoinType
(rather than say, if (Username == null) { // workgroup join }
)
Which brings me to option 2
Option 2
Something similar to:
interface INetworkJoin
{
string Name {get;set;}
void Join();
}
class DomainJoin : INetworkJoin
{
string Name {get;set;}
string Username {get;set;}
SecureString {get;set;}
void Join()
{
// Domain join code
}
}
class WorkgroupJoin : INetworkJoin
{
string Name {get;set;}
void Join()
{
// Workgroup join code
}
}
Now it's impossible for you to create an object with the wrong properties or any assumption about what type of join will be performed because ambiguous parameters we passed.
In fact, normally this would be a much better solution. Except for the binding it to the UI.
My ViewModel would basically have a INetworkJoin NetworkJoin, which means my View would only see a INetworkJoin. It needs to know its concrete type in order to determine whether to show/not show the credentials objects, it needs to bind the Username TextBox to the Username property (which INetworkJoin doesn't have...), same for Password, etc etc.
Summary
If feels like neither of the solutions is really appropriate. The first provides an ambiguous join and the second would require finding out the concrete type of an interface, as well as accessing properties only available in the concrete type.
I imagine there has to be a better solution than either of these, but this is really all I can think of.
Any help would be appreciated.