コード例 #1
0
ファイル: DAO.cs プロジェクト: lrenda91/CommonLib.NET
        /// <summary>
        /// Creates a <typeparamref name="TClass"/> instance from the specified query loader and executor.
        /// The implemented interface and the target subclass of <see cref="DAO"/> also implementing the interface are provided as generic arguments
        /// </summary>
        /// <typeparam name="TInterface">The base interface the DAO must implement</typeparam>
        /// <typeparam name="TClass">The concrete class to instantiate</typeparam>
        /// <param name="dbMngr">The db executor to assign to the DAO instance</param>
        /// <param name="qL">The query loader to assign to the DAO instance</param>
        /// <param name="proxy">The proxy object that intercepts target instance methods</param>
        /// <returns>The <typeparamref name="TClass"/> target instance</returns>
        public static TClass CreateInstance <TInterface, TClass>(IDBConnectionManager dbMngr, IQueryLoader qL, out TInterface proxy) where TClass : DAO, TInterface
        {
            TClass target = (TClass)Activator.CreateInstance(typeof(TClass));

            target.DBConnectionManager = dbMngr;
            target.SqlQueryLoader      = qL;

            proxy = InterceptorUtil.GetWrappedInstance <TInterface>(target);

            return(target);
        }
コード例 #2
0
        public static TInterface Get <TInterface>(IDBConnectionManager dbMngr, IQueryLoader qL)
        {
            TInterface proxy;
            DAO        target = DaoFactory.CreateInstance <TInterface>(null, null, out proxy);

            target.Done += (sender, eventArgs) =>
            {
                Console.WriteLine("FINE");
                foreach (var pair in eventArgs.Input)
                {
                    Console.WriteLine(pair.Key + " -> " + pair.Value);
                }
            };
            target.Error += (sender, eventArgs) =>
            {
                Console.WriteLine("Error");
            };
            return(proxy);
        }
コード例 #3
0
ファイル: DAO.cs プロジェクト: lrenda91/CommonLib.NET
        /// <summary>
        /// Creates a <see cref="DAO"/> instance from the specified query loader and executor.
        /// It finds the candidate concrete class looking for a subclass of DAO also implementing <typeparamref name="TInterface"/>,
        /// instantiates it and returns the created instance.
        /// </summary>
        /// <typeparam name="TInterface">The base interface the DAO must implement</typeparam>
        /// <param name="dbMngr">The db executor to assign to the DAO instance</param>
        /// <param name="qL">The query loader to assign to the DAO instance</param>
        /// <param name="proxy">The proxy object that intercepts target instance methods</param>
        /// <returns>The DAO target instance</returns>
        public static DAO CreateInstance <TInterface>(IDBConnectionManager dbMngr, IQueryLoader qL, out TInterface proxy)
        {
            var DaoInterfaceType = typeof(TInterface);

            if (!DaoInterfaceType.IsInterface)
            {
                throw new ArgumentException(string.Format("{0} deve essese una interface!", DaoInterfaceType.Name));
            }
            var types = Assembly.GetCallingAssembly().GetTypes().AsEnumerable();

            types = types.Where((t) =>
                                t.IsClass &&
                                !t.IsAbstract &&
                                t.IsSubclassOf(typeof(DAO)) &&
                                string.Equals(t.Namespace, DaoInterfaceType.Namespace)
                                );
            if (types.Count() == 0)
            {
                throw new ArgumentException("Nessuna sottoclasse concreta di DAO sotto " + DaoInterfaceType.Namespace);
            }
            var tuple = new Tuple <string, string>(DaoInterfaceType.Namespace, DaoInterfaceType.Name);

            types = types.Where((t) =>
            {
                var s = (t.GetInterfaces().Select <Type, Tuple <string, string> >((implemented) =>
                                                                                  new Tuple <string, string>(implemented.Namespace, implemented.Name))
                         );
                return(s.Contains(tuple));
            });

            int typesFound = types.Count();

            if (typesFound == 0)
            {
                throw new ArgumentException(string.Format("Nessuna sottoclasse di DAO sotto {0} implementa {1}", DaoInterfaceType.Namespace, DaoInterfaceType.Name));
            }
            else if (typesFound > 1)
            {
                var    selected = types.Select <Type, string>((t) => string.Format("{0}.{1}", t.Namespace, t.Name));
                string msg      = string.Empty;
                foreach (string s in selected)
                {
                    msg += (" " + s);
                }
                throw new ArgumentException(string.Format("Trovate più sottoclassi di DAO sotto {0} che implementa {1}:\n[{2}]", DaoInterfaceType.Namespace, DaoInterfaceType.Name, msg));
            }

            Type subclassType = types.ToList().First();

            if (DaoInterfaceType.IsGenericType)
            {
                subclassType = subclassType.MakeGenericType(DaoInterfaceType.GetGenericArguments());
            }

            DAO target = (DAO)Activator.CreateInstance(subclassType);

            target.DBConnectionManager = dbMngr;
            target.SqlQueryLoader      = qL;

            proxy = InterceptorUtil.GetWrappedInstance <TInterface>(target);

            return(target);
        }