I am trying to follow the ReactiveUI.Cinephile sample using the ContentPageBase. But when I extend it on my View, I get this error:
Inconsistent accessibility: base class 'ContentPageBase<HomeViewModel>' is less accessible than class 'HomeView'
Here uis the HomeView:
namespace ReactiveTest.Views
{
public partial class HomeView : ContentPageBase<HomeViewModel>
{
public HomeView()
{
InitializeComponent();
}
}
}
Here is the ContentPageBase class
namespace ReactiveTest.Views
{
public class ContentPageBase<TViewModel> : ReactiveContentPage<TViewModel> where TViewModel : class
{
}
}
Any idea how I can fix this?
Make sure both HomeView AND HomeViewModel are public. The generic type effects accessibility too.
Related
I'm working with a Symfony 3.4 (PHP 7.2, update to 7.4 soon) project. I have some classes who extends an abstract class and i would like all my classes got the same constant name (constant have value different in each class). I'm starting with a pattern like this :
abstract class AbstractClass
{
abstract public function getConstant(): string;
}
final class Foo extends AbstractClass
{
const MY_CONST = 'foo';
public function getConstant(): string
{
return self::MY_CONST;
}
}
final class Bar extends AbstractClass
{
const MY_CONST = 'bar';
public function getConstant(): string
{
return self::MY_CONST;
}
}
// echo $foo->getConstant() : 'foo'
// echo $bar->getConstant() : 'bar'
The goal: if a class who extends AbstractClass don't have MY_CONST, i want return an message error.
I have excluded theses solutions :
I can't add a constant in an interface (maybe in PHP 8 ?)
I can't use "abstract factory" pattern for a constant (in my code it runs with getConstant() method
I can't use a static property
The only way i have found is : implement an interface and tag the interface like explain in documentation. With compilerpass, helped with ReflexionClass, i will check if constant name exist, and if not: thrown an error or something like this.
So, i've edit like this :
final class Foo extends AbstractClass implements MyCustomInterface
{
// ...
}
final class Bar extends AbstractClass implements MyCustomInterface
{
// ...
}
The interface :
interface MyCustomInterface
{
}
Adding tag in AppKernel.php
protected function build(ContainerBuilder $container): void
{
$container
->registerForAutoconfiguration(MyCustomInterface::class)
->addTag('my_custom_tag');
}
And a compilerpass :
class MyCustomPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container): void
{
if (!$container->has(MyCustomInterface::class)) {
dump('No interface found');
return;
}
$definition = $container->findDefinition(MyCustomInterface::class);
$taggedServices = $container->findTaggedServiceIds('my_custom_tag');
dump($taggedServices);
}
}
The fun begin here...if i have only ONE class who implement the interface, $taggedServices find the service. BUT, if more than one class implements interface, no class are found...
I don't find where i am wrong. Do i need to implements AbstractClass instead of children classes ?
I want to build a base controller that I can put some reusable methods so I do not have to put a bunch of repeat code in all my controllers. So I built a BaseController.cs
public class BaseController : Controller
{
public IHttpClientFactory _clientFactory;
public BaseController(IHttpClientFactory clientFactory)
{
_clientFactory = clientFactory;
}
}
Then in one of my contollers I do public class TokenController : BaseController. But then it wants me to add the following but then it gives me errors
public TokenController(IHttpClientFactory clientFactory)
{
// I guess something goes here
}
But then VS Code tells me
There is no argument given that corresponds to the required formal parameter 'clientFactory' of 'BaseController.BaseController(IHttpClientFactory)' (CS7036)
What am I missing here? I been in JS world to long :)
When inheriting classes without default constructors you have to pass parameters to them using the following syntax:
public TokenController(IHttpClientFactory clientFactory) : base (clientFactory)
{
/* other initializations */
}
So add the following expression: : base (clientFactory)
See more information here: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/using-constructors
I am reading this article:
https://docs.asp.net/en/latest/tutorials/first-web-api.html.
and they say. you have to put this:
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using TodoApi.Models;
namespace TodoApi.Controllers
{
[Route("api/[controller]")]
public class TodoController : Controller
{
public TodoController(ITodoRepository todoItems)
{
TodoItems = todoItems;
}
}
}
But where is TodoItems declared?
Do I something wrong? Or is it mistake in the article?
Because If I put this in the file:
public ITodoRepository TodoItems { get; private set; }
The error goes away. But is this correct?
It's probably mistake. You should declare it as:
public class TodoController : Controller
{
private readonly ITodoRepository TodoItems;
public TodoController(ITodoRepository todoItems)
{
TodoItems = todoItems;
}
}
Here is using dependency injection by constructor.
So, I think that declaring of TodoItems as a public property is not a good idea. It should be a private readonly field that initializing only in constructor. In other case if you going to use inheritance it might be declared as a protected.
I am learning ServiceStack and developing simple demo for helloworld, but could not find namespace for ISservice interface, my code as per below:
public class Hello
{
public string name { get; set; }
}
public class HelloResponse
{
public string Result { get; set; }
}
public class HelloService : **IService**<Hello>
{
public object Execute(Hello request)
{
return new HelloResponse { Result = "Hello" + request.name };
}
}
public class HelloAppHost : AppHostBase
{
public HelloAppHost() : base("Hello Web Services", typeof(HelloService).Assembly) { }
public override void Configure(Funq.Container container)
{
Routes.Add<Hello>("/hello")
.Add<Hello>("/hello/{Name}");
}
}
Can anyone please tell me what namespace or DLL I need to add for IService interface?
ServiceStack's IService<T> is in the ServiceStack.ServiceHost namespace which lives in the ServiceStack.Interfaces.dll, why here's the class:
https://github.com/ServiceStack/ServiceStack/blob/master/src/ServiceStack.Interfaces/ServiceHost/IService.cs
Note: If you're just starting out, it's probably better to inherit from ServiceStack.ServiceInterface.ServiceBase<T> and override the Run() method which is a useful base class that provides things like auto exception handling for you.
If you want to be able run different code for different HTTP Verbs e.g GET/POST/PUT/DELETE (i.e. creating REST web services) than you want to inherit from RestServiceBase instead and override its OnGet/OnPost/OnPut/OnDelete methods.
I am trying to get my head around the sharp architecture and follow the tutorial. I am using this code:
using Bla.Core;
using System.Collections.Generic;
using Bla.Core.DataInterfaces;
using System.Web.Mvc;
using SharpArch.Core;
using SharpArch.Web;
using Bla.Web;
namespace Bla.Web.Controllers
{
public class UsersController
{
public UsersController(IUserRepository userRepository)
{
Check.Require(userRepository != null,"userRepository may not be null");
this.userRepository = userRepository;
}
public ActionResult ListStaffMembersMatching(string filter) {
List<User> matchingUsers = userRepository.FindAllMatching(filter);
return View("ListUsersMatchingFilter", matchingUsers);
}
private readonly IUserRepository userRepository;
}
}
I get this error:
The name 'View' does not exist in the current context
I have used all the correct using statements and referenced the assemblies as far as I can see. The views live in Bla.Web in this architecture.
Can anyone see the problem?
Thanks.
Christian
You should inherit UsersController from System.Web.Mvc.Controller class. View() method is defined in Controller class.
public class UsersController : Controller
{
//...
}