public class CommandResult
{
public string Command {get;set;}
}
public class CommandResult<T>:CommandResult
{
public T Data {get;set;}
}
public class FilesList
{
public string[] Files {get;set;}
}
public class UserInfo
{
...
}
public class MyAwesomeController:ApiController
{
public CommandResult<FilesList> GetFilesList(){
...
return new CommandResult<FilesList> (){
Command = "FilesList",
Data = new FilesList(){Files = new []{"file1","file2","file3","file4"}}
};
}
public CommandResult<UserInfo> GetUserInfo(){
...
}
}
public class MyAwesomeCommands
{
private CommandResult<FilesList> _getFilesList(){
...
return new CommandResult<FilesList> (){
Command = "FilesList",
Data = new FilesList(){Files = new []{"file1","file2","file3","file4"}}
};
}
public string GetFilesList(){
var data = getFilesList();
return JsonConvert.SerializeObject(data);
}
public string GetUserInfo(){
...
}
}
struct Row {
int data[10];
};
struct Table {
Row rows[10];
}
int sortJ;
int myCompare(const void* a,const void* b) {
int ia = reinterpret_cast<Row*>(a)->data[sortJ];
int ib = reinterpret_cast<Row*>(b)->data[sortJ];
if (ia < ib) return -1;
if (ia == ib) return 0;
return 1;
}
int someJ = 5;
sortJ = someJ;
qsort(table.rows, 10, sizeof(Row), myCompare);
class MyCompare {
public:
MyCompare(int aJ) : j(aJ) {}
bool operator () (const Row& a, const Row& b) const
{ return (a.data[j] < b.data[j]); }
private:
const int j;
}
int someJ = 5;
std::sort(table.rows, table.rows + 10, MyCompare(someJ));
int someJ = 5;
std::sort(table.rows, table.rows + 10,
[someJ](const Row& a, const Row& b) -> bool { return (a.data[someJ] < b.data[someJ]); } );
connect(this, &RenewThread::needUpdate, fMainControl.maincQobj(),
[this]() {
drm::createFile(fReregKey);
fMainControl.maincLayoutOnRegister();
});
HRESULT EnumObjects(
LPDIENUMDEVICEOBJECTSCALLBACK lpCallback,
LPVOID pvRef,
DWORD dwFlags
);
BOOL DIEnumDeviceObjectsCallback(
LPCDIDEVICEOBJECTINSTANCE lpddoi,
LPVOID pvRef
);
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
using IntV = vector<int>;
int main(int argc, char const *argv[]) {
IntV v = {0, 9, 4, 6, 7, 8, 5, 6};
size_t count = 0;
for_each(begin(v), end(v), [](IntV::value_type x){cout << x << " ";});
cout << endl;
sort(begin(v), end(v), [&count /*closure*/](IntV::value_type &a, IntV::value_type &b){
++count;
return a < b;
});
cout << count << endl;
for_each(begin(v), end(v), [](IntV::value_type x){cout << x << " ";});
cout << endl;
return 0;
}
interface IFoo
{
// Не содержит метод Commit()
// Но содержит все остальные методы и свойства, которые реализованы в классе Foo
}
interface ITransaction
{
void Commit();
}
class Foo : IFoo, ITransaction
{
// ...
public void Commit()
{
// ...
}
}
class Bar<T, TImpl> where TImpl : T, ITransaction, new()
{
private readonly TImpl _foo;
protected T Foo {
get { return _foo; }
}
public Bar() {
_foo = new TImpl();
}
public void Commit()
{
_foo.Commit();
}
}
services.AddTransient<DdContext>()
,public PersonService(DbContext context)
Глава 7. Метаданные и валидация модели
Аннотации данных для отображения свойств
Основы валидации
Атрибуты валидации
Валидация модели в контроллере
Отображение ошибок валидации
Создание собственной логики валидации
public class MovieViewModel : IValidatableObject
{
public int ID { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public string Image { get; set; }
public string Genre { get; set; }
public int GenreId { get; set; }
public string Director { get; set; }
public string Writer { get; set; }
public string Producer { get; set; }
public DateTime ReleaseDate { get; set; }
public byte Rating { get; set; }
public string TrailerURI { get; set; }
public bool IsAvailable { get; set; }
public int NumberOfStocks { get; set; }
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
var validator = new MovieViewModelValidator();
var result = validator.Validate(this);
return result.Errors.Select(item => new ValidationResult(item.ErrorMessage, new[] { item.PropertyName }));
}
}
public class MovieViewModelValidator : AbstractValidator<MovieViewModel>
{
public MovieViewModelValidator()
{
RuleFor(movie => movie.GenreId).GreaterThan(0)
.WithMessage("Select a Genre");
RuleFor(movie => movie.Director).NotEmpty().Length(1,100)
.WithMessage("Select a Director");
RuleFor(movie => movie.Writer).NotEmpty().Length(1,50)
.WithMessage("Select a writer");
RuleFor(movie => movie.Producer).NotEmpty().Length(1, 50)
.WithMessage("Select a producer");
RuleFor(movie => movie.Description).NotEmpty()
.WithMessage("Select a description");
RuleFor(movie => movie.Rating).InclusiveBetween((byte)0, (byte)5)
.WithMessage("Rating must be less than or equal to 5");
RuleFor(movie => movie.TrailerURI).NotEmpty().Must(ValidTrailerURI)
.WithMessage("Only Youtube Trailers are supported");
}
private bool ValidTrailerURI(string trailerURI)
{
return (!string.IsNullOrEmpty(trailerURI) && trailerURI.ToLower().StartsWith("https://www.youtube.com/watch?"));
}
}