2
Answers

How to create MVC application using WCF?

S P

S P

8y
357
1
I am new to MVC and WCF as well, I want to know the step by step creation of MVC application using wcf. Moreover, I want to use LINQ for that.
 
Thanks in advance. 
Answers (2)
1
Jaish Mathews

Jaish Mathews

NA 7.3k 1.2m 14y

Yes. So you made that "Properties" for  Sharp_GP2D12 and IR always Ref. to "Properties" of  RangeSensor . So you are handling common entity in alll case and value not losing. Please close this thread, if it's fine.
Accepted
0
Pi Robot

Pi Robot

NA 7 0 14y
Thanks again!  Your reply gave me an idea that now works perfectly.  It turns out that if I leave out the "_Properties = new PropertClass()" statement from the constructors in the abstract IR class and the Sharp_GP2D12 class, then everything works great--and I don't have to specify which kind of Range Sensor (IR, Sonar, Sharp, etc) to get back the MinRange and MaxRange values.  In other words, I can now do this:

this.Properties.MySensors.Add(new Sharp_GP2D12("My Sharp IR Sensor");

foreach (RangeSensor sensor in this.Properties.MySensors)
{
    Console.WriteLine(sensor.Properties.MaxRange.ToString());
}

And I get the correct value without having to specify that it is a Sharp_GP2D12 sensor in the foreach loop.

So the final RangeSensor, IR and Sharp_GP2D12 classes look like this:

public abstract class RangeSensor : Sensor
    {
        private PropertyClass _Properties;

        public new class PropertyClass : Sensor.PropertyClass
        {
            public int MinRange;
            public int MaxRange;
            public int FieldOfView;
        }

        public new PropertyClass Properties
        {
            get { return _Properties; }
            set { _Properties = value; }
        }

        public RangeSensor(string Name)
            : base(Name)
        {
            _Properties = new PropertyClass();         
        }

public abstract class IR : RangeSensor
    {
        public new class PropertyClass : RangeSensor.PropertyClass
        {
        }

        public IR(string Name) : base(Name)
        {
            this.Properties.Type = Sensor.SensorType.IR;   
        }
    }

public class Sharp_GP2D12 : IR
    {
        public new class PropertyClass : IR.PropertyClass
        {
        }

        public Sharp_GP2D12(string Name) : base(Name)
        {
            this.Properties.Model = SensorModel.Sharp_GP2D12;
            this.Properties.MinRange = 4;
            this.Properties.MaxRange = 31;
            this.Properties.FieldOfView = 2;
        }
    }


0
Jaish Mathews

Jaish Mathews

NA 7.3k 1.2m 14y

Hi,
ONly need to adjust the foreach like below. No casting needed.
 foreach (Sharp_GP2D12 sensor in MySensors)
            {
              
                   Console.WriteLine(sensor.Properties.MaxRange.ToString());
               
            }
NB:Look at the bolded red part. Based on storing type in side List<T>, you need to change there too. That's it.
0
Pi Robot

Pi Robot

NA 7 0 14y
Hey thanks!  This is really close to what I need.  I have added one modification which introduces a catch.  Just to back up for a moment, what I am trying to do is produce a hierarchy of sensor property classes from the more general to the more specific like this:

Node->Sensor->RangeSensor->IR->Sharp_GP2D12

I'll list the Node and Sensor classes at the end of this message, but the new RangeSensor class looks like this:

public abstract class RangeSensor : Sensor
    {
        private PropertyClass _Properties;

        public new class PropertyClass : Sensor.PropertyClass
        {
            public int MinRange;
            public int MaxRange;
            public int FieldOfView;
        }

        public new PropertyClass Properties
        {
            get { return _Properties; }
            set { _Properties = value; }
        }

        public RangeSensor(string Name)
            : base(Name)
        {
            _Properties = new PropertyClass();         
        }

And so my IR sensor class now looks like this:

public abstract class IR : RangeSensor
    {
        private PropertyClass _Properties;

        public new class PropertyClass : RangeSensor.PropertyClass
        {
        }

        public new PropertyClass Properties
        {
            get { return _Properties; }
            set { _Properties = value; }
        }

        public IR(string Name) : base(Name)
        {
            _Properties = new PropertyClass();
            Properties.Type = Sensor.SensorType.IR;   
        }

And my class for the Sharp IR sensor is:

public class Sharp_GP2D12 : IR
    {
        private PropertyClass _Properties;

        public new class PropertyClass : IR.PropertyClass
        {
        }

        public new PropertyClass Properties
        {
            get { return _Properties; }
            set { _Properties = value; }
        }

        public Sharp_GP2D12(string Name) : base(Name)
        {
            _Properties = new PropertyClass();

            this.Properties.Model = SensorModel.Sharp_GP2D12;
            this.Properties.MinRange = 4;
            this.Properties.MaxRange = 31;
            this.Properties.FieldOfView = 2;
        }

So now, using your idea, I want to be able to do something like this:

List<Sensor> MySensors = new List<Sensor>();
MySensors.Add(new Sharp_GP2D12("My Sharp IR Sensor");

foreach (Sensor sensor in this.Properties.MySensors)
{
    if (sensor is RangeSensor)
   {
       Console.WriteLine(((RangeSensor)sensor).Properties.MaxRange.ToString());
    }
}

However, when I do this, I get back 0 instead of 31.  It seems that the value is coming from the default value of the RangeSensor class rather than being "propagated down" to the Sharp_GP2D12 class.  Is there a way to make this happen without having to specifically down-cast to the Sharp_GP2D12 class?

Here now are the Node and Sensor classes:

public abstract class Node
    {
        private string _name;
        private PropertyClass _Properties;

        public Node(string Name)
        {
            _name = Name;
        }

        public Node()
        {
        }

        public class PropertyClass
        {
            public Message.Status Status;
        }

        public PropertyClass Properties
        {
            get { return _Properties; }
            set { _Properties = value; }
        }

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }
    }

public abstract class Sensor : Node
    {
        private PropertyClass _Properties;

        public new class PropertyClass : Node.PropertyClass
        {
            public int Value;
            public int UpdateInterval;
            public bool Active;
            public SensorType Type;
            public SensorModel Model;
        }

        public new PropertyClass Properties
        {
            get { return _Properties; }
            set { _Properties = value; }
        }

        public enum SensorType
        {
            IR,
            Sonar,
            Laser,
            Touch,
            Light,
            PIR,
            Temperature
        }

        public enum SensorModel
        {
            Sharp_GP2D12,
            Rovio
        }

        public Sensor(string Name) : base(Name)
        {
            _Properties = new PropertyClass();
        }
    }
0
Jaish Mathews

Jaish Mathews

NA 7.3k 1.2m 14y

Hi,
I got access to you properties using below foreach
foreach (IR irObj in mySensors)
{
Console.WriteLine( irObj.Properties.MaxRange.ToString());
}
NB:But You never given your "Node" class definition. So doubting about the same solution in your code.
0
Pi Robot

Pi Robot

NA 7 0 14y
Thanks for your reply.  However, the idea is that I do not want to put the MinRange and MaxRange properties in the parent class since not all sensor types have these properties (e.g. Light sensor).  Instead,  I want them only in the IR child class where they are applicable.  (I should have made clear that MinRange and MaxRange refer to distances which are only applicable to range sensors like infrared and sonar.)  I found in another post that I can down-cast a child list member to get the range properties:

(Sensors.IR)this.Properties.MySensors[0]).Properties.MinRange

However, this requires that I have the child's class type hard coded on the line above which I'd rather not do.



0
Amit Choudhary

Amit Choudhary

NA 27.7k 3m 14y
hi friend,

make your MinRange and MaxRange property of sensor class protected or public..

and Implement the indexer inside your Sensor class to access the properties and method using Collection index instance e.g., MySeson[0].MinRange

Please mark as "Do you like this post" if it helps you.