Hi,
I am very week in design and program to interface. I have a task to design few classes. I have some simple interfaces
like below. Sample interfaces.
public interface IParameterValue
{
string ParameterName { get; set; }
string ParameterValu { get; set; }
}
the above interface is a kind of common interface.
The below interfaces should actually implement the above interface.
Area Parameter interface:
public interface IAreaParameter
{
string AreaId { get; set; }
}
public class AreaParameter : IAreaParameter
{
public string AreaId { get; set; }
}
Plot paramtere interface:
public interface IPlotParameter
{
string AreaId { get; set; }
string PartId { get; set; }
}
public class PlotParameter : IPlotParameter
{
public string AreaId { get; set; }
public string PartId { get; set; }
}
Now the common interface IParameterValue has ParameterName and ParameterValu.
There will be one more incoming value scope. depending on the scope
the paramterValu will be applied Area wise or Plot wise.
Now, I think we have to implement IParameterValue interface to IAreaParameter and IPlotParamter.
My question is, how to implement in code and if we implement how to access all this paramters
from other class and assign a value to them.
Simple question i think but did'n work with interfaces more so...
Thanks.
VulpesPosted Feb 21, 2013, 7:29 PM
Sumitra PaulPosted Feb 21, 2013, 7:37 PM
There is again some problem with this design as I thought the requirement in a wrong way.
Actually, I was considering IParameterValue as top in my inheritence hirarchy.
Like I was thinking there will be a parameter value and I have to look into which AreaId and PartId it belongs to.
But actually, It seems I have to first consider the AreaId or PartId and assign all the parameters belonging to each AreaId or PartId i.e a Area can contain multiple parameterValues and if Area does not contain Parameters then
it might contain multiple Plots/Part which will contain multiple parameters.
I have added one more interface which "IGroupOfAllArea". This can also contain parameterValues. The thing is that if "IGroupOfAllArea" has Parameter then I will add the IParameterValue with a scope as "AllArea" and
if IArea has parameter then I will add the IParameterValue with a scope as "Area" and
if IPlot has parameter then I will add the IParameterValue with a scope as "Plot".
Let me explain it in other way.
Sample interfaces without any design.
public interface IParameterValue
{
string ParameterName { get; set; }
string ParameterValu { get; set; }
}
public interface IGroupOfAllArea
{
IEnumerable
IEnumerable
}
public interface IArea
{
string AreaId { get; set; }
IEnumerable
IEnumerable
}
public interface IPlot
{
string PlotId { get; set; }
IEnumerable
}
public interface IUser
{
// Earlier I was returning IParameterValue, But now not sure what should I return
// to the user which will make easy to manupulate my return collection.
//IEnumerable
}
Now, while assigning the values to my return IEnumerable collection I will be
checking following things:
1.I will be actually checking first the "IGroupOfAllArea" and if "ParameterValues" contains
values, I will add scope as "AllArea" and add the parameter to my list.
2. then I will iterate through "Areas" collection of "IGroupOfAllArea" and get all the "ParameterValues" for each "AreaId" respectively and add to my list with Scope as "Area".
SO here for each AreaId, I might have multiple parameterValues.
Now the same way
3. I will iterate through "Plots" collection and get all the "parameterValues" for each Plot respectively and add it to my collection with scope as "Plot".
for the above requirement, the interface hyrarchy that we had earlier is not going to work i.e
we were implementing IParameter value to IAreaParameter and IPlotParameter.
Note: All this values i,e Parameter values, Area Id's and parameters coresponsding to this Ids etc will be available in a in coming collection. I just need to design my interfaces and assign the values. We will be manupulating our collection and sending it back again using a kind of adaptor pattern.
I could have written some sample code, but as I am not atall sure about the interface design only, could'n write that. Also, now I am not sure like whether I should return IParameterValue list to the user or I have to retrun I list of IGroupOfAllArea? Because the concern is that the we should not have much dificulty menupulating the collection later.
If posible can you tell me how I can design this ?
Sorry for unmarking the answer.
Thanks a lot.
Sumitra PaulPosted Feb 20, 2013, 2:34 PM
I will implement this and see how it goes.
Thanks again.
VulpesPosted Feb 20, 2013, 1:45 PM
You could in fact get rid of the IParameterValue interface altogether (unless you might want to implement it via some other base class) and just use the abstract class in the design but, as you haven't asked for that, I've left it as it is for now:
The output, as expected, is the same as before.
Sumitra PaulPosted Feb 20, 2013, 1:27 PM
Yah, second part of my question..i do belive I can make abstract class say AbstractParameterValue by implementing IParameterValue interface. But not sure how this class will fit in my design?? and how to use it ??
Thanks for replying again... :-)
VulpesPosted Feb 20, 2013, 1:16 PM
The output was as follows from which it can be seen that the design is working fine:
It's slightly awkward having to cast to IAreaParameter or IPlotParameter to get at the properties which they provide but this can't be helped as IParameterValue doesn't provide these properties.
The only improvement I'd suggest is to pass the IUser object as a second parameter to the AssignValues method, rather than reading it in from a field of the containing class, as this will make the method self-contained and make it easier to pass different IUser objects to it.
I'll reply to the second point in a separate post after I've figured out how to do it.
Sumitra PaulPosted Feb 20, 2013, 12:15 PM
I think i can create an abstract class something like "AbstractParamterValue" and implement IParameterValue as this interface properties are common for
interface IAreaParameter and IPlotParameter.
But how to do that ??
Can you put some light on the design ??
Thanks again..
Sumitra PaulPosted Feb 20, 2013, 9:00 AM
Thanks a lot for the reply. I have done some changes to the class design.
I think with the below program you can actually understand what I am going to design.
Can you please tell me if the design is correct? or there can be some other best way to do the same thing.
I might have one more interface same as IAreaParameter.
Code sample:
// Sample class. this class is created just to create an object with
// these values. realtime, this values will be a collection coming from outside.
class Parameters
{
public string Scope { get; set; }
public string AreaId { get; set; }
public string PartId { get; set; }
public string ParameterName { get; set; }
public string ParameterValu { get; set; }
}
public interface IParameterValue
{
string ParameterName { get; set; }
string ParameterValu { get; set; }
}
public interface IAreaParameter
{
string AreaId { get; set; }
}
public interface IPlotParameter
{
string AreaId { get; set; }
string PartId { get; set; }
}
public class AreaParameter : IAreaParameter, IParameterValue
{
public string AreaId { get; set; }
public string ParameterName { get; set; }
public string ParameterValu { get; set; }
}
public class PlotParameter : IPlotParameter, IParameterValue
{
public string AreaId { get; set; }
public string PartId { get; set; }
public string ParameterName { get; set; }
public string ParameterValu { get; set; }
}
public interface IUser
{
string Scope{get;set;}
IEnumerable
}
public class User : IUser
{
public string Scope { get; set; }
public IEnumerable
}
class Program
{
static void Main(string[] args)
{
user = new User();
var parameters = GetParameters();
IEnumerable
Console.ReadKey();
}
private static IUser user;
private static IEnumerable
{
IList
foreach (Parameters p in perameters)
{
if (p.Scope == "Plot")
{
user.Scope = p.Scope;
IPlotParameter plotParameter = new PlotParameter();
plotParameter.AreaId = p.AreaId;
plotParameter.PartId = p.PartId;
IParameterValue parammeterValue = (IParameterValue)plotParameter;
parammeterValue.ParameterName = p.ParameterName;
parammeterValue.ParameterValu = p.ParameterValu;
parameterValueList.Add(parammeterValue);
}
if (p.Scope == "Area")
{
user.Scope = p.Scope;
IAreaParameter areaParam = new AreaParameter();
areaParam.AreaId = p.AreaId;
IParameterValue parammeterValue = (IParameterValue)areaParam;
parammeterValue.ParameterName = p.ParameterName;
parammeterValue.ParameterValu = p.ParameterValu;
parameterValueList.Add(parammeterValue);
}
}
return parameterValueList;
}
private static IEnumerable
{
// Sample values collection created.
// realtime, this values will be a collection coming from outside.
Parameters obj1 = new Parameters
{
Scope = "Plot",
AreaId = "A1",
PartId = "L1Value",
ParameterName = "ParameterA",
ParameterValu = "ParameterVal"
};
Parameters obj2 = new Parameters
{
Scope = "Area",
AreaId = "A2",
PartId = "A2Part",
ParameterName = "ParameterB",
ParameterValu = "ParameterValue"
};
Parameters obj3 = new Parameters
{
Scope = "Plot",
AreaId = "A3",
PartId = "A3Part",
ParameterName = "ParameterC",
ParameterValu = "ParameterValue"
};
IEnumerable
return param;
}
}
can you please tell me if the design is correct?
VulpesPosted Feb 20, 2013, 6:16 AM
Here's how you can assign values to the properties via interface references and also read them back. I've used object references for the latter because you can access all properties using the same reference. However, you can still use interface references for reading back except that each interface can only access its own properties.
The values I've assigned are purely for illustration and nonsense in themselves:
The output should be:
Sumitra PaulPosted Feb 20, 2013, 2:05 AM
Let me clarify my question in other way.
I know how to implement an interface.
My question is design related and also if we implement the interface like below, how to
access it and assign values to all the properties and also read them.
Say, Interface is implemented like this:
public interface IParameterValue
{
string ParameterName { get; set; }
string ParameterValu { get; set; }
}
public interface IAreaParameter
{
string AreaId { get; set; }
}
public interface IPlotParameter
{
string AreaId { get; set; }
string PartId { get; set; }
}
Implementaion:
public class AreaParameter : IAreaParameter, IParameterValue
{
public string AreaId { get; set; }
public string ParameterName { get; set; }
public string ParameterValu { get; set; }
}
public class PlotParameter : IPlotParameter, IParameterValue
{
public string AreaId { get; set; }
public string PartId { get; set; }
public string ParameterName { get; set; }
public string ParameterValu { get; set; }
}
Do I need to implement IParameterValue interface in a saparate class ParameterValue??
How to assign or access a value to these fields from other class say a class "USER"?
Actually, i don'nt know how to code this for assigning and retriving values ?
Thanks.
Sumitra PaulPosted Feb 20, 2013, 2:01 AM
Let me clarify my question in other way.
I know how to implement an interface.
My question is design related and also if we implement the interface like below, how to
access it and assign values to all the properties and also read them.
Say, Interface is implemented like this:
public interface IParameterValue
{
string ParameterName { get; set; }
string ParameterValu { get; set; }
}
public interface IAreaParameter
{
string AreaId { get; set; }
}
public interface IPlotParameter
{
string AreaId { get; set; }
string PartId { get; set; }
}
Implementaion:
public class AreaParameter : IAreaParameter, IParameterValue
{
public string AreaId { get; set; }
public string ParameterName { get; set; }
public string ParameterValu { get; set; }
}
public class PlotParameter : IPlotParameter, IParameterValue
{
public string AreaId { get; set; }
public string PartId { get; set; }
public string ParameterName { get; set; }
public string ParameterValu { get; set; }
}
Do I need to implement IParameterValue interface in a saparate class ParameterValue??
How to assign or access a value to these fields from other class say a class "USER"?
Actually, i don'nt know how to code this for assigning and retriving values ?
Thanks.