public static Expression <Func <ExportDefinition, bool> > CreateConstraint(string contractName, string requiredTypeIdentity, IEnumerable <KeyValuePair <string, Type> > requiredMetadata, CreationPolicy requiredCreationPolicy) { ParameterExpression parameter = Expression.Parameter(typeof(ExportDefinition), "exportDefinition"); Expression constraintBody = ConstraintServices.CreateContractConstraintBody(contractName, parameter); if (!string.IsNullOrEmpty(requiredTypeIdentity)) { Expression typeIdentityConstraintBody = ConstraintServices.CreateTypeIdentityContraint(requiredTypeIdentity, parameter); constraintBody = Expression.AndAlso(constraintBody, typeIdentityConstraintBody); } if (requiredMetadata != null) { Expression metadataConstraintBody = ConstraintServices.CreateMetadataConstraintBody(requiredMetadata, parameter); if (metadataConstraintBody != null) { constraintBody = Expression.AndAlso(constraintBody, metadataConstraintBody); } } if (requiredCreationPolicy != CreationPolicy.Any) { Expression policyConstraintBody = ConstraintServices.CreateCreationPolicyContraint(requiredCreationPolicy, parameter); constraintBody = Expression.AndAlso(constraintBody, policyConstraintBody); } Expression <Func <ExportDefinition, bool> > constraint = Expression.Lambda <Func <ExportDefinition, bool> >(constraintBody, parameter); return(constraint); }
public void TryParseConstraint_ConstraintFromCreateConstraintAsConstraintArgument1_CanParse() { var expectations = Expectations.GetContractNamesWithEmpty(); foreach (var e in expectations) { var constraint = ConstraintServices.CreateConstraint((string)e, null, null, CreationPolicy.Any); AssertCanParse(constraint, e, new Dictionary <string, Type>()); } }
public void TryParseConstraint_ConstraintFromCreateConstraintAsConstraintArgument2_CanParse() { var expectations = Expectations.GetRequiredMetadata(); foreach (var e in expectations) { var constraint = ConstraintServices.CreateConstraint((IEnumerable <KeyValuePair <string, Type> >)e); AssertCanParse(constraint, (string)null, e); } }
public void CreationPolicyConstraint_NoCreationPolicy_ShouldNotMatch() { var contractName = "MyContract"; var metadata = new Dictionary <string, object>(); var exportDefinition = new ExportDefinition(contractName, metadata); var constraint = ConstraintServices.CreateConstraint(contractName, null, null, CreationPolicy.Shared); var predicate = constraint.Compile(); Assert.IsTrue(predicate(exportDefinition)); }
public void TypeIdentityConstraint_InvalidExportDef_ShouldNotMatch() { var contractName = "MyContract"; var typeIdentity = AttributedModelServices.GetTypeIdentity(typeof(ConstraintServicesTests)); var metadata = new Dictionary <string, object>(); var exportDefinition = new ExportDefinition(contractName, metadata); var constraint = ConstraintServices.CreateConstraint(contractName, typeIdentity, null, CreationPolicy.Any); var predicate = constraint.Compile(); Assert.IsFalse(predicate(exportDefinition)); }
public void CreationPolicyConstraint_ValidNonMatchingCreationPolicy_ShouldNotMatch() { var contractName = "MyContract"; var metadata = new Dictionary <string, object>(); metadata.Add(CompositionConstants.PartCreationPolicyMetadataName, CreationPolicy.NonShared); var exportDefinition = new ExportDefinition(contractName, metadata); var constraint = ConstraintServices.CreateConstraint(contractName, null, null, CreationPolicy.Shared); var predicate = constraint.Compile(); Assert.IsFalse(predicate(exportDefinition)); }
public void TryParseConstraint_ConstraintFromCreateConstraintAsConstraintArgument3_CanParse() { var contractNames = Expectations.GetContractNames(); var metadataValues = Expectations.GetRequiredMetadata(); foreach (var contractName in contractNames) { foreach (var metadataValue in metadataValues) { var constraint = ConstraintServices.CreateConstraint(contractName, null, metadataValue, CreationPolicy.Any); AssertCanParse(constraint, contractName, metadataValue); } } }
public static Expression <Func <ExportDefinition, bool> > CreateConstraint(IEnumerable <KeyValuePair <string, Type> > requiredMetadata) { ParameterExpression parameter = Expression.Parameter(typeof(ExportDefinition), "exportDefinition"); Expression metadataConstraintBody = null; if (requiredMetadata != null) { metadataConstraintBody = ConstraintServices.CreateMetadataConstraintBody(requiredMetadata, parameter); } if (metadataConstraintBody != null) { return(Expression.Lambda <Func <ExportDefinition, bool> >(metadataConstraintBody, parameter)); } return(null); }