//----------------------------------------------------------------------------------------------------------------------------------------------------- void IObjectFactoryConvention.Apply(ObjectFactoryContext context) { if (m_Will.HasFlag(Will.InspectDeclaration)) { OnInspectDeclaration(context); } if (m_Will.HasFlag(Will.ImplementBaseClass)) { OnImplementBaseClass(context.CreateImplementationWriter <TypeTemplate.TBase>()); } if (m_Will.HasFlag(Will.ImplementPrimaryInterface) && context.TypeKey.PrimaryInterface != null && ShouldImplementInterface(context, context.TypeKey.PrimaryInterface)) { using (TypeTemplate.CreateScope <TypeTemplate.TInterface>(context.TypeKey.PrimaryInterface)) { OnImplementPrimaryInterface(context.CreateImplementationWriter <TypeTemplate.TInterface>()); } } if (m_Will.HasFlag(Will.ImplementAnySecondaryInterface)) { foreach (var secondaryInterface in context.TypeKey.SecondaryInterfaces .Where(t => ShouldImplementInterface(context, t))) { using (TypeTemplate.CreateScope <TypeTemplate.TInterface>(secondaryInterface)) { OnImplementAnySecondaryInterface(context.CreateImplementationWriter <TypeTemplate.TInterface>()); } } } if (m_Will.HasFlag(Will.ImplementAnyInterface)) { foreach (var secondaryInterface in context.TypeKey.GetAllInterfaces() .Where(t => ShouldImplementInterface(context, t))) { using (TypeTemplate.CreateScope <TypeTemplate.TInterface>(secondaryInterface)) { OnImplementAnyInterface(context.CreateImplementationWriter <TypeTemplate.TInterface>()); } } } if (m_Will.HasFlag(Will.ImplementAnyBaseType)) { foreach (var secondaryInterface in context.TypeKey.GetAllAncestorTypes() .Where(t => !t.IsInterface || ShouldImplementInterface(context, t))) { using (TypeTemplate.CreateScope <TypeTemplate.TBase>(secondaryInterface)) { OnImplementAnyBaseType(context.CreateImplementationWriter <TypeTemplate.TBase>()); } } } }
//----------------------------------------------------------------------------------------------------------------------------------------------------- public void Apply(ObjectFactoryContext context) { foreach (var convention in m_ContainedConventions) { if (convention.ShouldApply(context)) { convention.Apply(context); } } }
//----------------------------------------------------------------------------------------------------------------------------------------------------- private IObjectFactoryConvention[] GetConventions(ObjectFactoryContext context) { if (m_ConventionSingletonInstances != null) { return(m_ConventionSingletonInstances); } else { return(m_TransientConventionFactory(context).ToArray()); } }
//----------------------------------------------------------------------------------------------------------------------------------------------------- protected override ClassType DefineNewClass(TypeKey key) { var context = new ObjectFactoryContext(this, key); var conventions = GetConventions(context); foreach (var convention in conventions) { if (convention.ShouldApply(context)) { convention.Apply(context); } } if (context.ClassType == null) { throw new CodeGenerationException("Class type was not created."); } return(context.ClassType); }
//----------------------------------------------------------------------------------------------------------------------------------------------------- #region IObjectFactoryConvention Members public bool ShouldApply(ObjectFactoryContext context) { return(m_ContainedConventions.Any(c => c.ShouldApply(context))); }
//----------------------------------------------------------------------------------------------------------------------------------------------------- protected virtual bool ShouldApply(ObjectFactoryContext context) { return(true); }
//----------------------------------------------------------------------------------------------------------------------------------------------------- #region IObjectFactoryConvention Members bool IObjectFactoryConvention.ShouldApply(ObjectFactoryContext context) { return(this.ShouldApply(context)); }
//----------------------------------------------------------------------------------------------------------------------------------------------------- protected virtual void OnInspectDeclaration(ObjectFactoryContext context) { }
//----------------------------------------------------------------------------------------------------------------------------------------------------- protected virtual bool ShouldImplementInterface(ObjectFactoryContext context, Type interfaceType) { return(true); }
//----------------------------------------------------------------------------------------------------------------------------------------------------- void IObjectFactoryConvention.Apply(ObjectFactoryContext context) { context.CreateDecorationWriter(this); }