https://github.com/thomaslevesque/unity.extras.autofactory
A Unity extension to automatically generate strongly typed factories at runtime.
https://github.com/thomaslevesque/unity.extras.autofactory
Last synced: about 2 months ago
JSON representation
A Unity extension to automatically generate strongly typed factories at runtime.
- Host: GitHub
- URL: https://github.com/thomaslevesque/unity.extras.autofactory
- Owner: thomaslevesque
- License: apache-2.0
- Created: 2015-08-30T03:12:05.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2017-06-07T11:54:17.000Z (about 9 years ago)
- Last Synced: 2025-01-30T07:43:20.398Z (over 1 year ago)
- Language: C#
- Homepage:
- Size: 31.3 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Unity.Extras.AutoFactory
A Unity extension to automatically generate strongly typed factories at runtime.
For instance, if you have these types:
```csharp
public interface IFoo
{
int Id { get; }
string Name { get; }
}
public interface IFooFactory
{
IFoo Create(int id, string name);
}
public class Foo : IFoo
{
private readonly IMyDependency _dependency;
public int Id { get; }
public string Name { get; }
public Foo(int id, string name, IMyDependency dependency)
{
Id = id;
Name = name;
_dependency = dependency;
}
}
```
Unity.Extras.AutoFactory can automatically generate an implementation of `IFooFactory` that will create instances of `Foo` with the specified parameters and inject the required dependencies:
```csharp
var container = new UnityContainer();
// Activate extension
container.AddNewExtension();
// Register dependencies
container.RegisterType();
// Register automatic factory
// Note how the concrete IFoo type is specified
container.RegisterType(new AutoFactory());
// This will generate the appropriate factory type
var factory = container.Resolve();
var foo = factory.Create(42, "test");
```