Bryan
Bryan

Reputation: 3541

How to use model types between projects?

I'm using service layer lattern, and my solution contains three projects:

In my controller In the UI-project I have the following code:

 public ActionResult AddSpotifyAlbums(List<SpotifyAlbums> albums)
        {
            _profileService.AddSpotifyAlbums(albums);
            return Json(new { data = albums });
        }

SpotifyAlbums in List<SpotifyAlbums> Is a Model in my UI project. It looks like this:

  public class SpotifyAlbums
    {
        public string URI { get; set; }
        public int profileId { get; set; }
    }

As you can see, I'm using a service here, _profileService, to call a method that handles the saving of the albums.

But the type of the albums Is SpotifyAlbums. How should I do in my service when the model type of the albums is SpotifyAlbums?

I have tried to create a identical model in my Service-project, but it doesn't work. I get the following error:

Severity Code Description Project File Line Suppression State Error CS1503 Argument 1: cannot convert from 'System.Collections.Generic.List' to 'soundyladder.Service.Models.SpotifyAlbums'

Upvotes: 3

Views: 1361

Answers (3)

Clint B
Clint B

Reputation: 4700

I use the same design pattern (resource-based trusted subsystem). I like to use a .dll that contains common code and shared models. Then all projects reference that .dll for the model definitions.

Upvotes: 0

Riad Baghbanli
Riad Baghbanli

Reputation: 3319

Bryan, you need to decide which module "owns" SpotifyAlbum. Decision needs to take in consideration data encapsulation, data access and data manipulation aspects. Is important to establish proper service references and get all modules using SpotifyAlbum instances to grab it from the module "owning" the SpotifyAlbum. Then all other modules would have proxy classes built from the references to "owning" module.

Upvotes: 0

Babak Naffas
Babak Naffas

Reputation: 12561

You cannot arbitrarily assign objects of different types to one another. Even though you give them the same name, they are independent entities.

Assuming the Service and UI projects reference your Core project, you can define the SpotifyAlbums class in your Core project and reference that class across your other layers.

Upvotes: 1

Related Questions