public INamedReference GenerateFactoryMethod( IServiceSetupRecord setup, Dictionary <string, INamedReference> ctorParamValueFactoryFields, INamedReference valueFactoryField, IClassBuilder locatorBuilder, INamedReference previousPluginMethod) { var implementingType = setup.ImplementingType ?? setup.InterfaceType; var methodName = $"Factory{TypeNameHelper.GetTypeMark(setup.InterfaceType)}_{Guid.NewGuid():N}"; var method = locatorBuilder.HasMethod(methodName).WithModifier("private").Returns(setup.InterfaceType); method.Body.Write("return ").InvokeConstructor(implementingType, invocation => { foreach (var constructorParameter in setup.PreferredConstructor.GetParameters()) { INamedReference factoryField; if (ctorParamValueFactoryFields.TryGetValue(constructorParameter.Name, out factoryField)) { invocation.WithParam(b => b.Write("(").Write(constructorParameter.ParameterType).Write(")").Write(factoryField).Write("(this)")); } else { invocation.WithParam(b => b.Write("(").Write(constructorParameter.ParameterType).Write(")").Write("Get(typeof(").Write(constructorParameter.ParameterType).Write("))")); } } }).EndStatement(); return(method); }
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 INamedReference GenerateFactoryMethod( IServiceSetupRecord setup, Dictionary <string, INamedReference> ctorParamValueFactoryFields, INamedReference valueFactoryField, IClassBuilder locatorBuilder, INamedReference previousPluginMethod) { var methodName = $"Factory{TypeNameHelper.GetTypeMark(setup.InterfaceType)}_{Guid.NewGuid():N}"; var method = locatorBuilder.HasMethod(methodName).WithModifier("private").Returns(setup.InterfaceType); method.Body.Write("return ").Write("(").Write(setup.InterfaceType).Write(")").Write(valueFactoryField).Write("(this)").EndStatement(); return(method); }
public INamedReference GenerateFactoryMethod( IServiceSetupRecord setup, Dictionary <string, INamedReference> ctorParamValueFactoryFields, INamedReference valueFactoryField, IClassBuilder locatorBuilder, INamedReference previousPluginMethod) { if (!setup.GetBehavior(s_defaultBehavior).Dispose) { return(previousPluginMethod); } var method = locatorBuilder.HasMethod($"Dispose{TypeNameHelper.GetTypeMark(setup.InterfaceType)}_{Guid.NewGuid():N}") .Returns(setup.InterfaceType); method.Body.Write("return ") .Invoke("TryRegisterDisposable", invocation => invocation.WithParam(p => p.Invoke(previousPluginMethod, inv => { }))) .EndStatement(); return(method); }
public INamedReference GenerateFactoryMethod( IServiceSetupRecord setup, Dictionary <string, INamedReference> ctorParamValueFactoryFields, INamedReference valueFactoryField, IClassBuilder locatorBuilder, INamedReference previousPluginMethod) { if (setup.GetBehavior(s_defaultBehavior).AlwaysNewInstance) { return(previousPluginMethod); } var field = locatorBuilder.HasField(setup.InterfaceType).WithModifier("private"); var fMethod = locatorBuilder.HasMethod($"Singleton_{TypeNameHelper.GetTypeMark(setup.InterfaceType)}_{Guid.NewGuid():N}").Returns(setup.InterfaceType); fMethod.Body.Write(" return ") .LazyReadOrAssign(field, a => a.Invoke(previousPluginMethod, inv => { })) .EndStatement(); return(fMethod); }