- Introduction
- What's New?
- Features
- Upgrading the Design Editor
- Breaking Changes in 6.0
- Getting Started
- Integration with E-commerce
- Integration with Remote Asset Sources
- Printable Files and Proof Images
- Configuring Products
- Creating Product Templates
- User Interface
- Configuration Files
- APIs
- Add-ons
- System Requirements
- Troubleshooting
- Migrating Your Applications
- Licensing
- Copyright Notices
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 to instantiate services and pass all necessary dependencies to it. The Injector.Instance property returns an object containing the Resolve<TDependency> method, which in turn instantiates the service registered in the Design Atoms Framework. You can set up DI in your application as follows:
using Aurigma.DesignAtoms.Configuration;
Injector.CurrentFactory = () => DefaultInjectorFactory.Create();
Then, for example, to parse PSD templates and convert them to products, you can create and use an instance of the ITemplateParser interface as follows:
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);
}
}
}
}
}
}