我正在寻找一些违反单一责任原则的代码示例.不要向我展示鲍勃叔叔的书籍或网站上的任何例子,因为这些都是在互联网上贴满的,就像这样:
interface Modem
{
public void dial(String pno);
public void hangup();
public void send(char c);
public char recv();
}
Run Code Online (Sandbox Code Playgroud) 我的项目中有这样的东西,项目已经完成了(它正在工作)我只想知道它是否适用于SOLID原则
static public class Tools
{
static public GetProduct(this id){...}
static public GetProductCategory(this id){...}
static public GetUser(this id){...}
// I also have here methods like IsStringNull ...
// IsNull IsFalse, lots of stuff, everything static
}
Run Code Online (Sandbox Code Playgroud)
用法是这样的
var UserThatCreatedCategoryForThisProduct =
prodId.GetProduct().CategoryId.GetProductCategory().Creator.GetUser();
Run Code Online (Sandbox Code Playgroud)
我知道这是显而易见的,它是违反了SRP,但这个类是静态的,它包含了彼此独立的一个静态方法,它是一种过相同的,如果我会为每个方法创建一个静态类
所以说我有一个C#类,
class Foo : Bar, IBar, IBar2
{
}
Run Code Online (Sandbox Code Playgroud)
...其中Bar是一个类,IWhatever和IFine是接口.我打算在多个类中使用类似的IWhatever和IFine实现 - 我可以看到封装这些代码并在所有类中重用它的唯一理智方法是创建一个类,无论继承自IW和I的继承自IFine和使它们成为实现这些接口的类的成员,然后在从接口实现的成员中调用它们的成员,如下所示:
class Foo : Bar, IWhatever, IFine
{
IWhatever mWhatever;
IFine mFine;
Foo()
{
mWhatever = new Whatever();
mFine = new Fine();
}
// from IWhatever
void Kick()
{
mWhatever.Kick();
}
// from IFine
void Punch()
{
mFine.Punch();
}
}
Run Code Online (Sandbox Code Playgroud)
我做得对吗?有没有更好的办法?
我正在asp.net中构建一个简单的MVC应用程序.我想遵循依赖倒置原则,我不知道我是否做得对.
我目前正在研究身份验证系统.我有一个AccountController,里面使用Authenticator服务.Authenticator服务通过构造函数注入注入控制器.
public class AccountController : Controller
{
private IAuthenticator _authenticator;
public AccountController(IAuthenticator authenticator)
{
_authenticator = authenticator;
}
//
// POST: /Account/Login
[HttpPost]
public ActionResult Login(LoginModel model, string redirectToUrl = null)
{
...
}
Run Code Online (Sandbox Code Playgroud)
文件的结构是这样的:

但我想如果我想要完全颠倒控制器及其依赖关系之间的重要性,我将不得不将身份验证服务的接口移到控制器旁边.像这样的东西:

这样,客户端 - 控制器 - 以及服务的抽象将位于同一名称空间中.因此,服务接口的更改将来自客户端,并将传播到服务实现.而不是以前的方式,服务中发生的变化传播到客户端.依赖项是Inverted - 服务依赖于客户端.
当客户端和服务在不同的程序集中时,我可以看到这样做的好处,但是我不确定如果在同一个程序集中我应该这样做.
如果我这样做是正确的,我是否应该使用第一个文件结构或第二个文件结构,请告诉我.
谢谢,阿西尔
c# asp.net-mvc inversion-of-control solid-principles dependency-inversion
我对SOLID设计原则很陌生.我理解的一个问题是Liskov Substition Principle违规的"方形矩形"示例.为什么Square的高度/宽度设置器会覆盖矩形的高度?当存在多态性时,这不正是导致问题的原因吗?
不删除这个解决问题?
class Rectangle
{
public /*virtual*/ double Height { get; set; }
public /*virtual*/ double Width { get; set; }
public double Area() { return Height * Width; }
}
class Square : Rectangle
{
double _width;
double _height;
public /*override*/ double Height
{
get
{
return _height;
}
set
{
_height = _width = value;
}
}
public /*override*/ double Width
{
get
{
return _width;
}
set
{
_width = _height = value;
}
} …Run Code Online (Sandbox Code Playgroud) 我在课堂设计上陷入两难境地.我正在尽力尊重SOLID原则,但我不知道如何处理依赖注入.
这是我的困境:
为了说明我的困境,我试图创建一个用例.
我想创建一个脚本(我在下面部分实现),生成一个文件并打印另一个文件:
<?php
/**
* Generate a file, add it to the queue and print the next one
*/
class Script
public function run() {
#Generate file
$fileComputor = new FileComputer(...);
$file = $fileComputor->compute();
#Instantiate dependencies for printing
$db = new Db(new PdoAdapter());
$printerDriver = new Driver(new HttpRequest(new CurlAdapter()));
$log = new Log($db);
$queue = new Queue($db);
$statsUpdater = new StatsUpdater($db);
$monitor = new Monitor(new StatsdDriver(new SocketAdapter()));
#Add generated file to the queue
$queueModel->push($file);
#Print …Run Code Online (Sandbox Code Playgroud) 我想更多地了解单一责任。如果我尝试代表可以添加,删除,更新,检索的客户,那么过去我的客户类将具有所有的add,remove,update和get方法。这有助于将现实世界中客户的行为归为一类。我只负责将客户类别分为AddCustomer类,删除客户类别,更新客户类别和获取客户类别,甚至获取所有客户类别?
c# encapsulation single-responsibility-principle solid-principles
我需要你帮我做一个我不满意的设计.该应用程序正在消费RSS新闻(文章的RSS,以及每篇文章的评论RSS).
我创建了一个名为IDataService的接口,它提供了数据提供程序的基本行为.
public interface IDataService
{
Task<List<Item>> GetItemsAsync(string url, IItemsParser parser);
Task<List<Comment>> GetItemCommentsAsync(string url, ICommentsParser parser);
}
Run Code Online (Sandbox Code Playgroud)
如您所见,每个函数都将web服务url(在我的情况下为RSS feed url)和一些知道如何处理数据的解析器的接口作为参数.
这些是两个解析的接口:
public interface IItemsParser
{
List<Item> ParseRawData(string rawData);
}
public interface ICommentsParser
{
List<Comment> ParseRawData(string rawData);
}
Run Code Online (Sandbox Code Playgroud)
现在让我们具体一点,这是实现类:
public class MyRSSDataService : IDataService
{
public async Task<List<Item>> GetItemsAsync(string url, IItemsParser parser)
{
using (var httpClient = new HttpClient())
{
var response = await httpClient.GetAsync(new Uri(url));
if (response.IsSuccessStatusCode)
{
var jsonResponse = await response.Content.ReadAsStringAsync();
List<Item> items = parser.ParseRawData(jsonResponse);
return items;
} …Run Code Online (Sandbox Code Playgroud) 好吧,当我在观看SOLID视频时,我想到了这一点.该单Responsabily原理说,"一类的应该只有一个单一的责任".
非常好.但与此同时,我正在使用N-Layer模型构建的ASP.NET MVC 5项目.我们有UI层,存储库层,域层和要公开的服务层.在服务层,我们有每一个基本类域类(UserService,CompanyService,等).该UserService班有一个责任就是采取的护理User操作,但另一方面,它有很多型动物responsabilities如身份验证和处理用户/公司的关系.这违反了SRP原则吗?
我有一个Lawsuit有Employer列表的类.要求列表中的一个雇主必须被设定为主要雇主.我想到了两种方法来满足这个业务规则.
解决方案1
这是我当前的实现,我有一个MainEmployer和我在同一个属性和Employers列表上存储相同的实体:
public class Lawsuit()
{
public int Id { get; set; }
public virtual Employer MainEmployer { get; set; }
public virtual ICollection<Employer> Employers { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
解决方案2
我还可以EmployerLawsuit使用bool属性创建一个名为的中间类Main:
public class LawsuitEmployer()
{
public int Id { get; set; }
public bool Main { get; set; }
public virtual Employer Employer { get; set; }
public virtual Lawsuit Lawsuit { get; set; …Run Code Online (Sandbox Code Playgroud) solid-principles ×10
c# ×6
oop ×4
architecture ×2
single-responsibility-principle ×2
asp.net-mvc ×1
inheritance ×1
interface ×1
liskov-substitution-principle ×1
php ×1
rss ×1