Let's say that I have a domain assembly that describes a domain model, and it has a class called a product:
public class Product { public int Id { get; set; } public string Name { get; set; } }
I have another assembly, which is a web application working with this domain model. Now I want to create a form to create new products and get some attribute validation. The easiest way to do this is to use DataAnnotations in the class. However, this leads to the fact that the domain model now contains metadata about form validation, which is not a very clear separation of problems.
There may be a MetadataType attribute for the class, but I believe this is not better. Suddenly, your domain model class has a dependency on the form validation metadata class.
Another way is to create the CreateProductForm class and add the necessary attributes there and make a mapping between the classes. However, this creates some overhead, since you need to maintain these classes separately, and changes to one may violate the other. In some scenarios this may be desirable, but in some others it may just create additional work (suppose you have an Address class, for example).
UPDATE: some people have suggested that I use AutoMapper for this, which I already know about. AutoMapper just simplifies and simplifies the comparison, in fact does not solve the problem of saving two separate classes, which will be almost the same. My preference would be to create classes of forms only if there is a definite need for this.
Is it possible to simply declare annotations in a web assembly without creating unnecessary dependencies for the domain assembly?
source share