Fixing an issue with output parameters that are of type IntPtr
[castle.git] / Samples / Castle / PetStore.Service / DefaultRecommendationService.cs
blob3dd248837d40e8990f10627f139d57f2d4c53159
1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace PetStore.Service
17 using System;
19 using PetStore.Model;
21 /// <summary>
22 /// A clever recommendation service should use some
23 /// branch of AI to suggest meaningfull products
24 /// (think adaptive resonance theory / http://jroller.com/page/hammett?entry=finally_something_interesting_on_my)
25 /// based on the history of the customer (if available).
26 /// But we just pick up some random products
27 /// </summary>
28 public class DefaultRecommendationService : IRecommendationService
30 private readonly Random random;
31 private readonly IProductDataAccess productDataAccess;
33 public DefaultRecommendationService(IProductDataAccess productDataAccess)
35 this.productDataAccess = productDataAccess;
36 this.random = new Random((int) DateTime.Now.Ticks);
39 public Product[] GetProducts(Customer customer)
41 int[] ids = productDataAccess.GetProductIds();
43 if (ids.Length == 0) return new Product[0];
45 // We select two
47 int firstIndex = random.Next(0, ids.Length - 1);
48 int secondIndex = random.Next(0, ids.Length - 1);
50 if (firstIndex != secondIndex)
52 return new Product[] {
53 Product.Find(ids[firstIndex]),
54 Product.Find(ids[secondIndex]) };
56 else
58 return new Product[] { Product.Find(ids[firstIndex]) };