我的公司正从一个遗留的代码库转移到一个更现代化的平台上,我们正在向Blazor移动。我们目前只是参与ORM的和最佳实践,在项目设置方面似乎有很多相互矛盾的想法(至少从我所收集到的信息来看)。我目前的结构如下:
首先是一个名为DAL的类库--这是我们的“数据层”。我们正在使用Dapper,这是相对简单的。示例类如下所示:
public class Person
{
public string Id {get; set;}
public string FirstName {get; set;}
public string LastName {get; set;}
public Person() {}
public Person(DbContext context) {}
public void GetPerson(int id) {}
public void DeletePerson(int id) {}
etc....
}第二个项目是一个服务器Blazor,它引用项目DAL。该项目按如下方式划分:
一个例子可能是这样的:
public class EmployeeModel
{
public int Id {get; set;}
public int Department{get; set;}
public DateTime HireDate {get; set;}
public decimal Salary {get; set;}
public Person {get; set;}
}public class EmployeeService
{
private DbContext _dbContext = dbContext;
public EmployeeService(DbContext dbContext)
{
_dbContext = dbContext;
}
public Task<EmployeeModel> Get(int id)
{
var personRepository = new Person(_dbContext);
var person = personRepository.Get(id);
Id = id;
if (id > 10)
Department = "Accounting"
etc...
}
public Task<int>CalculateBonus(DateTime hireDate, string department, decimal salary)
{
//logic here...
}
}服务和dbcontext都是通过startup.cs通过依赖注入生成的。页面类将使用以下内容加载数据:
@code{
[Parameter]
int EmployeeId;
public Employee employee;
public decimal bonus;
protected override OnAfterRenderAsync(bool firstRender)
{
if (!firstRender)
return;
employee = EmployeeService.Get(EmployeeId);
}
public void GetBonus()
{
if (employee != null)
bonus = EmployeeService.CalculateBonus(employee.HireDate, employee.Department, employee.Salary)
}
}到目前为止,这似乎还不错,但是有很多不同的解释。例如,我喜欢使用MVVM模式的想法。我最初遵循的一个示例如下:https://itnext.io/a-simple-mvvm-implementation-in-client-side-blazor-8c875c365435
但是,我并没有看到将Model/ViewModel分离出去的目的,因为他们似乎在做相同的事情,但只是在应用程序中的不同区域。我在网上也找不到这个实现的任何其他例子,所以我认为我走错了路,一开始就放弃了,但在这个过程中还早到可以尝试一下这个方法。例如,在这个方法中,EmployeeService类看起来是这样的:
public class EmployeeService
{
private EmployeeModel _employeeModel;
public EmployeeService(EmployeeModel employeeModel)
{
_employeeModel = employeeModel;
}
private EmployeeModel currentEmployee;
public EmployeeModel CurrentEmployee
{
get { return currentEmployee}
}
{
set {currentEmployee = value; }
}
public Task<EmployeeModel> Get(int id)
{
currentEmployee = EmployeeModel.Get(id);
}
public Task<int>CalculateBonus()
{
//logic implemented here with properties instead of parameters...
}
}然后,在页面上,它将如下所示:
@code{
[Parameter]
int EmployeeId;
public decimal bonus;
protected override OnAfterRenderAsync(bool firstRender)
{
if (!firstRender)
return;
EmployeeService.Get(EmployeeId); //reference Employee on page with EmployeeService.CurrentEmployee
}
public void GetBonus()
{
bonus = EmployeeService.CalculateBonus();
}
}考虑到我是如何长时间使用遗留代码的,而且没有人会告诉我其他情况,我只想知道我做得对。这是特别正确的,因为这应该是我们业务的核心前进,我不想结束意大利面条代码或做一个完整的重构线。
我想我的问题如下:
1. Does my current implementation of Blazor "work"? Or are there any other better design practices I can follow? I like MVVM, but I really just don't see the value in any implementations I've seen so far. What is the point of having a page call a function on a ViewModel, that just calls a function in another class with the same name/parameters? Wouldn't it make sense to cut out the middle man so to speak?
2. Are there any sample enterprise projects that I could follow to get a better idea of what to do here? As I've stated, there is no one senior at my company to go to regarding any of this. I am just trying to make it as adaptable to change/professional as possible.
提前感谢您的帮助!
发布于 2019-12-31 04:37:26
因此,我深入寻找更多的示例项目,我遇到了一个Server应用程序(https://www.c-sharpcorner.com/article/create-a-blazor-server-spa-with-dapper/)。从我能收集到的信息来看,在这里,以及在其他地方,添加一个单独的项目来处理CRUD操作似乎比它更麻烦。
我将按照这个链接中的内容来实现一些东西,并看看它是如何进行的。如果其他人在寻找灵感,这里有一些很好的例子:
https://github.com/AdrienTorris/awesome-blazor#sample-projects
FWIW,每个示例似乎都遵循这条路径,只是似乎以稍微不同的方式(ORM使用、文件夹名称等)来实现它。这意味着我需要添加更多的服务(总共至少20个),但是如果它是一个复杂的业务应用程序,我只是认为这就是猛兽的本质。
编码愉快!
发布于 2019-12-31 02:46:41
我刚刚创建了一个新的ASP .NET核心3.1项目与3个网络应用程序: MVC,Razor和Blazor。
NetLearner:https://github.com/shahedc/NetLearnerApp
我正在并行地开发所有3种功能,这样您就可以在它们中看到类似的功能。为了便于共享,我将公共项目提取到一个共享库中。
共享库包括:
下面是相应的博客文章,接下来是A周刊系列,在接下来的6个月里,将探讨26个不同的主题。
希望现在的版本对你所要求的有用。保持关注,并随时提出建议或对项目的结构提供反馈。

https://stackoverflow.com/questions/59538859
复制相似问题