Class Factory<TObject>
Provides a base class of IFactory<TCreate> that supports lifetime management using scopes and FactoryMode.
Implements
Inherited Members
Namespace: BionicCode.Utilities.Net
Assembly: BionicCode.Utilities.Net.Common.dll
Syntax
public abstract class Factory<TObject> : IFactory<TObject>
Type Parameters
| Name | Description |
|---|---|
| TObject | The type of the instance to create. |
Remarks
This base class is provided to make it easier for implementers of IFactory<TCreate> to create a custom factory. Implementers are encouraged to extend this base class instead of creating their own.
Factory<TObject> provides useful features like factory scopes and other lifetime managment modes like shared instances.
The internals will handle the lifetime management based on the value of the FactoryMode property. To control instantiation of the factory products, simply implement the abstract CreateInstance() and CreateInstance(Object[]) members.
Examples
The following example shows how to implement an IFactory<TCreate> by extending the anstract Factory<TObject> clas.
It also shows how to create and use a factory scope.
public class PersonFactoryExample
{
PersonFactory PersonFactory { get; }
public FactoryTest()
{
this.PersonFactory = new PersonFactory();
}
public void FactoryExample()
{
// Configure factory to produce shared instances
this.PersonFactory.FactoryMode = FactoryMode.Singleton;
Person firstSharedInstanceInScope = null;
Person secondSharedInstanceInScope = null;
Person sharedInstanceCreatedBeforeScope = this.PersonFactory.Create();
// Create a new factory scope
using (IDisposable factoryScope = this.PersonFactory.CreateScope())
{
firstSharedInstanceInScope = this.PersonFactory.Create();
secondSharedInstanceInScope = this.PersonFactory.Create();
}
Person sharedInstanceCreatedAfterScope = this.PersonFactory.Create();
bool isReferenceEqual = ReferenceEquals(firstSharedInstanceInScope, secondShartedInstanceInScope); // true
isReferenceEqual = ReferenceEquals(firstSharedInstanceInScope, sharedInstanceCreatedBeforeScope); // false
isReferenceEqual = ReferenceEquals(secondSharedInstanceInScope, sharedInstanceCreatedBeforeScope); // false
isReferenceEqual = ReferenceEquals(firstSharedInstanceInScope, sharedInstanceCreatedAfterScope); // false
isReferenceEqual = ReferenceEquals(secondSharedInstanceInScope, sharedInstanceCreatedAfterScope); // false
isReferenceEqual = ReferenceEquals(sharedInstanceCreatedBeforeScope, sharedInstanceCreatedAfterScope); // true
// Configure factory to produce transient instances
this.PersonFactory.FactoryMode = FactoryMode.Transient;
Person firstTransientInstance = this.PersonFactory.Create();
Person secondTransientInstance = this.PersonFactory.Create();
isReferenceEqual = ReferenceEquals(firstTransientInstance, secondTransientInstance); // false
}
}
public class PersonFactory : Factory{Person}
{
protected override Person CreateInstance()
=> new Person("DefaultPersonFirstName", "DefaultPersonLastName", -1);
protected override Person CreateInstance(params object[] args)
=> new Person(args[0] as string, args[1] as string, (int)args[2]);
}
public class Person
{
public Person(string firstName, string lastName, int id)
{
this.FirstName = firstName;
this.LastName = lastName;
this.Id = id;
}
public string FirstName { get; }
public string LastName { get; }
public int Id { get; }
}
Constructors
| Improve this Doc View SourceFactory()
Initializes the Factory to create instances using Singleton.
Declaration
protected Factory()
Factory(FactoryMode)
Initializes instance.
Declaration
protected Factory(FactoryMode factoryMode)
Parameters
| Type | Name | Description |
|---|---|---|
| FactoryMode | factoryMode | Describes the created objects lifetime using FactoryMode. |
Properties
| Improve this Doc View SourceFactoryMode
Configures the lifetime scope of the created object instances. Default is Singleton
Declaration
public FactoryMode FactoryMode { get; set; }
Property Value
| Type | Description |
|---|---|
| FactoryMode | The lifetime scope Singleton of the created object instances |
SharedProductInstance
The shared instance which Factory<TObject> returns when FactoryMode is set to Singleton or Scoped.
Declaration
protected TObject SharedProductInstance { get; }
Property Value
| Type | Description |
|---|---|
| TObject |
Methods
| Improve this Doc View SourceCreate()
Creates the instance.
Declaration
public TObject Create()
Returns
| Type | Description |
|---|---|
| TObject | An instance of |
Create(Object[])
Creates the instance, allowing arguments.
Declaration
public TObject Create(params object[] args)
Parameters
| Type | Name | Description |
|---|---|---|
| Object[] | args | The arguments for the factory to use to instantiate the type. |
Returns
| Type | Description |
|---|---|
| TObject | An instance of |
CreateInstance()
Implementation to create and initialize instances of type TObject.
Declaration
protected abstract TObject CreateInstance()
Returns
| Type | Description |
|---|---|
| TObject | An instance of type |
CreateInstance(Object[])
Implementation to create and initialize instances of type TObject.
Declaration
protected abstract TObject CreateInstance(params object[] args)
Parameters
| Type | Name | Description |
|---|---|---|
| Object[] | args | A variable list of arguments. |
Returns
| Type | Description |
|---|---|
| TObject | An instance of type |
CreateScope()
Creates a scope in which created instances of the current Factory<TObject> are shared instances.
Declaration
public IDisposable CreateScope()
Returns
| Type | Description |
|---|---|
| IDisposable | An IDisposable to define the scope. |
Remarks
The instances returned from the current Factory<TObject> instance are shared instances within this scope.
This means the returned instances are the same whithin the current scope, but different from the enclosing scope.
To define the scope, simply call CreateScope() and wrap the returned IDisposable inside a langword_csharp_using statement or expression.
Examples
The following example shows how to create a factory scope. See Factory<TObject> for the complete example.
public class PersonFactoryExample
{
PersonFactory PersonFactory { get; }
public FactoryTest()
{
this.PersonFactory = new PersonFactory();
}
public void FactoryExample()
{
// Configure factory to produce shared instances
this.PersonFactory.FactoryMode = FactoryMode.Singleton;
Person firstSharedInstanceInScope = null;
Person secondSharedInstanceInScope = null;
Person sharedInstanceCreatedBeforeScope = this.PersonFactory.Create();
// Create a new factory scope
using (IDisposable factoryScope = this.PersonFactory.CreateScope())
{
firstSharedInstanceInScope = this.PersonFactory.Create();
secondSharedInstanceInScope = this.PersonFactory.Create();
}
Person sharedInstanceCreatedAfterScope = this.PersonFactory.Create();
bool isReferenceEqual = ReferenceEquals(firstSharedInstanceInScope, secondShartedInstanceInScope); // true
isReferenceEqual = ReferenceEquals(firstSharedInstanceInScope, sharedInstanceCreatedBeforeScope); // false
isReferenceEqual = ReferenceEquals(secondSharedInstanceInScope, sharedInstanceCreatedBeforeScope); // false
isReferenceEqual = ReferenceEquals(firstSharedInstanceInScope, sharedInstanceCreatedAfterScope); // false
isReferenceEqual = ReferenceEquals(secondSharedInstanceInScope, sharedInstanceCreatedAfterScope); // false
isReferenceEqual = ReferenceEquals(sharedInstanceCreatedBeforeScope, sharedInstanceCreatedAfterScope); // true
// Configure factory to produce transient instances
this.PersonFactory.FactoryMode = FactoryMode.Transient;
Person firstTransientInstance = this.PersonFactory.Create();
Person secondTransientInstance = this.PersonFactory.Create();
isReferenceEqual = ReferenceEquals(firstTransientInstance, secondTransientInstance); // false
}
}