/// <summary> /// Creates a list of Association fields for all the properties that use NakedObjects. /// </summary> private Tuple <IEnumerable <IAssociationSpecImmutable>, IImmutableDictionary <string, ITypeSpecBuilder> > CreateRefPropertySpecs(IEnumerable <PropertyInfo> foundProperties, IObjectSpecImmutable spec, IImmutableDictionary <string, ITypeSpecBuilder> metamodel) { var specs = new List <IAssociationSpecImmutable>(); foreach (PropertyInfo property in foundProperties) { // create a reference property spec var identifier = new IdentifierImpl(FullName, property.Name); Type propertyType = property.PropertyType; var result = reflector.LoadSpecification(propertyType, metamodel); metamodel = result.Item2; var propertySpec = result.Item1; if (propertySpec is IServiceSpecImmutable) { throw new ReflectionException(Log.LogAndReturn($"Type {propertyType.Name} is a service and cannot be used in public property {property.Name} on type {property.DeclaringType?.Name}. If the property is intended to be an injected service it should have a protected get.")); } var referenceProperty = ImmutableSpecFactory.CreateOneToOneAssociationSpecImmutable(identifier, spec, propertySpec as IObjectSpecImmutable); // Process facets for the property metamodel = FacetFactorySet.Process(reflector, property, new IntrospectorMethodRemover(methods), referenceProperty, FeatureType.Properties, metamodel); specs.Add(referenceProperty); } return(new Tuple <IEnumerable <IAssociationSpecImmutable>, IImmutableDictionary <string, ITypeSpecBuilder> >(specs, metamodel)); }
protected (IServiceProvider, IHost) GetContainer(Action <NakedCoreOptions> setup) { ImmutableSpecFactory.ClearCache(); var hostBuilder = CreateHostBuilder(new string[] { }, setup).Build(); return(hostBuilder.Services, hostBuilder); }
private Tuple <IEnumerable <IAssociationSpecImmutable>, IImmutableDictionary <string, ITypeSpecBuilder> > CreateCollectionSpecs(IEnumerable <PropertyInfo> collectionProperties, IObjectSpecImmutable spec, IImmutableDictionary <string, ITypeSpecBuilder> metamodel) { var specs = new List <IAssociationSpecImmutable>(); foreach (PropertyInfo property in collectionProperties) { IIdentifier identifier = new IdentifierImpl(FullName, property.Name); // create a collection property spec Type returnType = property.PropertyType; Type defaultType = typeof(object); var result = reflector.LoadSpecification(returnType, metamodel); metamodel = result.Item2; var returnSpec = result.Item1 as IObjectSpecImmutable; result = reflector.LoadSpecification(defaultType, metamodel); metamodel = result.Item2; var defaultSpec = result.Item1 as IObjectSpecImmutable; var collection = ImmutableSpecFactory.CreateOneToManyAssociationSpecImmutable(identifier, spec, returnSpec, defaultSpec); metamodel = FacetFactorySet.Process(reflector, property, new IntrospectorMethodRemover(methods), collection, FeatureType.Collections, metamodel); specs.Add(collection); } return(new Tuple <IEnumerable <IAssociationSpecImmutable>, IImmutableDictionary <string, ITypeSpecBuilder> >(specs, metamodel)); }
protected IServiceProvider GetContainer(IReflectorConfiguration rc) { ImmutableSpecFactory.ClearCache(); var hostBuilder = CreateHostBuilder(new string[] { }, rc).Build(); return(hostBuilder.Services); }
/// <summary> /// Creates a list of Association fields for all the properties that use NakedObjects. /// </summary> private IEnumerable <IAssociationSpecImmutable> CreateRefPropertySpecs(IEnumerable <PropertyInfo> foundProperties, IObjectSpecImmutable spec) { var specs = new List <IAssociationSpecImmutable>(); foreach (PropertyInfo property in foundProperties) { Log.DebugFormat("Identified 1-1 association method {0}", property); Log.DebugFormat("One-to-One association {0} -> {1}", property.Name, property); // create a reference property spec var identifier = new IdentifierImpl(FullName, property.Name); Type propertyType = property.PropertyType; var propertySpec = reflector.LoadSpecification(propertyType); if (propertySpec is IServiceSpecImmutable) { throw new ReflectionException(string.Format( "Type {0} is a service and cannot be used in public property {1} on type {2}." + " If the property is intended to be an injected service it should have a protected get.", propertyType.Name, property.Name, property.DeclaringType.Name )); } var referenceProperty = ImmutableSpecFactory.CreateOneToOneAssociationSpecImmutable(identifier, spec, propertySpec as IObjectSpecImmutable); // Process facets for the property FacetFactorySet.Process(reflector, property, new IntrospectorMethodRemover(methods), referenceProperty, FeatureType.Properties); specs.Add(referenceProperty); } return(specs); }
protected IUnityContainer GetContainer() { ImmutableSpecFactory.ClearCache(); var c = new UnityContainer(); RegisterTypes(c); return(c); }
private Tuple <IActionSpecImmutable[], IImmutableDictionary <string, ITypeSpecBuilder> > FindActionMethods(ITypeSpecImmutable spec, IImmutableDictionary <string, ITypeSpecBuilder> metamodel) { var actionSpecs = new List <IActionSpecImmutable>(); var actions = facetFactorySet.FindActions(methods.Where(m => m != null).ToArray(), reflector.ClassStrategy).Where(a => !facetFactorySet.Filters(a, reflector.ClassStrategy)).ToArray(); methods = methods.Except(actions).ToArray(); // ReSharper disable once ForCanBeConvertedToForeach // kepp for look as actions are nulled out within loop for (int i = 0; i < actions.Length; i++) { MethodInfo actionMethod = actions[i]; // actions are potentially being nulled within this loop if (actionMethod != null) { string fullMethodName = actionMethod.Name; Type[] parameterTypes = actionMethod.GetParameters().Select(parameterInfo => parameterInfo.ParameterType).ToArray(); // build action & its parameters // if static leave to facet to sort out return type if (actionMethod.ReturnType != typeof(void) && !actionMethod.IsStatic) { metamodel = reflector.LoadSpecification(actionMethod.ReturnType, metamodel).Item2; } IIdentifier identifier = new IdentifierImpl(FullName, fullMethodName, actionMethod.GetParameters().ToArray()); //IActionParameterSpecImmutable[] actionParams = parameterTypes. // Select(pt => ImmutableSpecFactory.CreateActionParameterSpecImmutable(reflector.LoadSpecification<IObjectSpecImmutable>(pt, metamodel), identifier)).ToArray(); var actionParams = new List <IActionParameterSpecImmutable>(); foreach (var pt in parameterTypes) { var result = reflector.LoadSpecification(pt, metamodel); metamodel = result.Item2; var ospec = result.Item1 as IObjectSpecImmutable; var actionSpec = ImmutableSpecFactory.CreateActionParameterSpecImmutable(ospec, identifier); actionParams.Add(actionSpec); } var action = ImmutableSpecFactory.CreateActionSpecImmutable(identifier, spec, actionParams.ToArray()); // Process facets on the action & parameters metamodel = facetFactorySet.Process(reflector, actionMethod, new IntrospectorMethodRemover(actions), action, FeatureType.Actions, metamodel); for (int l = 0; l < actionParams.Count; l++) { metamodel = facetFactorySet.ProcessParams(reflector, actionMethod, l, actionParams[l], metamodel); } actionSpecs.Add(action); } } return(new Tuple <IActionSpecImmutable[], IImmutableDictionary <string, ITypeSpecBuilder> >(actionSpecs.ToArray(), metamodel)); }
private (IActionSpecImmutable[], IImmutableDictionary <string, ITypeSpecBuilder>) FindActionMethods(ITypeSpecImmutable spec, IImmutableDictionary <string, ITypeSpecBuilder> metamodel) { var actionSpecs = new List <IActionSpecImmutable>(); var actions = FacetFactorySet.FindActions(methods.Where(m => m != null).ToArray(), reflector.ClassStrategy).Where(a => !FacetFactorySet.Filters(a, reflector.ClassStrategy)).ToArray(); methods = methods.Except(actions).ToArray(); // ReSharper disable once ForCanBeConvertedToForeach // keep a "for loop" as actions are nulled out within loop for (var i = 0; i < actions.Length; i++) { var actionMethod = actions[i]; // actions are potentially being nulled within this loop if (actionMethod != null) { var fullMethodName = actionMethod.Name; var parameterTypes = actionMethod.GetParameters().Select(parameterInfo => parameterInfo.ParameterType).ToArray(); // build action & its parameters if (actionMethod.ReturnType != typeof(void)) { (_, metamodel) = reflector.LoadSpecification(actionMethod.ReturnType, metamodel); } IIdentifier identifier = new IdentifierImpl(FullName, fullMethodName, actionMethod.GetParameters().ToArray()); var actionParams = new List <IActionParameterSpecImmutable>(); foreach (var pt in parameterTypes) { IObjectSpecBuilder oSpec; (oSpec, metamodel) = reflector.LoadSpecification <IObjectSpecBuilder>(pt, metamodel); var actionSpec = ImmutableSpecFactory.CreateActionParameterSpecImmutable(oSpec, identifier); actionParams.Add(actionSpec); } var action = ImmutableSpecFactory.CreateActionSpecImmutable(identifier, spec, actionParams.ToArray()); // Process facets on the action & parameters metamodel = FacetFactorySet.Process(reflector, actionMethod, new IntrospectorMethodRemover(actions), action, FeatureType.Actions, metamodel); for (var l = 0; l < actionParams.Count; l++) { metamodel = FacetFactorySet.ProcessParams(reflector, actionMethod, l, actionParams[l], metamodel); } actionSpecs.Add(action); } } return(actionSpecs.ToArray(), metamodel); }
public void TestDefaultPageSizePickedUp() { MethodInfo actionMethod = FindMethod(typeof(Customer1), "SomeAction"); var identifier = new IdentifierImpl("Customer1", "SomeAction"); var actionPeer = ImmutableSpecFactory.CreateActionSpecImmutable(identifier, null, null); new FallbackFacetFactory(0).Process(Reflector, actionMethod, MethodRemover, actionPeer); IFacet facet = actionPeer.GetFacet(typeof(IPageSizeFacet)); Assert.IsNotNull(facet); Assert.IsTrue(facet is PageSizeFacetDefault); var pageSizeFacet = (IPageSizeFacet)facet; Assert.AreEqual(20, pageSizeFacet.Value); AssertNoMethodsRemoved(); }
private IActionSpecImmutable[] FindActionMethods(ITypeSpecImmutable spec) { Log.Debug("Looking for action methods"); var actionSpecs = new List <IActionSpecImmutable>(); var actions = FacetFactorySet.FindActions(methods.Where(m => m != null).ToArray(), reflector.ClassStrategy).Where(a => !FacetFactorySet.Filters(a, reflector.ClassStrategy)).ToArray(); methods = methods.Except(actions).ToArray(); // ReSharper disable once ForCanBeConvertedToForeach // kepp for look as actions are nulled out within loop for (int i = 0; i < actions.Length; i++) { MethodInfo actionMethod = actions[i]; // actions are potentially being nulled within this loop if (actionMethod != null) { string fullMethodName = actionMethod.Name; Type[] parameterTypes = actionMethod.GetParameters().Select(parameterInfo => parameterInfo.ParameterType).ToArray(); // build action & its parameters if (actionMethod.ReturnType != typeof(void)) { reflector.LoadSpecification(actionMethod.ReturnType); } IIdentifier identifier = new IdentifierImpl(FullName, fullMethodName, actionMethod.GetParameters().ToArray()); IActionParameterSpecImmutable[] actionParams = parameterTypes.Select(pt => ImmutableSpecFactory.CreateActionParameterSpecImmutable(reflector.LoadSpecification <IObjectSpecImmutable>(pt), identifier)).ToArray(); var action = ImmutableSpecFactory.CreateActionSpecImmutable(identifier, spec, actionParams); // Process facets on the action & parameters FacetFactorySet.Process(reflector, actionMethod, new IntrospectorMethodRemover(actions), action, FeatureType.Actions); for (int l = 0; l < actionParams.Length; l++) { FacetFactorySet.ProcessParams(reflector, actionMethod, l, actionParams[l]); } actionSpecs.Add(action); } } return(actionSpecs.ToArray()); }
private static IActionSpecImmutable CreateHolderWithParms() { var tps1 = new Mock <IObjectSpecImmutable>(); //"System.Int32" var tps2 = new Mock <IObjectSpecImmutable>(); //System.Int64" var tps3 = new Mock <IObjectSpecImmutable>(); //"System.Int64" var param1 = ImmutableSpecFactory.CreateActionParameterSpecImmutable(tps1.Object, null); var param2 = ImmutableSpecFactory.CreateActionParameterSpecImmutable(tps2.Object, null); var param3 = ImmutableSpecFactory.CreateActionParameterSpecImmutable(tps3.Object, null); var parms = new[] { param1, param2, param3 }; var tpi = new Mock <IIdentifier>(); // ""action" var id = tpi.Object; return(ImmutableSpecFactory.CreateActionSpecImmutable(id, null, parms)); }
public void TestDefaultPageSizePickedUp() { IImmutableDictionary <string, ITypeSpecBuilder> metamodel = new Dictionary <string, ITypeSpecBuilder>().ToImmutableDictionary(); var actionMethod = FindMethod(typeof(Customer1), "SomeAction"); var identifier = new IdentifierImpl("Customer1", "SomeAction"); var actionPeer = ImmutableSpecFactory.CreateActionSpecImmutable(identifier, null, null); metamodel = new FallbackFacetFactory(0, null).Process(Reflector, actionMethod, MethodRemover, actionPeer, metamodel); var facet = actionPeer.GetFacet(typeof(IPageSizeFacet)); Assert.IsNotNull(facet); Assert.IsTrue(facet is PageSizeFacetDefault); var pageSizeFacet = (IPageSizeFacet)facet; Assert.AreEqual(20, pageSizeFacet.Value); AssertNoMethodsRemoved(); Assert.IsNotNull(metamodel); }
private IEnumerable <IAssociationSpecImmutable> CreateCollectionSpecs(IEnumerable <PropertyInfo> collectionProperties, IObjectSpecImmutable spec) { var specs = new List <IAssociationSpecImmutable>(); foreach (PropertyInfo property in collectionProperties) { IIdentifier identifier = new IdentifierImpl(FullName, property.Name); // create a collection property spec Type returnType = property.PropertyType; var returnSpec = reflector.LoadSpecification <IObjectSpecImmutable>(returnType); Type defaultType = typeof(object); var defaultSpec = reflector.LoadSpecification <IObjectSpecImmutable>(defaultType); var collection = ImmutableSpecFactory.CreateOneToManyAssociationSpecImmutable(identifier, spec, returnSpec, defaultSpec); FacetFactorySet.Process(reflector, property, new IntrospectorMethodRemover(methods), collection, FeatureType.Collections); specs.Add(collection); } return(specs); }
private ITypeSpecBuilder CreateSpecification(Type type) { TypeUtils.GetType(type.FullName); // This should ensure type is cached return(IsService(type) ? (ITypeSpecBuilder)ImmutableSpecFactory.CreateServiceSpecImmutable(type, metamodel) : ImmutableSpecFactory.CreateObjectSpecImmutable(type, metamodel)); }