这可能是代码审查的问题,而不是堆栈溢出.
我正在使用Dapper for MicroORM来检索并将数据保存到SQL Server 2014.我在DTO Proj中有DTO类,它代表从DB检索到的数据或保存到DB.
我正在使用存储库模式,所以在我的服务层,如果需要存储库,我使用构造函数DI来注入该依赖项,然后调用存储库上的方法来完成工作.
所以假设我有2个名为CustomerService和CarService的服务.
然后,我有2个存储库,一个CustomerRepository和一个CarRepository.
我有一个接口,它定义每个存储库中的所有方法,然后定义具体的实现.
下面显示了一个示例方法(调用Stored Proc来执行DB INSERT(注意存储过程的实际字符串变量被定义为类顶部的私有字符串):
public void SaveCustomer(CustomerDTO custDTO)
{
using (IDbConnection db = new SqlConnection(ConfigurationManager.ConnectionStrings["myDB"].ConnectionString))
{
db.Execute(saveCustSp, custDTO, commandType: CommandType.StoredProcedure);
}
}
Run Code Online (Sandbox Code Playgroud)
这一切都很好但我发现自己在每个存储库中的每个方法中重复使用块.我有两个真正的问题,如下所述.
有没有更好的方法,我可能会以某种方式使用BaseRepository类,每个其他存储库继承,Base将实现数据库连接的实例化?
对于系统上的多个并发用户,这仍然可以正常工作吗?
********更新
根据Silas的回答,我创建了以下内容
public interface IBaseRepository
{
void Execute(Action<IDbConnection> query);
}
public class BaseRepository: IBaseRepository
{
public void Execute(Action<IDbConnection> query)
{
using (IDbConnection db = new SqlConnection(ConfigurationManager.ConnectionStrings["myDB"].ConnectionString))
{
query.Invoke(db);
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是,在我的存储库中,我有其他方法,如下所示:
public bool IsOnlyCarInStock(int carId, int year)
{
using (IDbConnection db = new SqlConnection(ConfigurationManager.ConnectionStrings["myDB"].ConnectionString)) …
Run Code Online (Sandbox Code Playgroud) 我有一个Car WebAPI控制器方法如下 - 注意_carService.GetCarData返回CarDataDTO对象的集合
[HttpGet]
[Route("api/Car/Retrieve/{carManufacturerID}/{year}")]
public IEnumerable<CarData> RetrieveTest(int carManufacturerID, int year)
{
//Mapper.Map<>
var cars = _carService.GetCarData(carManufacturerID, year);
//var returnData = Mapper.Map<CarData, CarDataDTO>();
return cars;
}
Run Code Online (Sandbox Code Playgroud)
CarData是我创建的WebAPI模型.
public class CarData
{
public string Model { get; set; }
public string Colour { get; set; }
//other properties removed from brevity
}
Run Code Online (Sandbox Code Playgroud)
CarDataDTO是我创建的类,它为DB表建模 - 我通过使用dapper调用的存储过程检索数据.
public class CarDataDTO
{
public int CarID { get; set; }
public int CarManufacturerID { get; set; }
public int Year { get; set; } …
Run Code Online (Sandbox Code Playgroud) 我在我的应用程序中使用CK Rich Text Editor.我有一个模态弹出窗口,在其中我有三个选项卡 - 每个选项卡呈现相同的局部视图,其中我有一个字段调用描述这是我使用CK编辑器.当我使用IE 11时,一切都按预期工作,Tabs加载Textarea变成CK编辑框,每次文本区域保持为富文本编辑器时在标签之间导航.但是,当我第一次打开模式框时,我看到Chrome中的奇怪行为,每个选项卡上的描述文本区域都按预期变成了ck编辑器,而我在它们之间的选项卡中每个选项卡都是正确的文本区域.但是在Chrome中如果我关闭模态框并重新启动我在控制台中得到上面的错误?如果我打开模态框并在选项卡之间导航6次,我会出现相同的错误,然后丢失文本区域的功能,作为CK富文本编辑器.有没有人有类似的东西或有可能的解决方案.
我的js文件中的代码如下:
$(document).ready(function () {
var editor = CKEDITOR.instances['Description'];
if (editor) { editor.destroy(true); }
CKEDITOR.replaceAll();
});
Run Code Online (Sandbox Code Playgroud)
在3个选项卡中呈现的局部视图中的cshtml标记如下:
<div class="row">
@Html.LabelFor(model => model.Description)
<div class="col-md-10">
@Html.TextAreaFor(model => model.Description)
</div>
</div>
Run Code Online (Sandbox Code Playgroud) 我要求强制用户在90天后更改密码.这可以正常工作,如下所示:
if (memberUser.LastPasswordChangedDate.Date.AddDays(90) < DateTime.Now.Date)
{
return RedirectToAction("MyChangePasswordMethod", "MyController");
}
Run Code Online (Sandbox Code Playgroud)
然而,我的另一个要求是用户使用的最后5个密码不能重复 - asp.net是否内置了这样的内容?我无法在aspnet数据库表中看到存储最后一个密码的任何内容.是否应该将最后的密码值存储在我自己的db表中,然后根据我自己的自定义db表中的用户值输入的新密码字段进行比较?
我有一个倒数计时器表格 - 在第一个表格上,用户将输入倒计时时间 - 警告时间,结束消息等.还有两个单选按钮(最大/最小),根据选择哪个,他们将打开一个新的最大值或最小形式,时间实际上将开始倒计时.它正常工作并按照我的预期倒计时.但是,如果我退出Max或Min形式并尝试再次运行新的时间,我会收到错误.代码如下 - 注意注释掉.ShowDialog(this); 是我试过的东西 - 它让我关闭并打开新表格确定但它实际上并没有开始倒计时.UpdateLabels是用于更新标签的功能.
bool Max = rbMax.Checked;
if (Max == true)
{
//_Max.ShowDialog(this);
_Max.Show();
}
else
//_Min.ShowDialog(this);
_Min.Show();
UpdateLabels();
}
Run Code Online (Sandbox Code Playgroud)
我也尝试了以下在线阅读作为可能的解决方案,但它也没有工作....
private void Max_FormClosing(object sender, FormClosingEventArgs e)
{
this.Hide();
this.Parent = null;
}
Run Code Online (Sandbox Code Playgroud)
任何人都可以帮助我 - 如果需要,我可以发布UpdateLabels功能.我对UI C#开发很陌生,所以任何帮助都会很棒.谢谢.
我的MVC 5应用程序中有许多不同的Web.config用于不同的环境 - 例如Test/Prod
我有适当的网络转换来更改不同环境的值.例如,我的web.config文件中有以下应用程序设置:
<appSettings>
<add key="DevDisplayPanel" value="true" />
</appSettings>
Run Code Online (Sandbox Code Playgroud)
然后在我的Web.Test.config和Web.Prod.config中使用Web转换来更改值,如下所示:
<appSettings>
<add key="DevDisplayPanel"
xdt:Transform="Replace"
xdt:Locator="Match(key)"
value="false" />
<appSettings>
Run Code Online (Sandbox Code Playgroud)
但是在我的Web.config中我也有自己的自定义部分,它在<appSettings>
部分之外,如下所示:
<myCustomSection>
<serverList>
<add zone="Zone1" url="https://dev-myurl1.com"/>
<add zone="Zone2" url="https://dev-myurl2.com"/>
<add zone="Zone2" url="https://dev-myurl3.com"/>
</serverList>
</myCustomSection>
Run Code Online (Sandbox Code Playgroud)
我的问题是 - 是否可以进行Web转换,以便Test和Prod看起来如下所示:
测试:
<myCustomSection>
<serverList>
<add zone="Zone1" url="https://test-myurl1.com"/>
<add zone="Zone2" url="https://test-myurl2.com"/>
<add zone="Zone2" url="https://test-myurl3.com"/>
</serverList>
</myCustomSection>
Run Code Online (Sandbox Code Playgroud)
PROD:
<myCustomSection>
<serverList>
<add zone="Zone1" url="https://prod-myurl1.com"/>
<add zone="Zone2" url="https://prod-myurl2.com"/>
<add zone="Zone2" url="https://prod-myurl3.com"/>
</serverList>
</myCustomSection>
Run Code Online (Sandbox Code Playgroud) asp.net asp.net-mvc web-config web-config-transform web.config-transform
我基本上在我的页面上进行了以下验证 - 它是一个单词规则,文本框中的描述不能超过3个单词,不包括单词'和'.我在C#中实现了以下服务器端验证,它运行正常
if (Desc.Trim().ToString() != "")
{
MatchCollection collection = Regex.Matches(Desc.Replace("and", ""), @"[\S]+");
if (collection.Count > 3)
{
ErrorMsg.Append("Description should contain at most 3 words(excluding 'and').");
ErrorMsg.Append("\\n");
}
}
Run Code Online (Sandbox Code Playgroud)
但是我很难在Javascript中使用相同的工作.我已经尝试了以下但是它到目前为止还没有工作,所以希望有更好的Javascript知识的人可以看到错误.注意if是在页面上触发的更大验证函数的一部分 - 警报只是在那里看它是否进入这个if(它没有) - 当这个块被删除时,页面上的其余JS是工作正常.
if (Desc.val().trim() != "")
{
alert('1');
!regexWordRule.test(Desc.val());
alert('2');
if (Desc.val().match(regexWordRule).length > 3)
{
errorText += "Description should contain at most 3 words(excluding 'and').";
}
valid = false;
}
Run Code Online (Sandbox Code Playgroud)
以下是我在js文件最顶部定义的regexWordRule.
var regexWordRule = /[\S]+/;
Run Code Online (Sandbox Code Playgroud) 我是从我们的网络团队成员那里得到的:
您可以看到extensionAttribute2中有一个值.如何检索此值 - 我无法在UserPrincipal对象中看到extensionAttributes - 除非我遗漏了某些内容.
我已经回到了一个级别并尝试了以下内容:
UserPrincipal myUser = UserPrincipal.FindByIdentity(con, identityName);
DirectoryEntry de = (myUser.GetUnderlyingObject() as DirectoryEntry);
if (de != null)
{
// go for those attributes and do what you need to do
if (de.Properties.Contains("extensionAttribute2"))
{
return de.Properties["extensionAttribute2"][0].ToString();
}
else
{
return string.Empty;
}
}
Run Code Online (Sandbox Code Playgroud)
但是这不起作用 - 调试它有大约40个属性可用但没有extensionAttribute2
我的页面上有一些内容,这些内容都包裹在ng-if中,如下所示:
<div ng-if="ShowMessgae" class="text-danger">
<p>
<strong>
Message displayed to User
</strong>
</p>
</div>
Run Code Online (Sandbox Code Playgroud)
然后在我的angular js控制器中,我有以下内容:
function MyController($scope, $q, notifyHelper) {
openAjaxLoaderGif();
$scope.ShowMessgae = false;
MyService.getVarsFromSession().then(function (result) {
// some code removed for brevity
if(some coditional) {
$scope.ShowMessgae = true;
}
}, function (data, failReason, headers) {
notifyHelper.error("Error has occurred - try again");
})
})
.finally(function () {
closeAjaxLoaderGif();
});
};
Run Code Online (Sandbox Code Playgroud)
我将ShowMessage的值设置为false,并且只有在满足特定条件的情况下才为true。在大多数情况下,ShowMessage将为false。但是,这导致当页面加载显示给用户的标记消息时短暂呈现的消息,然后消失(以显示我的期望)-我做错了什么导致此闪烁吗?
我正在开发一个新的MVC 5应用程序.我的项目中有一个Integration Layer,我有一个外部Web服务的服务引用.
然后我创建了一个有一些方法的接口 - 然后我有一个服务类,如下所示:
public class MyService : IMyService
{
private readonly ExternalServiceClient _serviceClient;
public MyService()
{
_serviceClient = new ExternalServiceClient ("WSHttpBinding_IMyService");
}
public string GetName(Coordinate[] coordinates)
{
string name = string.Empty;
name = _serviceClient.GetInformationsForCoordinates(coordinates);
return name;
}
Run Code Online (Sandbox Code Playgroud)
注意这是剥离的,实际上会添加带有异常处理的try catch块等
我有一个Integration.Tests项目,用于集成层的单元测试.测试GetName方法的最佳实践方法是什么?我是否应该在测试项目中向服务的端点添加另一个服务引用,或者如果我们使用Moq,我可以创建一个实际服务的实例,例如Web层将调用 - 以及如何完成?
c# ×6
.net ×3
javascript ×3
angularjs ×1
asp.net ×1
asp.net-mvc ×1
automapper ×1
ckeditor ×1
dapper ×1
forms ×1
jquery ×1
moq ×1
unit-testing ×1
validation ×1
web-config ×1
winforms ×1