如何处理与多个实体(服务)相关的表单?

发布于 2025-01-18 23:58:39 字数 2963 浏览 2 评论 0原文

在我的应用程序中,我使用工作单元和存储库模式。
我创建了这样的服务来管理 CRUD 操作

public DepartmentService(IUnitOfWork unitOfWork, IMapper mapper):base(unitOfWork, mapper)
{
}
public async Task<IEnumerable<DepartmentDto>> GetAllDepartmentsAsync()
{
    var department = await unitOfWork.Departments.GetAllAsync();
    return mapper.Map<IEnumerable<DepartmentDto>>(department);
}
public async Task<bool> InsertDepartmentAsync(DepartmentDto departmentDto)
{
    var department = mapper.Map<Department>(departmentDto);
    return await unitOfWork.Departments.AddAsync(department);
}
public async Task<bool> UpdateDepartmentAsync(DepartmentDto departmentDto,int id)
{
}
public async Task<bool> RemoveDepartmentAsync(DepartmentDto departmentDto,int id)
{
}
public async Task<int> CompleteAsync()
{
    return await unitOfWork.CompleteAsync();
}

,在部门表单中,我像这样使用它

private readonly DepartmentService departmentService;
public Frm_Department(DepartmentService departmentService)
{
    InitializeComponent();
    this.departmentService= departmentService;
}

现在我有一个员工表单,其中包含部门的文本框和组合框。
我的问题是在这种情况下,我是否在员工表格中注入服务(员工服务、部门服务)还是有其他方法可以做到这一点?当有两个以上的服务时怎么样?

public EmployeeService(IUnitOfWork unitOfWork, IMapper mapper):base(unitOfWork, mapper)
{
}
public async Task<IEnumerable<EmployeeDto>> GetAllEmployeesDetailsAsync()
{
    var employee = await unitOfWork.Employees.GetAllIncluding(
                    emp => emp.FkCountryNavigation,
                    emp => emp.FkDepartmentNavigation,
                    emp => emp.FkGenderNavigation,
                    emp => emp.FkJobTitleNavigation,
                    emp => emp.FkMaritalStatusNavigation,
                    emp => emp.FkProvincesNavigation,
                    emp => emp.FkTownNavigation);
    return mapper.Map<IEnumerable<EmployeeDto>>(employee);
}
public async Task<bool> InsertEmployeeAsync(EmployeeDto employeeDto)
{
    var employee = mapper.Map<Employee>(employeeDto);
    return await unitOfWork.Employees.AddAsync(employee);
}
    public async Task<int> CompleteAsync()
{
    return await unitOfWork.CompleteAsync();
}

这是 EmployeeDto

public partial class EmployeeDto
{
 public string IdEmployee { get; set; }
 public string FirstNameEmployee { get; set; }
 public string LastNameEmployee { get; set; }

 public virtual DepartmentDto FkDepartmentNavigationDto { get; set; }
 public virtual GenderDto FkGenderNavigationDto { get; set; }
 public virtual JobTitleDto FkJobTitleNavigationDto { get; set; }
 public virtual MaritalStatusDto FkMaritalStatusNavigationDto { get; set; }
 public virtual CountryDto FkCountryNavigationDto { get; set; }
 public virtual ProvinceDto FkProvincesNavigationDto { get; set; }
 public virtual TownDto FkTownNavigationDto { get; set; }
}

In my application I use a unit of work and repository pattern.
I have created services to manage crud operations like this

public DepartmentService(IUnitOfWork unitOfWork, IMapper mapper):base(unitOfWork, mapper)
{
}
public async Task<IEnumerable<DepartmentDto>> GetAllDepartmentsAsync()
{
    var department = await unitOfWork.Departments.GetAllAsync();
    return mapper.Map<IEnumerable<DepartmentDto>>(department);
}
public async Task<bool> InsertDepartmentAsync(DepartmentDto departmentDto)
{
    var department = mapper.Map<Department>(departmentDto);
    return await unitOfWork.Departments.AddAsync(department);
}
public async Task<bool> UpdateDepartmentAsync(DepartmentDto departmentDto,int id)
{
}
public async Task<bool> RemoveDepartmentAsync(DepartmentDto departmentDto,int id)
{
}
public async Task<int> CompleteAsync()
{
    return await unitOfWork.CompleteAsync();
}

