Domain Factory .NET Code Examples
| Class Code | Class Description | Test Code | Test Description | |
| Select a content section anchor button and then use the Browser’s back button to return to selections | ||||
Technology Stack Code Example Details
A .NET Code Example for the Domain Factory
The Factory Component has the responsibility to process the injected TechStackEntity State Object that is returned from the Repository Component and return a.
The Factory Component has the primary module responsibility of manufacturing its collections of Common Object Metadata, Tech Stack, Tech Stack Type Properties along with creating the related Stack Technologies and DevOps related technologies lists from the State Objects sent to it be a call to the Domain Repository Component.
When instantiated a MongoDB BsonObjectId is created as the Globally Unique Identifier for the Aggregate Root Domain Entity as defined by the Domain Driven Design specifications.
The MongoDB BsonObjectId becomes the Tech Stacks Cache dictionary’s Key/Value pair’s Value and paired with the Tech Stack Type string name as the dictionary’s Key in the Tech Stacks Cache dictionary of <string, BsonObjectId>.
A call is made to its Repository Class injecting the TechStackEntity State Object for population be either the Domain’s In-memory Cache or the Data Services Layer’s Entity Framework ORM.
After the TechStackEntity State Object is populated, as a Command, the injected empty TechStackEntityResponse Object along with the populated TechStackEntity State Object are sent to Class Helper Methods.
As the final actions of the CQS Command the Helper Methods transforms TechStackEntity State Object data into the requested TechStackEntityResponse Object.
The Services Module Component can now return the fully processed TechStackEntityResponse Object for the requesting Client’s Query for consumption.
The Tech Stack Factory Class:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
using AAP.LAD.Domain.TechStacks.Models.Entities; using AAP.LAD.Domain.TechStacks.Repositories; using AAP.SharedKernel.DTOs; namespace AAP.LAD.Domain.TechStacks.Factories { public sealed class TechStackFactory { private TechStackRepositories _techStackRepositories { get; set; } public TechStackFactory(string serverName = "local", string databaseName = "cdms-lad", string collectionName = "techStacks") { _techStackRepositories = new TechStackRepositories(serverName, databaseName, collectionName); } public void PopulateTechStack(TechStackEntity techStackEntity, TechStackEntityResponse response) { _techStackRepositories.GetTechStackEntity(techStackEntity); PopulateTechStackInfo(techStackEntity, response); PopulateTechStackTypeInfo(techStackEntity, response); response.TechStackInformation.StackTechnologyList = techStackEntity.StackTechnologyList; response.TechStackInformation.TechDevOpsList = techStackEntity.TechDevOpsList; PopulateTechStackBaseInfo(techStackEntity, response); } #region Helpers private static void PopulateTechStackInfo(TechStackEntity techStackEntity, TechStackEntityResponse response) { response.TechStackInformation.TechStack.StackName = techStackEntity.TechStack.StackName; response.TechStackInformation.TechStack.StackDescription = techStackEntity.TechStack.StackDescription; response.TechStackInformation.TechStack.StackOrder = techStackEntity.TechStack.StackOrder; response.TechStackInformation.TechStack.StackLogoEmblem = techStackEntity.TechStack.StackLogoEmblem; } private static void PopulateTechStackTypeInfo(TechStackEntity techStackEntity, TechStackEntityResponse response) { response.TechStackInformation.StackType.StackTechnologyType = techStackEntity.StackType.StackTechnologyType; response.TechStackInformation.StackType.StackTypeDescription = techStackEntity.StackType.StackTypeDescription; response.TechStackInformation.StackType.StackTypeStackOrder = techStackEntity.StackType.StackTypeStackOrder; response.TechStackInformation.StackType.StackTypeLogoEmblem = techStackEntity.StackType.StackTypeLogoEmblem; } private static void PopulateTechStackBaseInfo(BaseResponseDTO techStackEntity, TechStackEntityResponse response) { response.TechStackInformation.RequestTimeStamp = techStackEntity.RequestTimeStamp; response.TechStackInformation.SessionId = techStackEntity.SessionId; response.TechStackInformation.TransactionId = techStackEntity.TransactionId; response.TechStackInformation.UserId = techStackEntity.UserId; response.TechStackInformation.CacheExpirationTime = techStackEntity.CacheExpirationTime; response.TechStackInformation.ResponseTimeStamp = techStackEntity.ResponseTimeStamp; response.TechStackInformation.CacheKey = techStackEntity.CacheKey; response.TechStackInformation.DTODeliveryDurationInMilliseconds = techStackEntity.DTODeliveryDurationInMilliseconds; response.TechStackInformation.DataAccessDurationInMilliseconds = techStackEntity.DataAccessDurationInMilliseconds; response.TechStackInformation.ErrorList = techStackEntity.ErrorList; response.TechStackInformation.IsCacheEnabled = techStackEntity.IsCacheEnabled; response.TechStackInformation.IsCached = techStackEntity.IsCached; response.TechStackInformation.IsDataFromCached = techStackEntity.IsDataFromCached; response.TechStackInformation.IsLoggingEnabled = techStackEntity.IsLoggingEnabled; response.TechStackInformation.Success = techStackEntity.Success; response.TechStackInformation.SystemMessage = techStackEntity.SystemMessage; } #endregion } } |
The Tech Stack Factory Class Code Description:
The Using Statements:
The Namespace of Advanced Application Platform [AAP] and Laser Application Delivery [LAD] represents the Hybrid .NET / MEAN.js framework solution: AAP.LAD.
We are working in the Domain Layer
with strict Separation of Concerns and Data Hiding
The Factory Class only has knowledge of its next lower module, the Repository, the Entity State Objects and the Shared Kernel Base DTO for its Exception Handling and Metadata Properties.
The Factory Module Component queries for State Data from the Repository Module but does not have a “Need to Know” for how or where the data is sourced.
The Class Constructor:
The Tech Stack Factory class uses a “Parameterized” constructor to pass the MongoDB Server, Database and Collection names.
These parameters are used to create the Tech Stack Repository using the correct instance of MongoDB.
Default parameters are in place for the
local server with a default
database and collection assignment
but it is best practice to explicitly
define the MongoDB
connection string information
The Example Method:
The PopulateTechStack() method processes the injected TechStackEntity and TechStackEntityResponse objects.
The TechStackEntityResponse object is populated for final processing by the calling Domain Service Module as a Command and then returning a Void.
As before. a Return of Void
is in the CQS Principle Requirements
A Void return is possible because the Dependency Injected Response Object is “In-scope” and populated for the Services Module when returned to the Services class.
The Helper Methods:
The Factory Helper Methods encapsulate to functionality, using the Open/Closed Principle, of the data transformation activities for the Tech Stack Info, Tech Stack Type and the Base Class Metadata for Exception Handling and Request Object’s State Parameters.
The Open /Closed Principle is used here to “Open” the possibility to change the “Transformation Results” without changing the Tech Stack Factory’s calling PopulateTechStack() method.
The Calling Method is Closed for Modification
but Open for Changing the results
through the Class Helper Methods
The Tech Stack Factory Class Test Suite:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
using System; using AAP.LAD.Domain.TechStacks.Factories; using AAP.LAD.Domain.TechStacks.Models.Entities; using Microsoft.VisualStudio.TestTools.UnitTesting; using MongoDB.Bson; namespace AAP.LAD.TestSuite.Domain.TechStacks.Factories { public class TechStackFactoryTests { [TestClass] public sealed class GivenIWantToTestTheTechStackFactoryClass { private TechStackFactory _techStackFactory { get; set; } public GivenIWantToTestTheTechStackFactoryClass() { _techStackFactory = new TechStackFactory(); } #region Happy Tests [TestMethod] public void WhenICallThePopulateTechStackMethodWithAGoodParameter_ThenIWillValidateTheResult() { // Arrange const string stackTechnologyType = "MEAN"; var techStackEntity = new TechStackEntity(stackTechnologyType) { StackType = { StackTechnologyType = stackTechnologyType } }; var response = new TechStackEntityResponse(stackTechnologyType); _techStackFactory.PopulateTechStack(techStackEntity, response); // Act var result = response; // Assert Assert.IsNotNull(result); Console.WriteLine(); Console.WriteLine("Tech Stack Entity: " + result.ToJson()); Console.WriteLine(); } [TestMethod] public void WhenICallThePopulateTechStackMethodWithEntityInCache_ThenIWillValidateTheCacheResult() { // Arrange const string stackTechnologyType = "MEAN"; var techStackEntity = new TechStackEntity(stackTechnologyType) { StackType = { StackTechnologyType = stackTechnologyType } }; var response = new TechStackEntityResponse(stackTechnologyType); _techStackFactory.PopulateTechStack(techStackEntity, response); var response2 = new TechStackEntityResponse(stackTechnologyType); _techStackFactory.PopulateTechStack(techStackEntity, response2); // Act var result = response2; // Assert Assert.IsNotNull(result); Console.WriteLine(); Console.WriteLine("Tech Stack Entity: " + result.ToJson()); Console.WriteLine(); } #endregion } } } |
The Tech Stack Factory Class Test Suite Description:
The Test Class Constructor:
We create an instance of the Class Under Test (CUT) as a Private Encapsulated Property: _TechStackFactory within the Gherkin Language naming convention:
Given | When | Then
The Test Method Arrange:
We select the value for the Stack Type: “MEAN”.
We then create the Tech Stack Entity Object and set the StackTechnologyType to the selected Stack Type: “MEAN”.
An empty TechStackEntityResponse object is instantiated for injection into the Method Under Test (MUT), along with the created TechStackEntity object as a Command Void response.
We call the Method Under Test: _techStackFactory.PopulateTechStack(techStackEntity, response) in this section rather than the ACT Section due to the Void return type.
We will Assert against the newly populated injected TechStackEntityResponse Object.
The Test Method Act:
Since this is a Command with a Void return type we simply set the Results variable to the populated TechStackEntityResponse Object for Assertions.
The Test Method Asserts:
We evaluate the results by checking for a null response and convert the POCO Object to JSON for viewing in the Test Console as it would be seen by the Microservice and the Client.
We could validate all of the relevant properties as we did in the higher Services module but that would be redundant as that test validates the Factories results.
Wisdom Pearl # 140 – Tools of the Trade
The Right Technology Tool
… For the Right Technology Solution Job
…… Always be Diligent in Keeping Your Development Toolbox Up to Date
The following two tabs change content below.
I am a Principal Architect at Liquid Hub in the Philadelphia area specializing in Agile Practices as a Certified Scrum Master (CSM). I use Test Driven Development (TDD) and Acceptance Test Driven Development (ATDD) with Behavior Driven Development (BDD) as my bridge to Agile User Stories Acceptance Criteria in a Domain Driven Design (DDD) implementing true RESTful services
Latest posts by Brad Huett (see all)
- DevOps: A Bridge to Your DevOps Culture - March 25, 2016
- Embracing Test Driven Development (TDD) - March 25, 2016
- DevOps: Delivering Agile Projects - March 25, 2016


