コード例 #1
0
ファイル: Generics.cs プロジェクト: corso-enaip-2019/prog2
        static void Method(string[] args)
        {
            // Non posso istanziare un tipo con generics se:
            // 1) il tipo T non è specificato sull'oggetto e
            // 2) il tipo T non è specificato nel metodo in cui si è o nella classe che contiene il metodo.
            // Questa riga dà errore:
            //Person<T> agnostic = new Person<T>();

            Person <Cat> catLover1 = new Person <Cat>();

            catLover1.Pets.Add(new Cat()
            {
                Nickname = "Fuffi"
            });

            CatLover catLover2 = new CatLover();

            catLover2.Pets.Add(new Cat()
            {
                Nickname = "Socks"
            });

            Person <Dog> dogLover1 = CreateLover <Dog>();

            dogLover1.Pets.Add(new Dog {
                Nickname = "Whiskey"
            });
        }
コード例 #2
0
        public void MainTest()
        {
            using (var session = Domain.OpenSession())
                using (var t = session.OpenTransaction()) {
                    Animal animal = new Animal();
                    animal.Name = "Elephant";
                    Dog dog = new Dog();
                    dog.Name = "Rex";
                    Cat cat = new Cat();
                    cat.Name = "Pussycat";
                    var catLover = new CatLover {
                        Name = "CatLover", Favorite = cat
                    };
                    catLover.Pets.Add(cat);
                    var dogLover = new DogLover {
                        Name = "CatLover", Favorite = dog
                    };
                    dogLover.Pets.Add(dog);
                    var dogClinic = new DogClinic {
                        Name = "Dog healer.", Address = "Address 1"
                    };
                    var catClinic = new CatClinic {
                        Name = "Cat healer.", Address = "Address 2"
                    };
                    t.Complete();
                }
            using (var session = Domain.OpenSession())
                using (var t = session.OpenTransaction()) {
                    var animals    = session.Query.All <Animal>().ToList();
                    var dogs       = session.Query.All <Dog>().ToList();
                    var cats       = session.Query.All <Cat>().ToList();
                    var persons    = session.Query.All <Person>().ToList();
                    var dogLovers  = session.Query.All <DogLover>().ToList();
                    var catLovers  = session.Query.All <CatLover>().ToList();
                    var clinics    = session.Query.All <VetClinic>().ToList();
                    var dogClinics = session.Query.All <DogClinic>().ToList();
                    var catClinics = session.Query.All <CatClinic>().ToList();

                    Assert.AreEqual(3, animals.Count);
                    Assert.AreEqual(1, dogs.Count);
                    Assert.AreEqual(1, cats.Count);

                    Assert.AreEqual(2, persons.Count);
                    Assert.AreEqual(1, dogLovers.Count);
                    Assert.AreEqual(1, catLovers.Count);

                    Assert.AreEqual(2, clinics.Count);
                    Assert.AreEqual(1, dogClinics.Count);
                    Assert.AreEqual(1, catClinics.Count);
                    t.Complete();
                }
        }