3 Replies - 2162 Views - Last Post: 12 June 2013 - 09:42 AM Rate Topic: -----

#1 ray1234  Icon User is offline

  • D.I.C Head

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

how to register generic interface using unity?

Posted 10 June 2013 - 11:09 AM

I'm trying to register List<iaccount> List property but unable to do so using Unity. I can do so using injectionParameter but I want the creation of the object in the myclass1<t>

  public class MyClass1<T> : IMyclass<T> where T : iaccount
    {
        public List<iaccount> List { get; set; }  // I want to register this type

        public T InjectedValue { get; set; }
        public MyClass1(string s, object o)
        {
        }

        public MyClass1(T injectedValue)
        {
            InjectedValue = injectedValue;
            //List.Add(new Account());
  
        }
    }



my attempt so far
 var container = new UnityContainer();
         

            container.RegisterType(typeof(MyClass1<>),
                new InjectionConstructor(new GenericParameter("T")));

           Account a = new Account();
           container.RegisterInstance<Account>(a);

                     
            container.RegisterType<List<iaccount>>(new InjectionFactory(c => c.Resolve<List<Account>>()));
            container.RegisterType<iaccount, Account>();

           




?

This post has been edited by ray1234: 10 June 2013 - 11:11 AM


Is This A Good Question/Topic? 0
  • +

Replies To: how to register generic interface using unity?

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3481
  • View blog
  • Posts: 10,733
  • Joined: 05-May 12

Re: how to register generic interface using unity?

Posted 10 June 2013 - 10:09 PM

Aren't you defeating the whole purpose of Inversion of Control if you want the creation of the object within your class. The idea is that you instead of creating objects within your class, you would be passed in instances of the objects.

Anyway, if you really want to create instances of a type of object, why not take as one of the constructor parameters a function. Something like:

class CrapsTable
{
    Func<Dice> _createDice;
    List<Dice> _diceList;

    public CrapsTable(Func<Dice> createDice)
    {
        _createDice = createDice;
    }

    void GetFreshDice()
    {
        _diceList.Clear();
        for(int i = 0; i < 2; i++)
            _diceList.Add(_createDice());
    }
}


Was This Post Helpful? 0
  • +
  • -

#3 ray1234  Icon User is offline

  • D.I.C Head

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

Re: how to register generic interface using unity?

Posted 12 June 2013 - 09:27 AM

The issue I have is having to explicitly creating and passing objects to the constructor to initialize properties. In Windsor, you just register a type, container.register<mytype>(); and Windsor will automatically initilize every dependency within your class.

but with unity i'm still finding it difficult.

so the list propertyin this class i'm trying to register this type, so that it resolves this automatically upon object creation.

 public class messageservice<t> : imessageservice<t> where t : isendmessage, new()
    {
        IList<isendmessage> list { get; set; }

        imessageservice<isendmessage> messagetype { get; set; }
        public t getsendmessage()
        {
            return new t();
        }
    }



my current attempt.


 container.RegisterType<IList<isendmessage>>(new InjectionFactory(c => c.Resolve<List<emailmessage>>()));




the list contains null after resolve.
Was This Post Helpful? 0
  • +
  • -

#4 ray1234  Icon User is offline

  • D.I.C Head

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

Re: how to register generic interface using unity?

Posted 12 June 2013 - 09:42 AM

As I said earlier I can solve issue through the use of injecting objects in the constructor


static void Main(string[] args)
        {
            var newlist = new List<ivideodto>();
            var vid_repo = new videorepository(newlist);
            var repositoryinstance = new RepositoryContainer<videorepository>(vid_repo);
            

            var container = new UnityContainer();

            // register videologic
            container.RegisterType<fakevideologic>(new InjectionConstructor(new InjectionParameter<RepositoryContainer<videorepository>>(repositoryinstance)));
            container.RegisterInstance<RepositoryContainer<videorepository>>(repositoryinstance);

            //RegisterEventArgs repositorycontainer
            container.RegisterType(typeof(RepositoryContainer<>), new InjectionConstructor(new GenericParameter("repositorytype")));
            container.RegisterInstance<videorepository>(vid_repo);

            //register ilist<ivideodto>
            container.RegisterType<videorepository>(new InjectionConstructor(new InjectionParameter<List<ivideodto>>(newlist)));
            container.RegisterInstance<List<ivideodto>>(newlist);

            //resolve
            var ab = container.Resolve<fakevideologic>();
            var list = ab._repositoryContainer._repository.List;

            Console.WriteLine(list.ToString());

            Console.ReadKey();
        }



but I had to explicitly create objects and constructors
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1