UML

enter image description here


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

  1. The user fills out the form, indicating the details of the testing participant ( Particip ) - surname, name and patronymic;
  2. After clicking “Add”, AngularJS transfers to the Api-method partially (the Code property is not specified) the created Particip object and login ( User.Identity.Name );
  3. 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?

  • 2
    This "preprocessing logic" is usually called business logic, and it is customary to put it in a layer between the controller and the data access layer (which you have in the form of repositories). Pulling repositories directly from the controllers is permissible only in small projects or in mini-examples where there is no logic business. - PashaPash
  • @PashaPash, i.e. I'm not so bad at all, right? After all, my class ParticipCreator is once again located between these two comrades. - Adam
  • yes, something like this - PashaPash

0