C
C#7mo ago
!Rushaan

Unable to import models in service interface

Hi im learning Dependency Injection for the first time and making a micro-project to try it out. But I have a model class in my main project and a class from that model is needed in the service interface however im unable to import the model class in the interface.
namespace ASPNETCoreLearn2.Models
{
public class CityWeather
{
public string CityUniqueCode { get; set; }
public string CityName { get; set; }
public DateTime DateAndTime { get; set; }
public int TemperatureFahrenheit { get; set; }
}
}
namespace ASPNETCoreLearn2.Models
{
public class CityWeather
{
public string CityUniqueCode { get; set; }
public string CityName { get; set; }
public DateTime DateAndTime { get; set; }
public int TemperatureFahrenheit { get; set; }
}
}
using ASPNETCoreLearn2.Models; //TYPE OR NAMESPACE COULD NOT BE FOUND
namespace ServiceContracts
{
public interface IWeatherService
{
public List<CityWeather> GetWeatherDetails();
}
}
using ASPNETCoreLearn2.Models; //TYPE OR NAMESPACE COULD NOT BE FOUND
namespace ServiceContracts
{
public interface IWeatherService
{
public List<CityWeather> GetWeatherDetails();
}
}
I can import ServiceContracts in ASPNETCoreLearn2 but i can't do vice-versa even though ive properly added the project dependencies.
10 Replies
Becquerel
Becquerel7mo ago
if you can import ServiceContracts in ASPNETCoreLearn2, that would imply that the ASPNETCoreLearn2 project has a dependency on ServiceContracts as opposed to ServiceContracts having a dependency on ASPNETCoreLearn2. which contains the CityWeather type does the .csproj for the ServiceContracts project have a reference to the ASPNETCoreLearn2 project? project references in .NET are not bi-directional, they are one-way
!Rushaan
!RushaanOP7mo ago
i have added project dependency of each other in both the projects
Becquerel
Becquerel7mo ago
wouldn't that cause a cyclical dependency?
!Rushaan
!RushaanOP7mo ago
okay so how exactly am i supposed to import the models from ASPNETCoreLearn2 and use it in the interface and then use that interface back in ASPNETCoreLearn2 ig
Becquerel
Becquerel7mo ago
if you have types that are required by multiple projects, you would typically put them in a third, lower-level project
!Rushaan
!RushaanOP7mo ago
oh so i create another class library project to store the models u mean?
Becquerel
Becquerel7mo ago
yep a lot of people make a Models project for this purpose
!Rushaan
!RushaanOP7mo ago
alr that would be like 4th project in my solution i see ty imma try this
Becquerel
Becquerel7mo ago
or they call it Core, or Base, or Impl or whatever else unfortunately when you try to be 'clean' with projects you kinda just end up having a lot of them it is what it is
!Rushaan
!RushaanOP7mo ago
oh ty it worked btw
Want results from more Discord servers?
Add your server