Alerin
Alerin
CC#
Created by Alerin on 5/8/2023 in #help
❔ How to verify data according to DDD, SOLID and Clean Architecture?
I own in two places one IF, it is:
if(credential.Status != Credential.StatusType.Verified)
if(credential.Status != Credential.StatusType.Verified)
According to the above rules, how not to duplicate the condition? I would like to avoid a problem in the future where we change something in one place and forget the other. My code:
public async Task<bool> CheckCredentialAsync(string name)
{
var credential = await _credentialRepository.GetByValueAsync(name, Domain.Entities.Credential.MethodType.Phone);

if(credential is null)
return false;

if(credential.Status != Domain.Entities.Credential.StatusType.Verified)
return false;

return true;
}
public async Task<bool> CheckCredentialAsync(string name)
{
var credential = await _credentialRepository.GetByValueAsync(name, Domain.Entities.Credential.MethodType.Phone);

if(credential is null)
return false;

if(credential.Status != Domain.Entities.Credential.StatusType.Verified)
return false;

return true;
}
13 replies
CC#
Created by Alerin on 4/15/2023 in #help
❔ Is the functionality of my code in the right place
I created a valueobject for my sitemap. I'm wondering if it's in the right place or shouldn't it be in Application instead of Domain?
namespace Stand.Libraries.Website.Sitemaps.Domain.ValueObjects;

public class SitemapPage : IEquatable<SitemapPage>
{
public SitemapPage(IReadOnlyCollection<Page> pages)
{
Pages = pages?.ToList() ?? throw new ArgumentNullException(nameof(pages));
}

public List<Page> Pages { get; }

public bool Equals(SitemapPage other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return Pages.SequenceEqual(other.Pages);
}

public override bool Equals(object obj)
{
return Equals(obj as SitemapPage);
}

public override int GetHashCode()
{
return Pages.GetHashCode();
}

public override string ToString()
{
XNamespace xmlns = "http://www.sitemaps.org/schemas/sitemap/0.9";
XNamespace xhtml = "http://www.w3.org/1999/xhtml";

var urlElements = Pages.Select(page =>
{
var pageElements = new List<XElement>
{
new XElement(xmlns + "loc", page.Url)
};

if (page.LastModified.HasValue)
pageElements.Add(new XElement(xmlns + "lastmod", page.LastModified.Value.ToString("yyyy-MM-ddTHH:mm:ss+00:00")));

if (page.ChangeFrequency.HasValue)
pageElements.Add(new XElement(xmlns + "changefreq", page.ChangeFrequency.Value.ToString().ToLowerInvariant()));

if (page.Priority.HasValue)
pageElements.Add(new XElement(xmlns + "priority", page.Priority.Value.ToString(CultureInfo.InvariantCulture)));

if (page.Cultures?.Any() == true)
{
foreach (var culture in page.Cultures)
{
pageElements.Add(new XElement(xhtml + "link",
new XAttribute(XNamespace.Xmlns + "xhtml", xhtml),
new XAttribute("rel", "alternate"),
new XAttribute("hreflang", culture.Name),
new XAttribute("href", culture.Url)));
}
}

return new XElement(xmlns + "url", pageElements);
});

return new XDocument(
new XDeclaration("1.0", "UTF-8", null),
new XElement(xmlns + "urlset", urlElements)
).ToString();
}
}
namespace Stand.Libraries.Website.Sitemaps.Domain.ValueObjects;

