The first wave of advanced DDD combat (7): Develop a direct sales system for the general business of the big health industry (implement product context interface and testing)

In the previous article, we introduced how to organize the domain logic of creating products and the persistent storage of products through the use case of listing products to complete a function. In actual projects, various front-end forms such as PC Web,

WeChat applet, native APP, etc. need to call back-end functions. Usually, the back-end functions should be packaged in RESTFUL style, so that the front-end can use Http Get or Post to call the back-end functions. So in this article, we will complete the back-end first.

Asp.net Core WebApi, which exposes the functions of the listed products through WebApi .

Implement the upper and lower product interfaces:

[Produces("application/json")]
    [Route("api/Product")]
    public class ProductController : Controller
    {
        ServiceLocator servicelocator = new ServiceLocator();
        [HttpPost]
        [Route("AddProduct")]
        public ResultEntity<bool> AddProduct([FromBody] AddProductSPUDTO addproductspudto)
        {
            var result = new ResultEntity<bool>();
            var productdbcontext = servicelocator.GetService<IProductContext>();
            var irepository = servicelocator.GetService<IRepository>(new ParameterOverrides { { "context", productdbcontext } });
            var iproductrepository=servicelocator.GetService<IProductRepository>(new ParameterOverrides { { "context", productdbcontext } });
            var addproductspuusecase = new AddProductSPUUseCase(irepository,iproductrepository);
            try
            {
                result = addproductspuusecase.AddProduct(addproductspudto);
                result.IsSuccess = true;
                result.Count = 1;
                result.Msg = " Successful product launch! " ;
            }
            catch(Exception error)
            {
                result.ErrorCode = 100;
                result.Msg = error.Message;
            }
            return result;
        }
    }

 

1. First of all, you can see that the interface layer is a very thin layer. It does not contain business logic and data access. It just initializes some objects, then completes the invocation of application services and returns objects in the format required by the front end.

2. The product data access context, warehousing interface, product context warehousing interface, etc. need to obtain specific implementation classes through the dependency injection framework. The dependency injection framework can use the one that comes with Asp.net Core or the framework such as Unity. The dependency injection framework is omitted here

The specific implementation can be viewed in the official account.

3. If an exception may be thrown when calling the application service, it is necessary to specify each catch and the thrown content in detail.

 

When the backend interface is completed, as a backend developer, we need to write unit tests to complete the call to the backend interface and try to get the desired result. We use MSTest here, you can also use XUnit.

On-shelf product unit test:

 

HttpClient httpclient;       

        [TestMethod]
        public void AddProductTest()
        {
            httpclient = new HttpClient();
            var addproductspudto = new AddProductSPUDTO();
            addproductspudto.SPUName = " XXX Pomegranate Dew " ;
            addproductspudto.SPUDesc = " XXX Essence Pomegranate Dew for Health " ;
            addproductspudto.SKUSpecs = new List<string>();
            addproductspudto.SKUSpecs.Add( " 50ml per bottle " );
            addproductspudto.SKUSpecs.Add( " 100ml per bottle " );
            addproductspudto.SKUUnits = new List<string>();
            addproductspudto.SKUUnits.Add("");
            addproductspudto.SKUUnits.Add("");
            addproductspudto.SKUPvs = new List<decimal>();
            addproductspudto.SKUPvs.Add(120);
            addproductspudto.SKUPvs.Add(300);
            addproductspudto.SKUDealerPrices = new List<decimal>();
            addproductspudto.SKUDealerPrices.Add(3000);
            addproductspudto.SKUDealerPrices.Add(4000);
            var fs = new FileStream(@"c:\test.jpg", FileMode.Open, FileAccess.Read);
            var imgbytes = new byte[fs.Length];
            fs.Read(imgbytes, 0, Convert.ToInt32(fs.Length));
            fs.Close();
            addproductspudto.SKUImages = new List<byte[]>();
            addproductspudto.SKUImages.Add(imgbytes);
            addproductspudto.SKUImages.Add(imgbytes);

            string json = JsonConvert.SerializeObject(addproductspudto);
            HttpContent httpcontent = new StringContent(json);
            httpcontent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            var response = httpclient.PostAsync("http://localhost:2209/api/Product/AddProduct", httpcontent).Result;
            var responsevalue = response.Content.ReadAsStringAsync().Result;
            var responsemsg = JsonConvert.DeserializeObject<ResultEntity<bool>>(responsevalue).Msg;
            Assert.AreEqual( " Successful product launch! " , responsemsg);
        }
[TestMethod]
        public void AddProductTest()
        {
            httpclient = new HttpClient();
            var addproductspudto = new AddProductSPUDTO();
            addproductspudto.SPUName = "XXX面膜";
            addproductspudto.SPUDesc = " XXX mask for skincare " ;
            addproductspudto.SKUSpecs = new List<string>();
            addproductspudto.SKUSpecs.Add( " 5 sheets per box " );
            addproductspudto.SKUSpecs.Add( " 10 sheets per box " );
            addproductspudto.SKUUnits = new List<string>();
            addproductspudto.SKUUnits.Add("");
            addproductspudto.SKUUnits.Add("");
            addproductspudto.SKUPvs = new List<decimal>();
            addproductspudto.SKUPvs.Add(200);
            addproductspudto.SKUPvs.Add(350);
            addproductspudto.SKUDealerPrices = new List<decimal>();
            addproductspudto.SKUDealerPrices.Add(5000);
            addproductspudto.SKUDealerPrices.Add(8000);
            var fs = new FileStream(@"c:\test1.jpg", FileMode.Open, FileAccess.Read);
            var imgbytes = new byte[fs.Length];
            fs.Read(imgbytes, 0, Convert.ToInt32(fs.Length));
            fs.Close();
            addproductspudto.SKUImages = new List<byte[]>();
            addproductspudto.SKUImages.Add(imgbytes);
            addproductspudto.SKUImages.Add(imgbytes);

            string json = JsonConvert.SerializeObject(addproductspudto);
            HttpContent httpcontent = new StringContent(json);
            httpcontent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            var response = httpclient.PostAsync("http://localhost:2209/api/Product/AddProduct", httpcontent).Result;
            var responsevalue = response.Content.ReadAsStringAsync().Result;
            var responsemsg = JsonConvert.DeserializeObject<ResultEntity<bool>>(responsevalue).Msg;
            Assert.AreEqual( " Successful product launch! " , responsemsg);
        }

 

With unit testing, we backend developers can verify that the backend interface works with the entire use case, and unit testing can also be used as part of a daily automated build.

QQ discussion group: 309287205

Please pay attention to the WeChat public account for the advanced video of DDD actual combat:

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324852015&siteId=291194637
Recommended