UML
Code
class Particip { public string Code { get; set; } public string Surname { get; set; } public string Name { get; set; } public string SecondName { get; set; } } interface IParticipRepository { Particip Get(string code); Add(Particip item); } class ParticipRepository : IParticipRepository { private dbContext db = new dbContext(); public Add(Particip item) { db.Particips.Add(item); } public Particip Get(string code) { //... } } class ParticipCreator { private IParticipRepository participRepository; public ParticipCreator(IParticipRepository participRepository) { this.participRepository = participRepository; } public Particip FactoryMethod(Particip unfinishedItem, string userLogin) { var newCode = //... //алгоритм формирования кода для юзера //... unfinishedItem.Code = newCode; participRepository.Add(unfinishedItem); return unfinishedItem; } } class ParticipApiControlle : ApiController { private IParticipRepository participRepository; private ParticipCreator participCreator; public ParticipApiControlle(IParticipRepository participRepository) { this.participRepository = participRepository; this.participCreator = new ParticipCreator(participRepository); } public Particip PostParticip(Particip unfinishedItem, string userLogin) { var item = participCreator.FactoryMethod(unfinishedItem, userLogin); return item; } public Particip Get(string code) { return participRepository.Get(code); } } Explanation
- The user fills out the form, indicating the details of the testing participant (
Particip) - surname, name and patronymic; - After clicking “Add”,
AngularJStransfers to the Api-method partially (the Code property is not specified) the createdParticipobject and login (User.Identity.Name); - Now it is necessary for the Code property of the Particip object to generate a value and only then add it to the database.
The fact is that the code for the new участника-сущности БД is formed in a special way and depends on the current user login .
Question
Where exactly, according to a good design style, should the logic of generating new code be implemented? Those. Where is the best place to do preprocessing of an entity when transferring its repository?
Wherever you look, it is recommended to make Api and repositories thin and, as you can see, I implemented the “Factory Method” pattern for this and, after receiving the Particip object and login ( userLogin ), userLogin new code and then calling the Add repository method, I add in the database. Perhaps even I did not work out badly :)
I also have a request: pay attention to my UML diagram - did I build it correctly?

ParticipCreatoris once again located between these two comrades. - Adam