我总是在所有地方插入我的Linq2SQL查询,几乎每个类都在这里.
我想知道你在哪里放置Linq2SQL查询的策略是什么?
您是将它们放在单独的数据层类中还是将它们存储在遍布各处的地方?
我认为我需要改变Linq2SQL查询的策略并将它们存储到单独的DataLayer类中.我认为,如果我能够有效地进行TDD并遵守依赖注入和固体原则,那是必须的.
我不确定我班内的这种方法是否违反了单一责任原则,
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) 在实现我的课程之后,目前处于实质性的重构工作中.我试图分解一些事情,更好地遵循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
我正在研究单一职责原则(SRP)和开闭原则(OCP)。
SRP 规定,一个类必须只有一个改变的理由。OCP 规定类必须对修改关闭,但对扩展开放。
我觉得这是矛盾的。一个原则规定类必须足够简单,您可以出于单一原因进行更改,但另一条原则规定类不得更改而只能扩展。
有人有更好的解释吗?
我试图遵循接口隔离和单一职责原则,但是我对如何将它们整合在一起感到困惑。
在这里,我有一个示例,其中有一些接口,我已将其拆分为更小、更直接的接口:
public interface IDataRead
{
TModel Get<TModel>(int id);
}
public interface IDataWrite
{
void Save<TModel>(TModel model);
}
public interface IDataDelete
{
void Delete<TModel>(int id);
void Delete<TModel>(TModel model);
}
Run Code Online (Sandbox Code Playgroud)
我稍微简化了它(有一些where条款妨碍了可读性)。
目前我正在使用SQLite ,但是,这种模式的美妙之处在于,如果我选择不同的数据存储方法(例如Azure),它有望让我有机会更好地适应变化。
现在,我对每个接口都有一个实现,下面是每个接口的简化示例:
public class DataDeleterSQLite : IDataDelete
{
SQLiteConnection _Connection;
public DataDeleterSQLite(SQLiteConnection connection) { ... }
public void Delete<TModel>(TModel model) { ... }
}
...
public class DataReaderSQLite : IDataRead
{
SQLiteConnection _Connection;
public DataReaderSQLite(SQLiteConnection connection) { ... }
public …Run Code Online (Sandbox Code Playgroud) c# interface single-responsibility-principle solid-principles interface-segregation-principle
我有一个类似于下面的类(C#):
public class Product {
public int ID {get;set;}
public string Name {get;set;}
public double Price {get;set;}
public void Save() {
string sql = "INSERT INTO Product.....";
Database.Execute(sql);
}
public void Delete() {
string sql = "DELETE Product WHERE.....";
Database.Execute(sql);
}
}
Run Code Online (Sandbox Code Playgroud)
我主要担心的是上面的代码违反了 SOLID 原则,因为它负责创建和删除自己。
也许这些 Save 和 Delete 方法应该放在 Product 实体之外的某个地方(也许是 Factory/Repository?)。
我的代码如下所示
public interface ICar
{
void Created();
}
public class BigCar : ICar
{
public void Created()
{
}
}
public class SmallCar : ICar
{
public void Created()
{
}
}
public class LuxaryCar : ICar
{
public void Created()
{
}
}
public class CarFactory
{
public ICar CreateCar(int carType)
{
switch (carType)
{
case 0:
return new BigCar();
case 1:
return new SmallCar();
case 2:
return new LuxaryCar();
default:
break;
}
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
在这段代码中,我有一个返回具体实例的工厂.但每次我需要有一个新的ICar接口实现时,我必须更改CarFactory的CreateCar()方法.我似乎不支持SOLID原则的开放封闭原则.请建议有更好的方法来处理这种情况.
根据接口隔离原则
不应该强迫客户端实现不需要的接口方法
...因此我们应该定义接口以进行逻辑分离。
但是defaultJava 8 中引入的方法提供了在 Java 接口中实现方法的灵活性。似乎 Java 8 提供了增强接口的可行性,使其具有一些与其核心逻辑无关的方法,但具有一些默认或空的实现。
它不违反ISP吗?
java solid-principles default-method interface-segregation-principle
solid-principles ×10
c# ×4
oop ×4
interface-segregation-principle ×3
single-responsibility-principle ×3
interface ×2
architecture ×1
dry ×1
java ×1
laravel ×1
linq-to-sql ×1
php ×1
python ×1
tdd ×1