public INamedReference GenerateFactoryMethod( IServiceSetupRecord setup, Dictionary <string, INamedReference> ctorParamValueFactoryFields, INamedReference valueFactoryField, IClassBuilder locatorBuilder, INamedReference previousPluginMethod) { if (!IsApplicable(setup)) { return(previousPluginMethod); } var behavior = setup.Behaviors.OfType <EntityBehavior>().Last(); var entityClass = locatorBuilder.HasNestedClass($"{TypeNameHelper.GetTypeMark(setup.InterfaceType)}_Impl_{Guid.NewGuid():N}") .WithModifier("private") .WithModifier("sealed") .Implements <IEntity>() .Implements(setup.InterfaceType); var locatorField = entityClass.HasField <IServiceLocator>(); var ctor = entityClass.WithConstructor().WithModifier("public"); var locatorParam = ctor.WithParam <IServiceLocator>("locator"); ctor.Body.Assign(locatorField, a => a.Write(locatorParam)).EndStatement(); ctor.Body.Write("IsDirty = true").EndStatement(); var entityProperties = ImplementProperties(entityClass, setup.InterfaceType, behavior).ToList(); ImplementIsDirtyProperty(entityClass); var pkProperty = ImplementReadPrimaryKeyAndGetPkProperty(entityClass, behavior, setup.InterfaceType); ImplementGetSaveMethodTypeMethod(entityClass, pkProperty); ImplementGetValuesMethod(entityClass, entityProperties, pkProperty); ImplementReadSelfMethod(entityClass, locatorField, entityProperties); ImplementReferenceEntityProperties(entityClass, setup.InterfaceType, m_setup.EntityNamingConvention); ImplementDbEntityNameProperty(entityClass, setup.InterfaceType); ImplementPkTypeProperty(entityClass, pkProperty); ImplementIsPkAuto(entityClass, pkProperty); var collectionMethod = locatorBuilder.HasMethod("GetEntitiesCollection").Returns(typeof(IEnumerable <Type>)); collectionMethod.Body.Write("yield return typeof(") .Write(setup.InterfaceType) .Write(")") .EndStatement() .NewLine(); var factoryMethod = locatorBuilder.HasMethod($"{TypeNameHelper.GetTypeMark(setup.InterfaceType)}_{Guid.NewGuid():N}").Returns(setup.InterfaceType); factoryMethod.Body.Write("return ") .InvokeConstructor(entityClass, inv => inv.WithParam("this")) .EndStatement(); return(factoryMethod); }
public INamedReference GenerateFactoryMethod( IServiceSetupRecord setup, Dictionary <string, INamedReference> ctorParamValueFactoryFields, INamedReference valueFactoryField, IClassBuilder locatorBuilder, INamedReference previousPluginMethod) { var implClass = locatorBuilder.HasNestedClass( $"{TypeNameHelper.GetTypeMark(this.GetType())}_implements_{TypeNameHelper.GetTypeMark(setup.InterfaceType)}_{Guid.NewGuid():N}") .Implements(setup.InterfaceType) .WithModifier("private") .WithModifier("sealed"); var locatorField = implClass.HasField <IServiceLocator>(); var locatorParam = implClass.WithConstructor().WithModifier("public").WithParam("locator", typeof(IServiceLocator)); implClass.WithConstructor().Body.Assign(locatorField, a => a.Write(locatorParam)); foreach (var property in setup.InterfaceType.GetProperties()) { if (!ImplementProperty(implClass, property, locatorField)) { DefaultImplementProperty(implClass, property); } } foreach (var method in setup.InterfaceType.GetMethods()) { if (method.IsSpecialName) { continue; } if (!ImplementMethod(implClass, method, locatorField)) { DefaultImplementMethod(implClass, method); } } var factoryMethod = locatorBuilder.HasMethod($"factory_{TypeNameHelper.GetTypeMark(setup.InterfaceType)}_{Guid.NewGuid():N}").Returns(setup.InterfaceType).WithModifier("private"); factoryMethod.Body.Write(" return ") .InvokeConstructor(implClass, i => i.WithParam(p => p.Write("this"))) .EndStatement(); return(factoryMethod); }
public virtual INamedReference BuildProxyClass(IClassBuilder locatorBuilder, Type controllerType, Type callBuilderType) { var proxy = locatorBuilder.HasNestedClass($"ControllerProxy_{controllerType.Name}_{Guid.NewGuid():N}"); proxy.Inherits(controllerType); proxy.Implements <ILocatorBoundController>(); var proxyCtor = proxy.WithConstructor().WithModifier("public"); var locatorRef = proxyCtor.WithParam("__locator", typeof(IServiceLocator)); var locatorField = proxy.HasField <IServiceLocator>().WithModifier("private").WithModifier("readonly"); var baseCall = proxyCtor.CallsBase(); var baseCtor = controllerType.GetConstructors().FirstOrDefault(); proxyCtor.Body.Assign(locatorField, a => a.Write(locatorRef)); var interceptorField = proxy.HasField(typeof(IControllerInterceptor)).WithModifier("private").WithModifier("readonly"); proxyCtor.Body.Assign( interceptorField, asg => asg.Write(typeof(InterceptorProvider)) .Write(".") .Write(nameof(InterceptorProvider.GetInterceptor)) .Write("(this)")); foreach (var baseCtorParam in baseCtor.GetParameters()) { baseCall.WithParam( p => p.Write(locatorRef) .Write(".Get<") .Write(baseCtorParam.ParameterType) .Write(">()")); } proxy.HasProperty <IServiceLocator>(nameof(ILocatorBoundController.Locator)).WithModifier("public").HasGetter().Returns(w => w.Write(locatorField)); AddDependencies(locatorBuilder, proxyCtor, controllerType); ImplementExecuteMethod(proxy, controllerType, callBuilderType, interceptorField); return(proxy); }