我有一个带有域对象和数据映射器的 Web MVC 应用程序。数据映射器的类方法包含所有数据库查询逻辑。我试图避免镜像任何数据库结构,因此在构造 sql 语句时实现最大的灵活性。因此,原则上,我尝试完全不使用任何 ORM 或 ActiveRecord 结构/模式。
让我举个例子:通常,我可以有一个AbstractDataMapper由所有特定数据映射器类继承的抽象类 - 就像该类一样UserDataMapper。然后我可以findById()在 中定义一个方法,通过给定值AbstractDataMapper(例如用户 ID)获取特定表的记录。但这意味着我总是从单个表中获取记录,而无法使用任何左连接从与给定用户 ID 对应的其他表中获取一些其他详细信息。usersidid
所以,我的问题是:在这些条件下 - 我自己有义务这样做,我应该实现一个抽象数据映射器类,还是每个数据映射器类应该包含其自己的数据访问层的完全“专有”实现?
我希望我能清楚地表达我的想法。如果我不清楚或者您有任何疑问,请告诉我。
非常感谢您的时间和耐心。
我正在使用 Symfony 3.2.9 创建一个应用程序,并且我想做一个管理面板来管理应用程序。应用程序的工作方式类似于 CMS,因此使用 URL 创建新页面,例如domain.com/pagename1 以及domain.com/pagename1/subpagelevel2 等。问题是当我想为管理面板创建 URL 时,URL 应该类似于:domain.com/admin,但管理面板还需要一些子页面,例如domain.com/admin/manage 或domain.com/admin/manage/编辑/1等。
我创建了 DefaultController ,其路由如下:
/**
* @Route("/", name="homepage")
*/
Run Code Online (Sandbox Code Playgroud)
和 AdminController 的路由如下:
/**
* @Route("/admin", name="admin")
*/
Run Code Online (Sandbox Code Playgroud)
问题是,当我想动态创建应用程序的新子页面时,我需要创建路由,例如:
/**
* @Route("/{page}")
*/
Run Code Online (Sandbox Code Playgroud)
但这会覆盖我的管理面板子页面(例如domain.com/admin/manage)。
是否可以通过 AdminController 从默认 DefaultController 中排除或覆盖路径?我希望能够从 DefaultController 创建所有 URL,除了像 domain.com/admin ... 等开头的路径。
play框架中的反向路由到底是什么?我是 Java 新手,目前正在研究 java play 框架。我已阅读播放框架文档,但对反向路由一无所知。请帮忙。
我想知道是否可以在 php 中创建一个 mvc 项目而不使用路由。例如:
我有控制器 strumenti.php
class Strumenti
{
public function index()
{
require 'application/models/strumentimodel.php';
$strumenti_model=new StrumentiModel("r");
$strumenti = $strumenti_model->getAllInstruments();
require 'application/views/_templates/header.php';
require 'application/views/strumenti/index.php';
require 'application/views/_templates/footer.php';
}
public function deleteStrumento($nome)
{
if (isset($nome)) {
require 'application/models/strumentimodel.php';
$strumenti_model=new StrumentiModel("r");
$strum=$strumenti_model->deleteStrumentoDaArray($nome);
}
header('location: 'mysite/strumenti/index');
}
}
Run Code Online (Sandbox Code Playgroud)
和我的模型 struentimodel.php
class StrumentiModel
{
private $handle;
function __construct($mode) {
try {
$this->handle = fopen(STRUMENTI, "$mode");
} catch (PDOException $e) {
exit('Errore di apertura file');
}
}
public function getAllInstruments()
{
$csv = array();
$lines …Run Code Online (Sandbox Code Playgroud) 我使用列表验证
public class Test
{
[ListValidation(ErrorMessage ="wrong")]
public List<string> Listt { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
列表验证实现
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public class ListValidationAttribute : ValidationAttribute
{
public override bool IsValid(object value)
{
var list = value as IList;
if (list != null)
{
return list.Count > 0;
}
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
当我测试它时
Test t = new Test();
List<string> str = new List<string>();
str.Add("haha");
str.Add("hoho");
t.Listt = str;
JsonResult json = ModelValidation.ValidateProperty(t, nameof(t.Listt));
Run Code Online (Sandbox Code Playgroud)
它抛出ArgumentException
{System.ArgumentException: The value …Run Code Online (Sandbox Code Playgroud) 我正在使用ThreadPool通用存储库,但收到此错误;
'无法访问已处置的对象。导致此错误的一个常见原因是处置从依赖项注入解析的上下文,然后尝试在应用程序的其他位置使用相同的上下文实例。如果您在上下文上调用 Dispose() 或将上下文包装在 using 语句中,则可能会发生这种情况。如果您使用依赖项注入,则应该让依赖项注入容器负责处理上下文实例。
private readonly AuthorizedServiceService _authorizedServiceService;
private readonly CustomerService _customerService;
public IConfigurationRoot Configuration { get; }
public UpdateService(AuthorizedServiceService authorizedServiceService, CustomerService customerService)
{
_authorizedServiceService = authorizedServiceService;
_customerService = customerService;
}
public void UpdateAllRecords()
{
_authorizedServiceService.GetByActive().ToList().ForEach(UpdateAuthorizedServiceRecords);
}
void UpdateAuthorizedServiceRecords(AuthorizedService authorizedService)
{
//UpdateStart(authorizedService);
var mywatch = new Stopwatch();
mywatch.Start();
ThreadPool.QueueUserWorkItem(new WaitCallback(state => { UpdateStart(authorizedService); }));
mywatch.Stop();
mywatch.Reset();
}
public void UpdateStart(AuthorizedService authorizedService)
{
UpdateCustomers(authorizedService);
ThreadPool.QueueUserWorkItem(new WaitCallback(state => { UpdateCustomers(authorizedService); }));
}
internal void UpdateCustomers(AuthorizedService authorizedService)
{
try …Run Code Online (Sandbox Code Playgroud) 当前正在运行身份服务器 4 并且能够使用 client_credential 流接收访问令牌。
我使用令牌从服务器访问 api 但收到此消息
error="invalid_token", error_description="未找到签名密钥"
我怀疑在调用 Web api 时传递令牌时需要以某种方式验证令牌。
我的设置是这样的。
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(options =>
{
options.Authority = Configuration.GetValue<string>("ApiAuthorityBaseUrl");
options.Audience = "api1";
options.RequireHttpsMetadata = false;
});
Run Code Online (Sandbox Code Playgroud)
并使用 AddDeveloperSigningCredential。
AddJwtBearer 中有一个名为 TokenValidationParameters 的东西,我不确定它是否是解决问题的正确位置。有人可以给我一些提示吗
我正在总结该守则。以下代码在foreach循环中迭代了近 4000 次。在我的本地 IIS 上,它可以正常工作,但在生产云服务器上,整个应用程序崩溃了。
try
{
cell.Value = decimal.Parse(dr[dc.ColumnName].ToString());
}
catch
{
cell.Value = dr[dc.ColumnName];
}
Run Code Online (Sandbox Code Playgroud)
我的疑问是:- 如上所述使用 Catch 块会导致 CPU 使用率高吗?
作为附加信息:-
Catch已点击 4000 次
该应用程序适用于商店和管理路线,但不适用于错误路线。错误路线也是 MVC 格式,但我也在没有架构的情况下对其进行了测试。
以下是根目录中的 app.js 片段。
const path = require('path');
const express = require('express');
const bodyParser = require('body-parser');
const errorController = require('./controllers/error');
const app = express();
app.set('view engine', 'ejs');
app.set('views', 'views');
const adminRoutes = require('./routes/admin');
const shopRoutes = require('./routes/shop');
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, 'public')));
app.use('/admin', adminRoutes);
app.use(shopRoutes);
app.use((req, res, next) => {
res.status(404).render('404', { pageTitle: 'Page Not Found' });
});
app.listen(3000);
Run Code Online (Sandbox Code Playgroud)
404 EJS代码是
<%- include('includes/head.ejs') %>
</head>
<body>
<%- include('includes/navigation.ejs') %>
<h1>Page Not Found!</h1>
<%- include('includes/end.ejs') %>
Run Code Online (Sandbox Code Playgroud)
并且,Navigation.ejs 是 …
我的 MVC 应用程序与 Autofac 连接。我还配置了 NLog,它在我的控制器类中按预期工作。我的nLogger注册如下:
var builder = new ContainerBuilder();
builder.RegisterGeneric(typeof(LoggerService<>))
.As(typeof(ILoggerService<>)).SingleInstance();
var container = builder.Build();
Run Code Online (Sandbox Code Playgroud)
ILoggerService 的构造函数是:
public LoggerService()
{
SourceClass = typeof (T).FullName;
Logger = LogManager.GetLogger(SourceClass);
}
Run Code Online (Sandbox Code Playgroud)
现在我也有很多我使用的静态帮助器类。例如:
public static class Helper
{
public static string GenerateQrBitmap(string secret, string issuer, string userEmail)
{
...
}
}
Run Code Online (Sandbox Code Playgroud)
但我也希望能够在这些 Helper 类中使用记录器。
c# ×4
php ×3
asp.net-core ×2
routes ×2
access-token ×1
attributes ×1
autofac ×1
class ×1
data-mapping ×1
express ×1
identity ×1
java ×1
nlog ×1
node.js ×1
oop ×1
performance ×1
symfony ×1
symfony-3.2 ×1
threadpool ×1
validation ×1