---
title: "Using Dependency Injection"
canonical: https://customerscanvas.com/docs/cc6/using-dependency-injection.htm
archived: true
applies_to:
  - product: "Customer's Canvas On-Premises"
  - product: "Design Atoms .NET"
    versions: ["6.15.0", "6.x (may differ)"]
---

**You’re viewing outdated documentation.** [View the latest Customer’s Canvas On-Premises documentation](https://customerscanvas.com/docs/on-premises/).

# Using Dependency Injection

**Applies to:** Customer's Canvas On-Premises; Design Atoms .NET 6.15.0, 6.x (may differ) · **Archived documentation**
 
The Design Atoms Framework allows you to work with its components through Dependency Injection (DI). You can use a static class [Injector](https://customerscanvas.com/docs/cc6/T_Aurigma_DesignAtoms_Configuration_Injector.htm.md) to instantiate services and pass all necessary dependencies to it. The [Injector.Instance](https://customerscanvas.com/docs/cc6/P_Aurigma_DesignAtoms_Configuration_Injector_Instance.htm.md) property returns an object containing the **Resolve&lt;TDependency&gt;** method, which in turn instantiates the service registered in the Design Atoms Framework. You can set up DI in your application as follows:
 
```csharp
using Aurigma.DesignAtoms.Configuration;
Injector.CurrentFactory = () => DefaultInjectorFactory.Create();
```
 
Then, for example, to parse PSD templates and convert them to [products](https://customerscanvas.com/docs/cc6/T_Aurigma_DesignAtoms_Model_Product.htm.md), you can create and use an instance of the [ITemplateParser](https://customerscanvas.com/docs/cc6/T_Aurigma_DesignAtoms_Convert_ITemplateParser.htm.md) interface as follows:
 
```csharp
using Aurigma.DesignAtoms.Convert;
var parser = Injector.Instance.Resolve<ITemplateParser>();
var product = parser.FromFile("nameplate.psd");
```
 
In this way, you can use such components as:
 
- **IStateFileSerializer** to store product states
- **IProductRenderer** to render products
- **IFileCache** to get rendered outputs
- **IImageLoader** to load images
- **IConfiguration** to configure the viewer

## The Sample
  
This sample illustrates how you can instantiate the **IStateFileSerializer** and **IProductRenderer** interfaces to work with state files and to render products.
 
```
using System.IO;
using Aurigma.DesignAtoms.Configuration;
using Aurigma.DesignAtoms.Configuration.RenderingConfig;
using Aurigma.DesignAtoms.Model;
using Aurigma.DesignAtoms.Rendering;
using Aurigma.DesignAtoms.Serialization;
using Aurigma.DesignAtoms.Services.Logger;
using Autofac;
using Newtonsoft.Json;

namespace DesignAtomsConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            // Setting up the dependency injection.
            Injector.CurrentFactory = () => DefaultInjectorFactory.Create();
            // Setting up the serialization.
            JsonConvert.DefaultSettings = () => DefaultJsonSettings.Get(Injector.Instance.Resolve<IConfiguration>());

            CreateState(@"C:\CustomersCanvas\states\one-page.st");
            RenderState(@"C:\CustomersCanvas\states\one-page.st", @"C:\CustomersCanvas\previews\one-page.jpg");
        }

        static void CreateState(string newStateFile)
        {
            // Create a product.
            var product = new Product();
            var surface = new Surface(330, 600);
            product.Surfaces.Add(surface);

            // Instantiate IStateFileSerializer.
            var stateFileSerializer = Injector.Instance.Resolve<IStateFileSerializer>();
            var state = new Aurigma.DesignAtoms.Model.State(product);
            using (var stateStream = File.OpenWrite(newStateFile))
            {
                // Save the product to a state file.
                stateFileSerializer.Serialize(state, stateStream);
            }
        }

        static void RenderState(string stateFileName, string resultFileName)
        {
            using (var fstream = File.OpenRead(stateFileName))
            {
                // Instantiate IStateFileSerializer.
                var stateFileSerializer = Injector.Instance.Resolve<IStateFileSerializer>();
                // Load a product from a state file. 
                var state = stateFileSerializer.Deserialize(fstream);
                var product = state.Product;

                // Instantiate IProductRenderer.
                var productRenderer = Injector.Instance.Resolve<IProductRenderer>();
                using (var memoryStream = new MemoryStream())
                {
                    // Render the product to a stream.
                    productRenderer.RenderProof(memoryStream, product.Surfaces[0], RenderingConfig.GetDefault());
                    // Write the resulting memory stream to a file.
                    memoryStream.Position = 0;
                    using (FileStream outputStream = File.OpenWrite(resultFileName))
                    {
                        outputStream.Write(memoryStream.GetBuffer(), 0, (int)memoryStream.Length);
                    }
                }
            }
        }
    }
}
```
 
## See Also
  
### Manual
 
- [Design Atoms Framework Overview](https://customerscanvas.com/docs/cc6/design-atoms-fw.htm.md)
- [Working with the Object Model](https://customerscanvas.com/docs/cc6/using-design-atoms-fw.htm.md)

### Reference
 
- [Injector Class](https://customerscanvas.com/docs/cc6/T_Aurigma_DesignAtoms_Configuration_Injector.htm.md)

### Other
 
- [Design Atoms sample project↗](https://github.com/aurigma/DesignAtomsSamples)
