3 Replies - 343 Views - Last Post: 08 March 2013 - 06:09 AM Rate Topic: -----

#1 ray1234  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 148
  • Joined: 07-September 12

all functionality of my type when passed to interface type parameter

Posted 07 March 2013 - 03:41 AM


 public interface IReservationDTO
    {
        public int Video_ID { get; set; }
        public string Video_Name { get; set; }
        public int Member_ID { get; set; }
        public byte Place_in_the_Queue { get; private set; }
        
    }


 public class ReservationRepository<T> : iRepository<T> where T : IReservationDto
    {
        private T _var;
        
        public ReservationRepository(T _instance)
        {
            this._var = _instance;
        }
        public void Add()
        {
            var obj = new ReservationContext();
            Reservation res = Mappings.ReservationDTOMapping(_var);
            obj.Reservations.Add(res);
            obj.SaveChanges();
        }
     }



My aim

in this generic class for its generic type it accepts only a type that implements the IReservationDTO interface. thus not alowing any type to be put in. The issue I have is, that say in the future someone creates a new class that implements this interface but also they want to add two more properties, such as:

public int salary {get; set;}
public string Address {get; set;}

if i now pass in this new type to my generic class

newclassDTO obj = new newclassDTO();
ReservationRepository<newclassDTO> a = new ReservationRepository<newclassDTO>(obj);
a._reservation.



when I call a.reservation. // the new properties ain't listed in the intellisense. I know why this is. but i want a way around this, restructuring my classes to have this possibility.

I want a.reservation to have api that includes the additional functions of the object I passed through to the reservationrepository constructer(obj) of salary and address. At the moment when i do [a.reservation.] the intellisense only list IreservationDTO properties and not the additional properties of the newclassDTO. ( video i.d, videoname, memeberid, place_in_the_queue) only.

I would like my generic method so that what when ever object that is passsed to the reservationrepository is of a type that implements IreservationnDTO, then that object full api is available.

how would i structure my classes or how to solve this issue


regards

ray

Is This A Good Question/Topic? 0
  • +

Replies To: all functionality of my type when passed to interface type parameter

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3534
  • View blog
  • Posts: 10,941
  • Joined: 05-May 12

Re: all functionality of my type when passed to interface type parameter

Posted 07 March 2013 - 06:48 AM

Seems to work just fine for me. Magic is available through Intellisense and the compiler:
using System;
using System.Diagnostics;
using System.Data;
using System.Text;
using System.Net;

namespace SimpleCsConsole2012
{

    interface IFoo
    {
        void DoFoo();
    }

    class Foo : IFoo
    {
        public int Magic { get; set; }

        public void DoFoo()
        {
        }
    }

    class Bar<T>
    {
        public T Obj { get; set; }

        public Bar(T obj)
        {
            Obj = obj;
        }
    }

    class Baz<T> : Bar<T> where T : IFoo
    {
        public Baz(T obj)
            : base(obj)
        {
        }
    }

    public class Program
    {
        static void Main()
        {
            Foo foo = new Foo();
            Bar<Foo> bar = new Bar<Foo>(foo);
            bar.Obj.Magic = 12;

            Baz<Foo> baz = new Baz<Foo>(foo);
            baz.Obj.Magic = 28;
        }
    }
}



Since you didn't show us how _reservation is declared, I am making the assumption that you are declaring it as an IReservationDTO instead of a T like I did above.
Was This Post Helpful? 0
  • +
  • -

#3 ray1234  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 148
  • Joined: 07-September 12

Re: all functionality of my type when passed to interface type parameter

Posted 08 March 2013 - 05:56 AM

Thanks for that, my issue is now solved. :rolleyes:

Thanks for that, my issue is now solved. :rolleyes:
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3534
  • View blog
  • Posts: 10,941
  • Joined: 05-May 12

Re: all functionality of my type when passed to interface type parameter

Posted 08 March 2013 - 06:09 AM

I'm glad that worked.

I'm a little concerned about your code above, though. If you are following the repository pattern, you should never need to expose a single record through your repository instance. If you are, then you are, then you are using the cursor or current record pattern rather than the repository pattern.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1