public SqlCommandGenerateContext Build() { var parser = new DefaultCommandParser(); this.context.QueryAttributes = this.context.Method.GetCustomAttributes <SqlAttribute>(); this.context.HasAnyQueryAttribute = this.context.QueryAttributes != null && this.context.QueryAttributes.Any(); if (!this.context.HasAnyQueryAttribute) { this.context.CommandInfo = parser.Parse(this.context.Method.Name); } this.context.IDaoType = this.context.Method.DeclaringType; IEntityTypeProvider entityTypeProvider = NpiServicesCollection.GetService <IEntityTypeProvider>(); this.context.EntityType = entityTypeProvider.Provide(this.context.Method); ITableNameProvider tableNameProvider = NpiServicesCollection.GetService <ITableNameProvider>(); this.context.TableName = tableNameProvider.Provide(this.context.EntityType); this.context.Properties = this.context.EntityType.GetProperties(); return(this.context); }
public void Intercept(IInvocation invocation) { string methodName = invocation.Method.Name; Type typeofIDao = typeof(INpiDao <>); Type entityType = invocation.Method.DeclaringType.GetInterface(typeofIDao.FullName).GetGenericArguments()[0]; // todo :对其单例化 ISqlCommandGenerator g = NpiServicesCollection.GetService <ISqlServerCommandGenerator>(); SqlCommandDescription d = g.Generate(invocation.Method, invocation.Arguments); DebugLogger.Debug(d.ToString()); DapperParameters dp = new DapperParameters(); foreach (var p in d.Parameters) { dp[p.Key] = p.Value.Value; } object dbReturnedValue = null; object handledValue = null; DebugLogger.Debug("准备执行 Sql"); switch (d.Type) { case SqlCommandTypes.Insert: case SqlCommandTypes.Update: case SqlCommandTypes.Delete: { var executor = ServicesCollection.GetService <ISqlCommandExecutor>(); dbReturnedValue = executor.Execute(d.SqlCommand, dp, dbConnectionContext); } break; case SqlCommandTypes.Select: { var querier = ServicesCollection.GetService <ISqlCommandQuerier>(); dbReturnedValue = querier.Select(entityType, d.SqlCommand, dp, dbConnectionContext); } break; default: break; } DebugLogger.Debug("Sql 执行完毕,开始处理结果集"); foreach (var handler in dbReturnValueHandlers) { if (!handler.CanHandle(invocation.Method, entityType)) { continue; } handledValue = handler.Handle(invocation.Method, entityType, dbReturnedValue); } invocation.ReturnValue = handledValue; DebugLogger.Debug("结果集处理完毕"); }
public override void Intercept(InterfaceInvocationInfo info) { this.AssertProviderIsValid(); Type typeOfIDao = typeof(INpiDao <>); ISqlCommandGenerator g = NpiServicesCollection.GetService <ISqlServerCommandGenerator>(); SqlCommandDescription d = g.Generate(info.Method, info.Arguments); this.EventBus.Publish(new SqlCommandDescriptionGeneratedEvent(this, d)); if (info.Method.ReturnType == typeof(void)) { d.Mode = SqlCommandExecuteModes.Execute; } switch (d.Mode) { case SqlCommandExecuteModes.Execute: int i = this.Executor.Execute(this.Provider.Provide(), d); foreach (var handler in this.ExecuteResultHandlers) { if (!handler.CanHandle(info.Method)) { continue; } info.ReturnValue = handler.Handle(info.Method, i); } break; case SqlCommandExecuteModes.Query: { Type returnType = info.Method.ReturnType; Type itemType; if (TryGetIEnumerableItemType(returnType, out itemType)) { returnType = itemType; } IEnumerable <object> list = this.Executor.Select(this.Provider.Provide(), d, returnType); foreach (var handler in this.SelectResultHandlers) { if (!handler.CanHandle(info.Method, returnType)) { continue; } info.ReturnValue = handler.Handle(info.Method, returnType, list); } } break; default: break; } }
private ICommandInfo ParseFromCommand(Queue <string> wordQueue) { string action = wordQueue.Dequeue(); string realCommand = wordQueue.Join("", x => x); switch (action) { case ACTION_PAGING: { SelectInfo info = this.ParseFromCommand(wordQueue) as SelectInfo; if (info == null) { throw new PagingMustBeFollowedBySelectException(); } info.Paging = true; return(info); } case ACTION_GET: case ACTION_SELECT: case ACTION_FETCH: case ACTION_FIND: return(NpiServicesCollection.GetService <ISelectParser>().Parse(realCommand)); case ACTION_UPDATE: case ACTION_MODIFY: return(NpiServicesCollection.GetService <IUpdateParser>().Parse(realCommand)); case ACTION_REMOVE: case ACTION_DELETE: return(NpiServicesCollection.GetService <IDeleteParser>().Parse(realCommand)); case ACTION_CREATE: case ACTION_NEW: case ACTION_INSERT: return(NpiServicesCollection.GetService <IInsertParser>().Parse(realCommand)); case ACTION_COUNT: return(NpiServicesCollection.GetService <ICountParser>().Parse(realCommand)); default: throw new NotSupportActionException(action); } }
public DefaultEntityTypeProvider() { this.cache = NpiServicesCollection.GetService <ICache>(); }
public DefaultCommandParser() { this.cache = NpiServicesCollection.GetService <ICache>(); }
public DefaultSqlServerCommandGenerator() { this.fieldNameProvider = NpiServicesCollection.GetService <IFieldNameProvider>(); this.conditionGenerateHandler = NpiServicesCollection.GetService <IConditionGenerateHandler>(); }
public DefaultParameterLookupFactory() { this.lookups = NpiServicesCollection.GetServices <IParameterLookup>(); }
public DefaultSqlParameterFinder() { cache = NpiServicesCollection.GetService <ICache>(); }
static OperatorMappedConditionGenerator() { operatorMapper = NpiServicesCollection.GetService <IOperatorMapper>(); }
public SqlCommandGeneratorBase() { this.parameterLookupFactory = NpiServicesCollection.GetService <IParameterLookupFactory>(); this.cache = NpiServicesCollection.GetService <ICache>(); this.sqlParameterFinder = NpiServicesCollection.GetService <ISqlParameterFinder>(); }
static DefaultParseStateMachine() { stateMachineBuilderFactory = NpiServicesCollection.GetService <IStateMachineBuilderFactory>(); }
public DefaultInsertParserTests() { this.parser = NpiServicesCollection.GetService <ICommandParser>(); }
public DefaultConditionGenerateHandler() { this.conditionGenerators = NpiServicesCollection.GetServices <IConditionGenerator>(); }
static DefaultOperatorMapper() { cache = NpiServicesCollection.GetService <ICache>(); }
public DefaultOperatorMapper() { this.ResourceProvider = NpiServicesCollection.GetService <IResourceProvider>(); }
public DefaultFieldNameProvider() { this.cache = NpiServicesCollection.GetService <ICache>(); }
public ResourceNameProviderTests() { this.resourceNameProvider = NpiServicesCollection.GetService <IResourceNameProvider>(); }
public DefaultStateMachineBuilderFactory() { this.cache = NpiServicesCollection.GetService <ICache>(); this.resourceProvider = NpiServicesCollection.GetService <IResourceProvider>(); this.resourceNameProvider = NpiServicesCollection.GetService <IResourceNameProvider>(); }