嗨,我在存储库域中有很多重复的代码,例如
public CommonModel GetLocations(IModelWithCurrentUser<xyz> model)
{
var sqlParameters = SqlHelpers.GetReportSqlParameters(model);
var outputParameters = new Dictionary<string, object>() { { "TotalCount", default(int) } };
var result = _queryExecutor.QueryWithOutputParameters<Locations>(PermissionsQueries.GetLocations, sqlParameters, outputParameters);
var totalCount = Convert.ToInt32(outputParameters["TotalCount"]);
return RepositoryConverter.ToCommonDSModel(result, model.Content, totalCount);
}
我试图制作一个像这样的通用方法:
public CommonModel GetSqlReport<T>(IModelWithCurrentUser<xyz> model, string queryName)
{
var sqlParameters = SqlHelpers.GetReportSqlParameters(model);
var outputParameters = new Dictionary<string, object>() { { "TotalCount", default(int) } };
var result = _queryExecutor.QueryWithOutputParameters<T>(queryName, sqlParameters, outputParameters);
var totalCount = Convert.ToInt32(outputParameters["TotalCount"]);
return RepositoryConverter.ToCommonDSModel(result, model.Content, totalCount);
}
但是不幸的是,在编译代码时出现错误: 不能在方法
中从“ System.Collections.Generic.List角色”转换为“ System.Collections.Generic.List角色” return RepositoryConverter.ToCommonDSModel(result, model.Content, totalCount);
respoitoryConverter方法ToCommonDSModel按列表类型正在重载,下面是一些示例:
public static CommonModel ToCommonDSModel(List<Roles> data, xyz dtoModel, long itemsCount)
public static CommonModel ToCommonDSModel(List<Locations> data, xyz dtoModel, long itemsCount)
public static CommonModel ToCommonDSModel(List<abc> data, xyz dtoModel, long itemsCount)
我的问题是如何删除存储库域中的代码重复项?
答案 0 :(得分:0)
您在这里:
FILES/PROCESSING/@{formatDateTime(utcnow(),'yyyy')}/@{formatDateTime(utcnow(),'MM')}/@{formatDateTime(utcnow(),'dd')}/*.zip
秘诀在于使用public CommonModel GetSqlReport<T>(IModelWithCurrentUser<xyz> model, string queryName)
{
var sqlParameters = SqlHelpers.GetReportSqlParameters(model);
var outputParameters = new Dictionary<string, object>() { { "TotalCount", default(int) } };
var result = _queryExecutor.QueryWithOutputParameters<T>(queryName, sqlParameters, outputParameters);
var totalCount = Convert.ToInt32(outputParameters["TotalCount"]);
return RepositoryConverter.ToCommonDSModel<T>(result, model.Content, totalCount);
}
public static class RepositoryConverter
{
private static Dictionary<Type, Delegate> __map = new Dictionary<Type, Delegate>()
{
{ typeof(Roles), (Func<List<Roles>, xyz, long, CommonModel>)ToCommonDSModel },
{ typeof(Locations), (Func<List<Locations>, xyz, long, CommonModel>)ToCommonDSModel },
{ typeof(abc), (Func<List<abc>, xyz, long, CommonModel>)ToCommonDSModel },
}
public static CommonModel ToCommonDSModel<T>(List<T> data, xyz dtoModel, long itemsCount)
=> ((Func<List<T>, xyz, long, CommonModel>)__map[typeof(T)])(data, dtoModel, itemsCount);
public static CommonModel ToCommonDSModel(List<Roles> data, xyz dtoModel, long itemsCount) => /* implementation here */;
public static CommonModel ToCommonDSModel(List<Locations> data, xyz dtoModel, long itemsCount) => /* implementation here */;
public static CommonModel ToCommonDSModel(List<abc> data, xyz dtoModel, long itemsCount) => /* implementation here */;
}
来保存您根据通用类型Dictionary<Type, Delegate>
检索的不同类型的函数。
答案 1 :(得分:0)
好的,谢谢:D提供了非常好的代码,但是在另一个论坛上,我发现了这种方式:
更改
<script type="text/javascript"
src="http://maps.google.com/maps/api/js?sensor=false&libraries=drawing"></script>
<style type="text/css">
#map, html, body {
padding: 0;
margin: 0;
height: 100%;
}
#panel {
width: 200px;
font-family: Arial, sans-serif;
font-size: 13px;
float: right;
margin: 10px;
}
#color-palette {
clear: both;
}
.color-button {
width: 14px;
height: 14px;
font-size: 0;
margin: 2px;
float: left;
cursor: pointer;
}
#delete-button {
margin-top: 5px;
}
</style>
<script type="text/javascript">
var drawingManager;
var selectedShape;
var colors = ['#1E90FF', '#FF1493', '#32CD32', '#FF8C00', '#4B0082'];
var selectedColor;
var colorButtons = {};
function clearSelection() {
if (selectedShape) {
selectedShape.setEditable(false);
selectedShape = null;
}
}
function setSelection(shape) {
clearSelection();
selectedShape = shape;
shape.setEditable(true);
selectColor(shape.get('fillColor') || shape.get('strokeColor'));
}
function deleteSelectedShape() {
if (selectedShape) {
selectedShape.setMap(null);
}
}
function selectColor(color) {
selectedColor = color;
for (var i = 0; i < colors.length; ++i) {
var currColor = colors[i];
colorButtons[currColor].style.border = currColor == color ? '2px solid #789' : '2px solid #fff';
}
// Retrieves the current options from the drawing manager and replaces the
// stroke or fill color as appropriate.
var polylineOptions = drawingManager.get('polylineOptions');
polylineOptions.strokeColor = color;
drawingManager.set('polylineOptions', polylineOptions);
var rectangleOptions = drawingManager.get('rectangleOptions');
rectangleOptions.fillColor = color;
drawingManager.set('rectangleOptions', rectangleOptions);
var circleOptions = drawingManager.get('circleOptions');
circleOptions.fillColor = color;
drawingManager.set('circleOptions', circleOptions);
var polygonOptions = drawingManager.get('polygonOptions');
polygonOptions.fillColor = color;
drawingManager.set('polygonOptions', polygonOptions);
}
function setSelectedShapeColor(color) {
if (selectedShape) {
if (selectedShape.type == google.maps.drawing.OverlayType.POLYLINE) {
selectedShape.set('strokeColor', color);
} else {
selectedShape.set('fillColor', color);
}
}
}
function makeColorButton(color) {
var button = document.createElement('span');
button.className = 'color-button';
button.style.backgroundColor = color;
google.maps.event.addDomListener(button, 'click', function() {
selectColor(color);
setSelectedShapeColor(color);
});
return button;
}
function buildColorPalette() {
var colorPalette = document.getElementById('color-palette');
for (var i = 0; i < colors.length; ++i) {
var currColor = colors[i];
var colorButton = makeColorButton(currColor);
colorPalette.appendChild(colorButton);
colorButtons[currColor] = colorButton;
}
selectColor(colors[0]);
}
function initialize() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 10,
center: new google.maps.LatLng(22.344, 114.048),
mapTypeId: google.maps.MapTypeId.ROADMAP,
disableDefaultUI: true,
zoomControl: true
});
var polyOptions = {
strokeWeight: 0,
fillOpacity: 0.45,
editable: true
};
// Creates a drawing manager attached to the map that allows the user to draw
// markers, lines, and shapes.
drawingManager = new google.maps.drawing.DrawingManager({
drawingMode: google.maps.drawing.OverlayType.POLYGON,
markerOptions: {
draggable: true
},
polylineOptions: {
editable: true
},
rectangleOptions: polyOptions,
circleOptions: polyOptions,
polygonOptions: polyOptions,
map: map
});
google.maps.event.addListener(drawingManager, 'overlaycomplete', function(e) {
if (e.type != google.maps.drawing.OverlayType.MARKER) {
// Switch back to non-drawing mode after drawing a shape.
drawingManager.setDrawingMode(null);
// Add an event listener that selects the newly-drawn shape when the user
// mouses down on it.
var newShape = e.overlay;
newShape.type = e.type;
google.maps.event.addListener(newShape, 'click', function() {
setSelection(newShape);
});
setSelection(newShape);
}
});
// Clear the current selection when the drawing mode is changed, or when the
// map is clicked.
google.maps.event.addListener(drawingManager, 'drawingmode_changed', clearSelection);
google.maps.event.addListener(map, 'click', clearSelection);
google.maps.event.addDomListener(document.getElementById('delete-button'), 'click', deleteSelectedShape);
buildColorPalette();
}
google.maps.event.addDomListener(window, 'load', initialize);
到
var result = _queryExecutor.QueryWithOutputParameters<T>(queryName, sqlParameters, outputParameters);