public class SitemapPage : IEquatable<SitemapPage>
{
public SitemapPage(IReadOnlyCollection<Page> pages)
{
Pages = pages?.ToList() ?? throw new ArgumentNullException(nameof(pages));
}

public List<Page> Pages { get; }

public bool Equals(SitemapPage other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return Pages.SequenceEqual(other.Pages);
}

public override bool Equals(object obj)
{
return Equals(obj as SitemapPage);
}

public override int GetHashCode()
{
return Pages.GetHashCode();
}

public override string ToString()
{
XNamespace xmlns = "http://www.sitemaps.org/schemas/sitemap/0.9";
XNamespace xhtml = "http://www.w3.org/1999/xhtml";

var urlElements = Pages.Select(page =>
{
var pageElements = new List<XElement>
{
new XElement(xmlns + "loc", page.Url)
};

if (page.LastModified.HasValue)
pageElements.Add(new XElement(xmlns + "lastmod", page.LastModified.Value.ToString("yyyy-MM-ddTHH:mm:ss+00:00")));

if (page.ChangeFrequency.HasValue)
pageElements.Add(new XElement(xmlns + "changefreq", page.ChangeFrequency.Value.ToString().ToLowerInvariant()));

if (page.Priority.HasValue)
pageElements.Add(new XElement(xmlns + "priority", page.Priority.Value.ToString(CultureInfo.InvariantCulture)));

if (page.Cultures?.Any() == true)
{
foreach (var culture in page.Cultures)
{
pageElements.Add(new XElement(xhtml + "link",
new XAttribute(XNamespace.Xmlns + "xhtml", xhtml),
new XAttribute("rel", "alternate"),
new XAttribute("hreflang", culture.Name),
new XAttribute("href", culture.Url)));
}
}

return new XElement(xmlns + "url", pageElements);
});

return new XDocument(
new XDeclaration("1.0", "UTF-8", null),
new XElement(xmlns + "urlset", urlElements)
).ToString();
}
}
Use:
namespace Stand.Libraries.Website.Sitemaps.Application.Services;

public class MapService : IMapService
{
readonly IEnumerable<ISitemap> _sitemaps;

public MapService(IEnumerable<ISitemap> sitemaps)
=> _sitemaps = sitemaps;

public IReadOnlyCollection<Map> GetMaps()
=> _sitemaps
.Select(map => new Map(map.Name))
.ToList();

public SitemapIndex GetSitemapIndex()
{
var maps = _sitemaps
.Select(map => new Map(map.Name))
.ToList();

return new SitemapIndex(maps);
}
}
namespace Stand.Libraries.Website.Sitemaps.Application.Services;

