#region Apache Notice /***************************************************************************** * $Revision: 374175 $ * $LastChangedDate$ * $LastChangedBy$ * * iBATIS.NET Data Mapper * Copyright (C) 2006/2005 - The Apache Software Foundation * * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ********************************************************************************/ #endregion using System; using System.Collections; using System.Collections.Specialized; #if dotnet2 using System.Collections.Generic; #endif using IBatisNet.DataMapper.Exceptions; namespace IBatisNet.DataMapper.Proxy { /// /// Gets instance. /// public class LazyFactoryBuilder { private IDictionary _factory = new HybridDictionary(); /// /// Initializes a new instance of the class. /// public LazyFactoryBuilder() { _factory[typeof(IList)] = new LazyListFactory(); #if dotnet2 _factory[typeof(IList<>)] = new LazyListGenericFactory(); #endif } /// /// Register (add) a lazy load Proxy for a type and member type /// /// The target type which contains the member proxyfied /// The member name the proxy must emulate /// The . public void Register(Type type, string memberName, ILazyFactory factory) { // To use for further used, support for custom proxy } /// /// Get a ILazyLoadProxy for a type, member name /// /// The target type which contains the member proxyfied /// Return the ILazyLoadProxy instance public ILazyFactory GetLazyFactory(Type type) { if (type.IsInterface) { #if dotnet2 if (type.IsGenericType && (type.GetGenericTypeDefinition() == typeof(IList<>)) ) { return _factory[ type.GetGenericTypeDefinition() ] as ILazyFactory; } else #endif if (type == typeof(IList)) { return _factory[type] as ILazyFactory; } else { throw new DataMapperException("Cannot proxy others interfaces than IList or IList<>."); } } else { // if you want to proxy concrete classes, there are also two requirements: // the class can not be sealed and only virtual methods can be intercepted. // The reason is that DynamicProxy will create a subclass of your class overriding all methods // so it can dispatch the invocations to the interceptor. return new LazyLoadProxyFactory(); } } } }