And in Department Form I use it like this

private readonly DepartmentService departmentService;
public Frm_Department(DepartmentService departmentService)
{
    InitializeComponent();
    this.departmentService= departmentService;
}

Now I have an Employee Form that contains text boxes and combo box for the Department.
My question is in this situation do I inject services in Employee Form (Employee Service, Department Service) or is there another way to do it? and how about when there are more than two services ?

public EmployeeService(IUnitOfWork unitOfWork, IMapper mapper):base(unitOfWork, mapper)
{
}
public async Task<IEnumerable<EmployeeDto>> GetAllEmployeesDetailsAsync()
{
    var employee = await unitOfWork.Employees.GetAllIncluding(
                    emp => emp.FkCountryNavigation,
                    emp => emp.FkDepartmentNavigation,
                    emp => emp.FkGenderNavigation,
                    emp => emp.FkJobTitleNavigation,
                    emp => emp.FkMaritalStatusNavigation,
                    emp => emp.FkProvincesNavigation,
                    emp => emp.FkTownNavigation);
    return mapper.Map<IEnumerable<EmployeeDto>>(employee);
}
public async Task<bool> InsertEmployeeAsync(EmployeeDto employeeDto)
{
    var employee = mapper.Map<Employee>(employeeDto);
    return await unitOfWork.Employees.AddAsync(employee);
}
    public async Task<int> CompleteAsync()
{
    return await unitOfWork.CompleteAsync();
}

This is EmployeeDto

public partial class EmployeeDto
{
 public string IdEmployee { get; set; }
 public string FirstNameEmployee { get; set; }
 public string LastNameEmployee { get; set; }

 public virtual DepartmentDto FkDepartmentNavigationDto { get; set; }
 public virtual GenderDto FkGenderNavigationDto { get; set; }
 public virtual JobTitleDto FkJobTitleNavigationDto { get; set; }
 public virtual MaritalStatusDto FkMaritalStatusNavigationDto { get; set; }
 public virtual CountryDto FkCountryNavigationDto { get; set; }
 public virtual ProvinceDto FkProvincesNavigationDto { get; set; }
 public virtual TownDto FkTownNavigationDto { get; set; }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

森林很绿却致人迷途 2025-01-25 23:58:39

好吧,如果您想以某种方式将逻辑与表格分开,您可以将所有形式视为视图,仅此而已。使其仅将其控件(文本框,标签,按钮..所有)视为普通属性。制作一个新文件夹,其中包含每种表格逻辑的类。
每个班级都有一个构造函数,该构造函数接受表格和服务将被使用的服务。您所要做的就是用事件处理程序方法填充这些类别的裸露属性事件,请订阅构造函数中的这些事件

(例如 - &gt; _form.enterbutton.click += youreventhandler ..重复
对于每个裸露的控制)

,并最终使用这些事件处理程序方法中的服务来编写您的逻辑。插入表单内部所需的逻辑/更改后,您可以简单地制作一个属性,将表格返回并使用它。

Well if you want to separate your logic from the forms somehow, you can think of all of your forms as just views and nothing more. Make it so that the forms expose only their controls (textboxes, labels, buttons .. everything) as plain properties. Make a new folder containing classes for the logic of each of your forms.
Each class will have a constructor that accepts a form and the services that will be used by it. All you are left to do is to populate those classes with event handler methods for the events of the exposed control properties, subscribe to those events in the constructor

(for example-> _form.EnterButton.Click += YourEventHandler .. repeat
that for each exposed control)

and finally use the services inside those event handler methods to write your logic. After inserting the needed logic/changes inside the form you can simply make a property that returns the form back and use it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文