public class MapService : IMapService
{
readonly IEnumerable<ISitemap> _sitemaps;

public MapService(IEnumerable<ISitemap> sitemaps)
=> _sitemaps = sitemaps;

public IReadOnlyCollection<Map> GetMaps()
=> _sitemaps
.Select(map => new Map(map.Name))
.ToList();

public SitemapIndex GetSitemapIndex()
{
var maps = _sitemaps
.Select(map => new Map(map.Name))
.ToList();

return new SitemapIndex(maps);
}
}
4 replies
CC#
Created by Alerin on 2/22/2023 in #help
❔ System.Reflection.ReflectionTypeLoadException: „Unable to load one or more of the requested types.
System.Reflection.ReflectionTypeLoadException: „Unable to load one or more of the requested types.
Could not load type 'SqlGuidCaster' from assembly 'Microsoft.Data.SqlClient, Version=5.0.0.0, Culture=neutral, PublicKeyToken=23ec7fc2d6eaa4a5' because it contains an object field at offset 0 that is incorrectly aligned or overlapped by a non-object field.”
System.Reflection.ReflectionTypeLoadException: „Unable to load one or more of the requested types.
Could not load type 'SqlGuidCaster' from assembly 'Microsoft.Data.SqlClient, Version=5.0.0.0, Culture=neutral, PublicKeyToken=23ec7fc2d6eaa4a5' because it contains an object field at offset 0 that is incorrectly aligned or overlapped by a non-object field.”
in code:
private IEnumerable<Assembly> Types()
=> GetAssemblies()
.SelectMany(s => s.GetTypes())
.Where(p =>
p.IsClass &&
p.IsAbstract == false &&
typeof(IAreaWeb).IsAssignableFrom(p)
)
.Select(x => x.Assembly);
private IEnumerable<Assembly> Types()
=> GetAssemblies()
.SelectMany(s => s.GetTypes())
.Where(p =>
p.IsClass &&
p.IsAbstract == false &&
typeof(IAreaWeb).IsAssignableFrom(p)
)
.Select(x => x.Assembly);
in line: s.GetTypes() I upgraded from .net 7 to .net 8. Anyone have any idea how to fix this error?
2 replies
CC#
Created by Alerin on 2/1/2023 in #help
❔ The application is available worldwide
I need to create an application that will be fast in different regions of the world. I want to use asp.net blazor version wasm for this, how should I make the server low ping in each region? How to make a database? Common for the whole world or base per region? Are there any ready-made solutions? Unfortunately, when I tested cloud azure, with a large number of queries to the database, the time is 40ms, which reduces the performance of the application.
28 replies
CC#
Created by Alerin on 12/6/2022 in #help
✅ Can I create a Liststring in mssql ef core?
my models:
public class Template
{
public int Id { get; set; }
public string Title { get; set; } = string.Empty; // Nazwa szablonu
public string Content { get; set; } = string.Empty;
public string Description { get; set; } = string.Empty;
public List<string> HashTags{ get; set; } = new();
}
public class Template
{
public int Id { get; set; }
public string Title { get; set; } = string.Empty; // Nazwa szablonu
public string Content { get; set; } = string.Empty;
public string Description { get; set; } = string.Empty;
public List<string> HashTags{ get; set; } = new();
}
Can I save data like this without creating a new table? This is a template, it will really be used little and I don't care about performance.
10 replies
CC#
Created by Alerin on 12/4/2022 in #help
❔ How to put schema.org in blazor server side?
Blaozr forbids us to put <script> in the code, the scheme works by putting json as javascript code. Any idea how to get to the server side of the header and put the @page code there?
4 replies
CC#
Created by Alerin on 12/2/2022 in #help
❔ How to make categories correctly?
I need to make a category in such a way that each can be inherited. I have this model:
public class Category
{
public int Id { get; set; }

public Thread Thread { get; set; } = null!;
public Guid ThreadId { get; set; }
public int Sorting { get; set; } = 0;
public string Name { get; set; } = string.Empty;
public ICollection<CategoryDetail> Details { get; set; } = null!;
public ICollection<Category> Subcategories { get; set; } = new List<Category>();
public int? CategoryId { get; set; }
}
public class Category
{
public int Id { get; set; }

public Thread Thread { get; set; } = null!;
public Guid ThreadId { get; set; }
public int Sorting { get; set; } = 0;
public string Name { get; set; } = string.Empty;
public ICollection<CategoryDetail> Details { get; set; } = null!;
public ICollection<Category> Subcategories { get; set; } = new List<Category>();
public int? CategoryId { get; set; }
}
public class CategoryDetail
{
public int Id { get; set; }
public Category Category { get; set; } = null!;
public int CategoryId { get; set; }

public string Culture { get; set; } = string.Empty;
public string Title { get; set; } = string.Empty;
public string Description { get; set; } = string.Empty;
}
public class CategoryDetail
{
public int Id { get; set; }
public Category Category { get; set; } = null!;
public int CategoryId { get; set; }

public string Culture { get; set; } = string.Empty;
public string Title { get; set; } = string.Empty;
public string Description { get; set; } = string.Empty;
}
Unfortunately, I'm having a big problem making an efficient query in EF Core. Did I do the model right?
7 replies
CC#
Created by Alerin on 11/28/2022 in #help
❔ ✅ Tree category EF Core
41 replies
CC#
Created by Alerin on 11/28/2022 in #help
❔ Own map with the possibility of embedding points.
Hi, I'm looking for a library that in blazor will allow me to create my own map (with my own background) and place points on it. I'm making a map with locations for an mmo game, do you know something like that?
6 replies
CC#
Created by Alerin on 11/27/2022 in #help
How to generate a category tree
3 replies
CC#
Created by Alerin on 10/16/2022 in #help
How the cache is stored
Hi, what kind of cache library do you use? How to solve the problem of multiple keys? Here is a similar problem: https://stackoverflow.com/questions/59103206/how-to-cache-an-object-with-multiple-keys-in-c-sharp The solution is interesting, but it makes the cache constant and can overflow (no expiry date of the item)
21 replies
CC#
Created by Alerin on 10/7/2022 in #help
The maximum length of the index
I have such a model:
[Index(nameof(Name), IsUnique = true)]
public class Variables
{
public int Id { get; set; }
[Required]
public string Name { get; set; } = string.Empty;
public string? Description { get; set; } = string.Empty;
public ICollection<Translate> Translates { get; set; } = null!;
}
[Index(nameof(Name), IsUnique = true)]
public class Variables
{
public int Id { get; set; }
[Required]
public string Name { get; set; } = string.Empty;
public string? Description { get; set; } = string.Empty;
public ICollection<Translate> Translates { get; set; } = null!;
}
EF core created this table for me:
CREATE TABLE [Stand].[Culture.Variables] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[Name] NVARCHAR (450) NOT NULL,
[Description] NVARCHAR (MAX) NULL,
CONSTRAINT [PK_Culture.Variables] PRIMARY KEY CLUSTERED ([Id] ASC)
);

