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 tobase.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 callsbase.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
DefaultCodecFactory()
Creates a new instance of DefaultCodecFactory.
Declaration
public DefaultCodecFactory()
See Also
Properties
AvailableServices
Gets a list of the available Codecs (by name).
Declaration
public virtual ICollection<string> AvailableServices { get; }
Property Value
Type | Description |
---|---|
ICollection<string> | A ICollection{string} of Codec names. |
See Also
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 |
---|---|
IEnumerable<Type> |
See Also
Methods
GetCodec(string)
Gets the Codec instance from the provided name
.
Declaration
public virtual Codec GetCodec(string name)
Parameters
Type | Name | Description |
---|---|---|
string | name | The name of the Codec instance to retrieve. |
Returns
Type | Description |
---|---|
Codec | The Codec instance. |
See Also
GetCodec(Type)
Gets the Codec instance from the provided type
.
Declaration
protected virtual Codec GetCodec(Type type)
Parameters
Type | Name | Description |
---|---|---|
Type | type |
Returns
Type | Description |
---|---|
Codec | The Codec instance. |
See Also
GetCodecType(string)
Declaration
protected virtual Type GetCodecType(string name)
Parameters
Type | Name | Description |
---|---|---|
string | name |
Returns
Type | Description |
---|---|
Type |
See Also
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).
Declaration
protected override void Initialize()
Overrides
See Also
NewCodec(Type)
Instantiates a Codec based on the provided type
.
Declaration
protected virtual Codec NewCodec(Type type)
Parameters
Type | Name | Description |
---|---|---|
Type | type |
Returns
Type | Description |
---|---|
Codec | The new instance. |
See Also
PutCodecType(Type)
Adds a Codec type to the 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 |
---|---|---|
Type | codec | A type that subclasses Codec. |
See Also
ScanForCodecs(IEnumerable<Assembly>)
Scans the given assemblies
for subclasses of Codec
and adds their names to the 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 |
---|---|---|
IEnumerable<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. |
See Also
ScanForCodecs(Assembly)
Scans the given assembly
for subclasses of Codec
and adds their names to the 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 |
---|---|---|
Assembly | assembly | The assembly to scan. |