Class DefaultCodecFactory
Implements the default functionality of ICodecFactory.
To replace the DefaultCodecFactory instance, call SetCodecFactory(ICodecFactory) at application start up. DefaultCodecFactory can be subclassed or passed additional parameters to register additional codecs, inject dependencies, or change caching behavior, as shown in the following examples. Alternatively, ICodecFactory can be implemented to provide complete control over codec creation and lifetimes.
Register Additional Codecs
Additional codecs can be added by initializing the instance of DefaultCodecFactory and passing an array of Codec-derived types.
// Register the factory at application start up.
Codec.SetCodecFactory(new DefaultCodecFactory {
CustomCodecTypes = new Type[] { typeof(MyCodec), typeof(AnotherCodec) }
});
Only Use Explicitly Defined Codecs
PutCodecType(Type) can be used to explicitly add codec types. In this example,
the call to base.Initialize()
is excluded to skip the built-in codec registration.
Since AnotherCodec
doesn't have a default constructor, the NewCodec(Type)
method is overridden to supply the required parameters.
public class ExplicitCodecFactory : DefaultCodecFactory
{
protected override void Initialize()
{
// Load specific codecs in a specific order.
PutCodecType(typeof(MyCodec));
PutCodecType(typeof(AnotherCodec));
}
protected override Codec NewCodec(Type type)
{
// Special case: AnotherCodec has a required dependency
if (typeof(AnotherCodec).Equals(type))
return new AnotherCodec(new SomeDependency());
return base.NewCodec(type);
}
}
// Register the factory at application start up.
Codec.SetCodecFactory(new ExplicitCodecFactory());
See the Lucene.Net.Codecs namespace documentation for more examples of how to inject dependencies into Codec subclasses.
Use Reflection to Scan an Assembly for Codecs
ScanForCodecs(Assembly) or ScanForCodecs(IEnumerable<Assembly>) can be used
to scan assemblies using .NET Reflection for codec types and add all subclasses that are found automatically.
This example calls base.Initialize()
to load the default codecs prior to scanning for additional codecs.
public class ScanningCodecFactory : DefaultCodecFactory
{
protected override void Initialize()
{
// Load all default codecs
base.Initialize();
// Load all of the codecs inside of the same assembly that MyCodec is defined in
ScanForCodecs(typeof(MyCodec).Assembly);
}
}
// Register the factory at application start up.
Codec.SetCodecFactory(new ScanningCodecFactory());
Codecs in the target assemblie(s) can be excluded from the scan by decorating them with the ExcludeCodecFromScanAttribute.
Inherited Members
Namespace: Lucene.Net.Codecs
Assembly: Lucene.Net.dll
Syntax
public class DefaultCodecFactory : NamedServiceFactory<Codec>, ICodecFactory, IServiceListable
Constructors
| Improve this Doc View SourceDefaultCodecFactory()
Creates a new instance of DefaultCodecFactory.
Declaration
public DefaultCodecFactory()
Properties
| Improve this Doc View SourceAvailableServices
Gets a list of the available Codecs (by name).
Declaration
public virtual ICollection<string> AvailableServices { get; }
Property Value
Type | Description |
---|---|
System.Collections.Generic.ICollection<System.String> | A ICollection{string} of Codec names. |
CustomCodecTypes
An array of custom Codec-derived types to be registered. This property can be initialized during construction of DefaultCodecFactory to make your custom codecs known to Lucene.
These types will be registered after the default Lucene types, so if a custom type has the same name as a Lucene Codec (via CodecNameAttribute) the custom type will replace the Lucene type with the same name.
Declaration
public IEnumerable<Type> CustomCodecTypes { get; set; }
Property Value
Type | Description |
---|---|
System.Collections.Generic.IEnumerable<System.Type> |
Methods
| Improve this Doc View SourceGetCodec(String)
Gets the Codec instance from the provided name
.
Declaration
public virtual Codec GetCodec(string name)
Parameters
Type | Name | Description |
---|---|---|
System.String | name | The name of the Codec instance to retrieve. |
Returns
Type | Description |
---|---|
Codec | The Codec instance. |
GetCodec(Type)
Gets the Codec instance from the provided type
.
Declaration
protected virtual Codec GetCodec(Type type)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | The System.Type of Codec to retrieve. |
Returns
Type | Description |
---|---|
Codec | The Codec instance. |
GetCodecType(String)
Gets the Codec System.Type from the provided name
.
Declaration
protected virtual Type GetCodecType(string name)
Parameters
Type | Name | Description |
---|---|---|
System.String | name | The name of the Codec System.Type to retrieve. |
Returns
Type | Description |
---|---|
System.Type | The Codec System.Type. |
Initialize()
Initializes the codec type cache with the known Codec types.
Override this method (and optionally call base.Initialize()
) to add your
own Codec types by calling PutCodecType(Type)
or ScanForCodecs(Assembly).
If two types have the same name by using the CodecNameAttribute, the last one registered wins.
Declaration
protected override void Initialize()
Overrides
NewCodec(Type)
Instantiates a Codec based on the provided type
.
Declaration
protected virtual Codec NewCodec(Type type)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | The System.Type of Codec to instantiate. |
Returns
Type | Description |
---|---|
Codec | The new instance. |
PutCodecType(Type)
Adds a Codec type to the Lucene.Net.Codecs.DefaultCodecFactory.codecNameToTypeMap, using the name provided in the CodecNameAttribute, if present, or the name of the codec class minus the "Codec" suffix as the name by default.
Note that if a Codec with the same name already exists in the map, calling this method will update it to the new type.
Declaration
protected virtual void PutCodecType(Type codec)
Parameters
Type | Name | Description |
---|---|---|
System.Type | codec | A type that subclasses Codec. |
ScanForCodecs(IEnumerable<Assembly>)
Scans the given assemblies
for subclasses of Codec
and adds their names to the Lucene.Net.Codecs.DefaultCodecFactory.codecNameToTypeMap. Note that names will be
automatically overridden if the Codec name appears multiple times - the last match wins.
Declaration
protected virtual void ScanForCodecs(IEnumerable<Assembly> assemblies)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.IEnumerable<System.Reflection.Assembly> | assemblies | A list of assemblies to scan. The assemblies will be scanned from first to last, and the last match for each Codec name wins. |
ScanForCodecs(Assembly)
Scans the given assembly
for subclasses of Codec
and adds their names to the Lucene.Net.Codecs.DefaultCodecFactory.codecNameToTypeMap. Note that names will be
automatically overridden if the Codec name appears multiple times - the last match wins.
Declaration
protected virtual void ScanForCodecs(Assembly assembly)
Parameters
Type | Name | Description |
---|---|---|
System.Reflection.Assembly | assembly | The assembly to scan. |