GO
CREATE UNIQUE NONCLUSTERED INDEX [IX_Culture.Variables_Name]
ON [Stand].[Culture.Variables]([Name] ASC);
CREATE TABLE [Stand].[Culture.Variables] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[Name] NVARCHAR (450) NOT NULL,
[Description] NVARCHAR (MAX) NULL,
CONSTRAINT [PK_Culture.Variables] PRIMARY KEY CLUSTERED ([Id] ASC)
);

GO
CREATE UNIQUE NONCLUSTERED INDEX [IX_Culture.Variables_Name]
ON [Stand].[Culture.Variables]([Name] ASC);
[Name] NVARCHAR (450) NOT NULL,
[Name] NVARCHAR (450) NOT NULL,
Here we have a maximum length of 450 characters. Can I safely change this to MAX?
5 replies
CC#
Created by Alerin on 9/27/2022 in #help
Search for text and sort by most hits.
I wrote a function like this, it looks for words in the database. I would like to make it sort by the most hits. If the article finds 20 words that we entered, it will display in front of the article with, for example, 10 hits.
public async Task<List<Article>> ByTextAsync(List<string> search, Func<IQueryable<Models.Article>, IQueryable<Models.Article>>? parameter = null)
{
var query = _context.Article
.Include(x => x.Details)
.AsNoTracking();

if (parameter is not null)
query = parameter(query);

var articles = await query
.AsAsyncEnumerable()
.Where(x =>
search.Any(term =>
x.Details.Content.ToLower().Contains(term.ToLower()) ||
x.Details.Title.ToLower().Contains(term.ToLower())
)
)
.Select(async x => new
{
Article = await _article.GetAsync(x.Id),
})
.Select(x => x.Result.Article)
.ToListAsync();

return articles;
}
public async Task<List<Article>> ByTextAsync(List<string> search, Func<IQueryable<Models.Article>, IQueryable<Models.Article>>? parameter = null)
{
var query = _context.Article
.Include(x => x.Details)
.AsNoTracking();

if (parameter is not null)
query = parameter(query);

var articles = await query
.AsAsyncEnumerable()
.Where(x =>
search.Any(term =>
x.Details.Content.ToLower().Contains(term.ToLower()) ||
x.Details.Title.ToLower().Contains(term.ToLower())
)
)
.Select(async x => new
{
Article = await _article.GetAsync(x.Id),
})
.Select(x => x.Result.Article)
.ToListAsync();

return articles;
}
Anyone have an idea how to do this?
1 replies
CC#
Created by Alerin on 9/22/2022 in #help
Access list when creating an account
I'm creating a login system, I need an access list in it. Who can I allow to log in and who can I refuse. My model:
[Display(Name = "Access List", Description = "Access or deny list. Ability to set name, e-mail address, telephone, IPv4 or IPv6")]
public class AccessList
{
public int Id { get; set; }
public string Name { get; set; } = string.Empty;
public string? Description { get; set; }
public DataType Type { get; set; }
public AccessType Access { get; set; } = AccessType.Denied;
public StatusType Status { get; set; } = StatusType.Active;

public enum DataType
{
None,
Name, // User name
Mail, // Adress Mail
Phone, // Phone number
Domain, // Mail domain
IPv4, // Adres IP v4
IPv6, // Adres IP v6
}

public enum AccessType
{
Denied, // Black list
Access // White list
}

public enum StatusType
{
Inactive,
Active
}

}
[Display(Name = "Access List", Description = "Access or deny list. Ability to set name, e-mail address, telephone, IPv4 or IPv6")]
public class AccessList
{
public int Id { get; set; }
public string Name { get; set; } = string.Empty;
public string? Description { get; set; }
public DataType Type { get; set; }
public AccessType Access { get; set; } = AccessType.Denied;
public StatusType Status { get; set; } = StatusType.Active;

public enum DataType
{
None,
Name, // User name
Mail, // Adress Mail
Phone, // Phone number
Domain, // Mail domain
IPv4, // Adres IP v4
IPv6, // Adres IP v6
}

public enum AccessType
{
Denied, // Black list
Access // White list
}

public enum StatusType
{
Inactive,
Active
}

}
If the data in the database is a regular string, there is no problem finding it. The problem starts when I want to do a regex. For example: we have a username, we can do e.g. fuck, fuuck or some variation of it. (I'm not strong in English). It would be nice to do it with regex, now the question is how to search for such data in the database? Also, won't the regex become very slow at some point? The plus is that we search short phrases (max 320 characters).
8 replies
CC#
Created by Alerin on 9/21/2022 in #help
Passing the where parameters by the function
How do I get this sample code to work? Unfortunately, it does not want to work.
public async Task<User> GetAsync(Func<Models.User, bool> selector)
{
_model = await this.GetData().Where(x => selector(x)).SingleOrDefaultAsync() ?? new();
return new(_context, _model);
}
public async Task<User> GetAsync(Func<Models.User, bool> selector)
{
_model = await this.GetData().Where(x => selector(x)).SingleOrDefaultAsync() ?? new();
return new(_context, _model);
}
Use:
var user = await User.GetAsync(x => x.Mail == Model.Identifier || (x.Phone == Model.Identifier && x.PhoneCode == Model.PhoneCode));
var user = await User.GetAsync(x => x.Mail == Model.Identifier || (x.Phone == Model.Identifier && x.PhoneCode == Model.PhoneCode));
8 replies
CC#
Created by Alerin on 9/21/2022 in #help
Error Introducing FOREIGN KEY constraint
An error occurred while accessing the Microsoft.Extensions.Hosting services. Continuing without the application service provider. Error: Introducing FOREIGN KEY constraint 'FK_Identity.Roles_Identity.User_UserId' on table 'Identity.Roles' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
Could not create constraint or index. See previous errors.
An error occurred while accessing the Microsoft.Extensions.Hosting services. Continuing without the application service provider. Error: Introducing FOREIGN KEY constraint 'FK_Identity.Roles_Identity.User_UserId' on table 'Identity.Roles' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
Could not create constraint or index. See previous errors.
Models:
public class Roles
{
public int Id { get; set; }
public User User { get; set; } = null!;
public Guid UserId { get; set; }
public Role Role { get; set; } = null!;
public int RoleId { get; set; }
}
public class Roles
{
public int Id { get; set; }
public User User { get; set; } = null!;
public Guid UserId { get; set; }
public Role Role { get; set; } = null!;
public int RoleId { get; set; }
}
public class Role
{
public int Id { get; set; }
public string Name { get; set; } = string.Empty;
public ICollection<RolePolicy> Permission { get; set; } = new List<RolePolicy>();
}
public class Role
{
public int Id { get; set; }
public string Name { get; set; } = string.Empty;
public ICollection<RolePolicy> Permission { get; set; } = new List<RolePolicy>();
}
public class RolePolicy
{
public int Id { get; set; }
public Role Role { get; set; } = null!;
public int RoleId { get; set; }
public string Policy { get; set; } = string.Empty;
}
public class RolePolicy
{
public int Id { get; set; }
public Role Role { get; set; } = null!;
public int RoleId { get; set; }
public string Policy { get; set; } = string.Empty;
}
public class User
{
[Key]
public Guid Id { get; set; }

[StringLength(16, MinimumLength = 4), Required]
public string Name { get; set; } = string.Empty;

...

public Role DisplayGroup { get; set; } = null!;
public int DisplayGroupId { get; set; }
public ICollection<Roles> Roles { get; set; } = new List<Roles>();
public class User
{
[Key]
public Guid Id { get; set; }

[StringLength(16, MinimumLength = 4), Required]
public string Name { get; set; } = string.Empty;

...

public Role DisplayGroup { get; set; } = null!;
public int DisplayGroupId { get; set; }
public ICollection<Roles> Roles { get; set; } = new List<Roles>();
5 replies
CC#
Created by Alerin on 9/21/2022 in #help
Microsoft.Data.SqlClient.SqlException (0x80131904) Invalid object name 'Stand.Identity.Role'.
What's wrong here? I do not understand what's going on...
public class Context : DbContext
{
public DbSet<Models.Role> Role => Set<Models.Role>();
public DbSet<Models.RolePolicy> RolePolicy => Set<Models.RolePolicy>();
public DbSet<Models.Roles> Roles => Set<Models.Roles>();
}
public class Context : DbContext
{
public DbSet<Models.Role> Role => Set<Models.Role>();
public DbSet<Models.RolePolicy> RolePolicy => Set<Models.RolePolicy>();
public DbSet<Models.Roles> Roles => Set<Models.Roles>();
}
public class Roles
{
public int Id { get; set; }
public User User { get; set; } = null!;
public Guid UserId { get; set; }
public Role Role { get; set; } = null!;
public int RoleId { get; set; }
}
public class Roles
{
public int Id { get; set; }
public User User { get; set; } = null!;
public Guid UserId { get; set; }
public Role Role { get; set; } = null!;
public int RoleId { get; set; }
}
public class Role
{
public int Id { get; set; }
public string Name { get; set; } = string.Empty;
public ICollection<RolePolicy> Permission { get; set; } = new List<RolePolicy>();
}
public class Role
{
public int Id { get; set; }
public string Name { get; set; } = string.Empty;
public ICollection<RolePolicy> Permission { get; set; } = new List<RolePolicy>();
}
public class RolePolicy
{
public int Id { get; set; }
public Role Role { get; set; } = null!;
public int RoleId { get; set; }
public string Policy { get; set; } = string.Empty;
}
public class RolePolicy
{
public int Id { get; set; }
public Role Role { get; set; } = null!;
public int RoleId { get; set; }
public string Policy { get; set; } = string.Empty;
}
Error: https://pastebin.com/PJhZDHyh
6 replies
CC#
Created by Alerin on 9/20/2022 in #help
Storage of telephone numbers in a database
Hi, I want to log in via e-mail and telephone number. How to store the phone number? I was thinking about code + number phone, unfortunately I have a problem with detecting what is a number and what is a code. The user has one input, if the system detects that he has given his phone number, he must add a code depending on the culture. There are a few problems: 1.) The user must enter the telephone number code each time 2.) When he enters the phone number alone, there may be a problem with sending the verification message, what if the user is in Poland and has a number from Germany? Is there any solution ready for this? The topic seems to be very difficult.
38 replies
CC#
Created by Alerin on 9/18/2022 in #help
A ready-made ef core data search solution
68 replies
CC#
Created by Alerin on 9/11/2022 in #help
Reload the list
2 replies