Пример #1
0
        public IDbTable <TEntity> Set <TEntity>(Action <DbModelStrategyBuilder> modelStrategyAction = null)
            where TEntity : class, new()
        {
            var strategyBuilder = new DbModelStrategyBuilder();

            modelStrategyAction?.Invoke(strategyBuilder);

            // get model type for this table.
            var modelType = typeof(TEntity);
            // create default implementation.
            var tableImplType = typeof(DbTable <>).MakeGenericType(modelType);

            // if model strategy is defined, pass it into table constructor.
            var modelStrategy = strategyBuilder.GetTableModelStrategy(modelType);

            // set instance to property.
            if (modelStrategy != null)
            {
                modelStrategy = DetectAndConfigureModelStrategy(modelStrategy);
            }
            else
            {
                modelStrategy = strategyBuilder.Table(modelType);
                modelStrategy = DetectAndConfigureModelStrategy(modelStrategy);
            }

            var dbServiceProvider = GetRuntimeServiceProvider();
            var tableImpl         = Activator.CreateInstance(tableImplType, modelStrategy, dbServiceProvider);

            return(tableImpl as IDbTable <TEntity>);
        }
Пример #2
0
        private void Initialize()
        {
            var strategyBuilder = new DbModelStrategyBuilder();

            DefiningDbModelStrategies(strategyBuilder);

            var tableProps = GetType().GetProperties(
                BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);

            foreach (var tableProp in tableProps)
            {
                if (!tableProp.PropertyType.IsGenericType)
                {
                    var modelType = strategyBuilder.GetModelTypeFromProperty(tableProp.Name);

                    // if model strategy is defined, pass it into table constructor.
                    var modelStrategy =
                        strategyBuilder.GetTableModelStrategy(modelType)
                        ?? strategyBuilder.Table(modelType);

                    // detect if model has data annotations.
                    DetectAndConfigureModelStrategy(modelStrategy);

                    // set instance to property.
                    var dbServiceProvider = GetRuntimeServiceProvider();
                    var table             = Activator.CreateInstance(typeof(IDbTable), modelStrategy, dbServiceProvider) as IDbTable;
                    tableProp.SetValue(this, table, null);
                }
                else
                {
                    if (!tableProp.PropertyType.FullName.Contains("IDbTable"))
                    {
                        continue;
                    }

                    var tableInterfaceType = tableProp.PropertyType;

                    // get model type for this table.
                    var modelType = tableInterfaceType.GetGenericArguments().First();
                    // create default implementation.
                    var tableImplType = typeof(DbTable <>).MakeGenericType(modelType);

                    // if model strategy is defined, pass it into table constructor.
                    var modelStrategy = strategyBuilder.GetTableModelStrategy(modelType);

                    // set instance to property.
                    if (modelStrategy != null)
                    {
                        modelStrategy = DetectAndConfigureModelStrategy(modelStrategy);
                    }
                    else
                    {
                        modelStrategy = strategyBuilder.Table(modelType);
                        modelStrategy = DetectAndConfigureModelStrategy(modelStrategy);
                    }

                    var dbServiceProvider = GetRuntimeServiceProvider();
                    var tableImpl         = Activator.CreateInstance(tableImplType, modelStrategy, dbServiceProvider);
                    tableProp.SetValue(this, tableImpl, null);
                }
            }

            _storedProcedureStrategies = strategyBuilder.GetRegisteredStoredProcedureStrategies();
        }