0

Based on the project structure below I will have some questions, but one important thing is that I'm not working with DDD

  • Domain Layer

1) MyApp.Domain.Entities (DLL)

public class Product : DataTransferObject {

    public int id { get; set; }

    public int idProducer { get; set; }

    public string Name { get; set; }

}

2) MyApp.Domain.Interfaces (DLL)

IProductRepository {

    void SaveProduct(Product Product);
    void UpdateProduct(ProductProduct);
    void RemoveProduct(int idProduct);

}
  • Service Layer

3) MyApp.Service.Interfaces (DLL)

IProductService {
    void SaveProduct(Product Product);
}

4) MyApp.Service (DLL)

ProductService : ProductValidation,  IProductService {

    void SaveProduct(Product Product) {

        this.Validate(Product);

        if(Product.Notification.HasNotifications()) {
            return Product.Notification.Messages();
        }

        //...Save in the database

    }
}

If you check I validate the object Product in my Service Layer and return the notifications to my upper Layer that is a Web API project

  • Presentation Layer

5) MyApp.WebAPI

A) JSON

public class ProductJson {
    public int id { get; set; }
    public int idProducer { get; set; }
    public string Name { get; set; }
}

B) Controllers

[HttpPost]
public IHttpActionResult SaveProduct([FromBody] ProductJson ProductJSON){

    Product Product = ProductMapping.Map(ProductJSON)

    this.ProductService.SaveProduct(Product);

    if(Product.Notification.HasNotification()){

        returnContent(PreconditionFailed,new 
            ErrorResult("412", String.Empty,Product.Notification.Messages()));
    }

    return OK("Product Saved!");
}

The main problem I face is that the client that is using my WebServices can hide int properties such as id and idProducer.

And when the Bind happens the value will be 0 and this is not the behaviour I expect.

I want to show the message idProducer is Required and not idProducer Invalid When the propertie is hidden and will be NULL!

I have read some articles telling me to just make my Entities ID nullable in my domain, but I do not like this change of behaviuor!

One important point is that I want to keep my validations at Service Layer, I do not put them at the Web Services!

The idea I had was remove ProductJSON from my Presentation Layer (make int Nullables) and put in my domain like shown Below

  • Domain Layer

1) MyApp.Domain.Entities (DLL)

public class Product {
    public int id { get; set; }
    public int idProducer { get; set; }
    public string Name { get; set; }
}

A)

public class ProductCommand {
    public int? id { get; set; }
    public int? idProducer { get; set; }
    public string Name { get; set; }
}

After that I Will change my controller like sown below

public IHttpActionResult 
    SaveProduct([FromBody] ProductJson ProductCommand){

And at least my Service

class ProductService : ProductValidation, IProductService {

    void SaveProduct(ProductCommand Product) {

        this.Validate(Product);

        if(Product.HasNotifications()){ 
            return Product.Notification.Messages(); 
        }

        //...Map ProductCommand to Product

        //...Save Product in the database
    }

Is this a good Solution? Any suggestion?

Thank you so much!

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.