我总是在所有地方插入我的Linq2SQL查询,几乎每个类都在这里.
我想知道你在哪里放置Linq2SQL查询的策略是什么?
您是将它们放在单独的数据层类中还是将它们存储在遍布各处的地方?
我认为我需要改变Linq2SQL查询的策略并将它们存储到单独的DataLayer类中.我认为,如果我能够有效地进行TDD并遵守依赖注入和固体原则,那是必须的.
我想避免使用switch语句.我有30多种文档类型.还有可能我需要添加更多的文档类型.我宁愿传递IDocument并且具有在IDocument的实现中指定的类型.我忘了提到的其他东西是ProgressNoteViewModel,LabViewModel ...都继承自WorkspaceViewModel,所有具体实现构造函数都将类型IPatient作为参数.我也使用Castle作为我的IoC容器
我想将代码重构为类似的东西
viewModel = new TreeViewModel(repository.GetPatientDocumentListing(IDocumentType);
this.DocTreeViewModel = viewModel;
//How would I then be able to instantiate the right ViewModel
//based on IDocumentType and also pass a object into the
//constructor that is not know at compile time
Run Code Online (Sandbox Code Playgroud)
我有以下代码:
switch (docType)
{
case "ProgressNotes":
viewModel = new TreeViewModel(repository.GetPatientProgressNotes());
this.DocTreeViewModel = viewModel;
ProgressNoteViewModel workspace = ProgressNoteViewModel.NewProgressNoteViewModel(_patient);
break;
case "Labs":
viewModel = new TreeViewModel(repository.GetPatientLabs());
this.DocTreeViewModel = viewModel;
LabViewModel workspace = LabViewModel.NewLabViewModel(_patient);
break;
}
this.Workspaces.Add(workspace);
this.SetActiveWorkspace(workspace);
Run Code Online (Sandbox Code Playgroud) 我有YoutubeVideoService执行 CRUD(创建、读取、更新和删除)操作的类。在我看来,创建、读取、更新和删除是类更改的四个原因。这个类是否违反了单一职责原则?
如果违反,那么,我们应该有四个类,如CreateYoutubeVideoService,ReadYoutubeVideoService,UpdateYoutubeVideoService和DeleteYoutubeVideoService。有很多课程是不是有点矫枉过正?
我有为HR系统开发的系统.有会计员工和程序员员工.在加入公司的第一个月,员工没有任何角色.一名员工可以同时是会计师和程序员.我有一个由以下代码显示的设计.
现在,我需要通过实现新功能来增强系统:
终止所有会计师.(终止表示将员工的状态设置为IsActive = false).问题是我不能在没有检查的情况下将所有会计师直接设置为非活动状态.我需要检查一下他是否还有其他角色.
如何重新构造这些类以使终止函数更自然OO?
UPDATE
我正在寻找一个有@AlexDev答案的EF Database First解决方案模型和数据库架构的答案.
C#代码
List<Accountant> allAccountants = Get All accountants from database
public class Employee
{
public int EmpID { get; set; }
public DateTime JoinedDate { get; set; }
public int Salary { get; set; }
public bool IsActive { get; set; }
}
public class Accountant : Employee
{
public Employee EmployeeData { get; set; }
}
public class Programmer : Employee
{
public Employee EmployeeData { get; set; }
} …Run Code Online (Sandbox Code Playgroud) 我不确定我班内的这种方法是否违反了单一责任原则,
public function save(Note $note)
{
if (!_id($note->getid())) {
$note->setid(idGenerate('note'));
$q = $this->db->insert($this->table)
->field('id', $note->getid(), 'id');
} else {
$q = $this->db->update($this->table)
->where('AND', 'id', '=', $note->getid(), 'id');
}
$q->field('title', $note->getTitle())
->field('content', $note->getContent());
$this->db->execute($q);
return $note;
}
Run Code Online (Sandbox Code Playgroud)
基本上它在方法中执行两个作业 - 插入或更新.
我应该将其分为两种方法,而不是遵守单一责任原则吗?
但SRP仅适用于课程,不是吗?它适用于类中的方法吗?
SRP -
一个类应该只有一个责任(即软件规范中只有一个潜在的变化应该能够影响类的规范)
编辑:
另一种列出笔记(包括许多不同类型的列表),搜索笔记等的方法......
public function getBy(array $params = array())
{
$q = $this->db->select($this->table . ' n')
->field('title')
->field('content')
->field('creator', 'creator', 'id')
->field('created_on')
->field('updated_on');
if (isset($params['id'])) {
if (!is_array($params['id'])) {
$params['id'] = array($params['id']); …Run Code Online (Sandbox Code Playgroud) 在维护SOLID原则的同时,我对使用Controller with Repository Pattern感到困惑.考虑一下,我有两种类型的报价
并且未来很有可能出现新类型的报价.每个报价都有不同的字段,业务逻辑,但它们共享许多常用功能.所以我创建了一个QuotationInterface
报价界面
interface QuotationInterface
{
public function save(array $data);
}
Run Code Online (Sandbox Code Playgroud)
实现接口的报价类
class CommercialQuotation implements QuotationInterface
{
public function(array $data)
{
// save commercial quotation
}
}
class PrivateQuotation implements QuotationInterface
{
public function(array $data)
{
// save Private quotation
}
}
Run Code Online (Sandbox Code Playgroud)
报价库
class QuotationRepository
{
public function save(array $data, QuotationInterface $quotation)
{
$quotation->save($data);
}
}
Run Code Online (Sandbox Code Playgroud)
QotationController
public function store(Resource $resource)
{
$inputs = $resource->all();
/**
* Clearly here Open/Close Principle is broken
*/
if …Run Code Online (Sandbox Code Playgroud) 我有一个关于 Uncle Bob 的Clean Architecture 中的“用例输出端口”的问题 。
在图像中,鲍勃叔叔将端口描述为接口。我想知道是否有要这样或者如果调用的用例交互器还可以返回一个“简单”的价值。在任何一种情况下,应用程序和业务规则层都将定义接口适配器层必须使用的接口。所以我认为对于简单的调用,只返回一个值不会违反架构理念。
真的吗?
此外,我认为演示者实现的这个输出端口接口应该像观察者模式一样工作。演示者只是观察交互者的相关“事件”。在 .NET 事件是一等公民的情况下,我认为使用其中一个是相同的想法。
这些想法是否与 Clean Architecture 背后的想法兼容?
在实现我的课程之后,目前处于实质性的重构工作中.我试图分解一些事情,更好地遵循SRP,但我总是发现很难评估一个班级是否有"改变的一个理由"的格言.我希望这个实际的例子可以帮助我理解.
有问题的代码旨在清理数据.目前这里有两个独立的进程 - 我们通过使用通过代码调用的外部应用程序来清理地址数据.我们使用C#中的内部算法清理其他数据字段.
当我被告知我们可能希望将来更改这两个进程时,这个重构就开始了 - 例如,使用数据库存储过程来执行这两个作业而不是C#代码和外部应用程序.所以我的第一直觉是将这两个函数隐藏在接口后面(FileRow并且FileContents只是DTO):
public interface IAddressCleaner
{
string CleanAddress(StringBuilder inputAddress);
void CleanFile(FileContents fc);
}
public interface IFieldCleaner
{
string CleanPhoneNumber(string phoneToClean);
void CleanAllPhoneFields(FileRow row, FileContents fc);
void MatchObscentities(FileRow row, FileContents fc);
void CleanEmailFields(FileRow row, FileContents fc);
}
Run Code Online (Sandbox Code Playgroud)
哪个好.然而,实际上,我无法想象一个班级会在没有其他班级的情况下使用其中一个.因此将它们(及其实现)合并到一个类中似乎是有意义的.这也是有道理的,因为我们可以用一个解决方案(如数据库)替换这两个函数.
另一方面,似乎IFieldCleaner已经违反了SRP,因为它正在做三件事:清理电话号码,发送电子邮件和寻找粗鲁的话语,所有这些都是逻辑上不同的过程.所以似乎有理由把它分成一个IPhoneCleaner,IObscenityMatcher和IEmailCleaner.
对后一种方法特别困扰的是这些类在服务中使用,该服务已经具有愚蠢的接口依赖性:
public class ReadFileService : IExecutableObject
{
private ILogger _log;
private IRepository _rep;
private IFileHelper _fileHelp;
private IFieldCleaner _fieldCleaner;
private IFileParser _fileParser;
private IFileWriter _fileWriter; …Run Code Online (Sandbox Code Playgroud) c# architecture interface single-responsibility-principle solid-principles
我正在努力理解单一责任原则如何与OOP合作.
如果我们要遵循这个原则来开球,那么我们是不是留下了许多课程,其中许多课程可能只有一个方法?
如果我们不完全遵循这个原则,那么这个原则的重点是什么?
oop design-patterns single-responsibility-principle solid-principles
solid-principles ×10
oop ×6
single-responsibility-principle ×4
c# ×3
architecture ×2
dry ×1
interface ×1
interface-segregation-principle ×1
laravel ×1
linq-to-sql ×1
ooad ×1
php ×1
python ×1
tdd ×1