Custom MVC model - where is a simple example?

I need to create a web application and I want to use MVC. However, my model cannot be one of the standard models - data is not stored in the database, but in an external application, accessible only through the API. Since this is the first MVC application that I implemented, I rely on examples to figure out how to do this. I cannot find examples of a non-database model. An example of a custom model would also be nice. Can someone point me to such a beast? Perhaps MVC is only for the new, and does not exist.

It looks like I could get away with the DataSet model, but I have not seen examples of using this object. I expect the DataSet example to help me too. (Maybe it's the same thing?)

Please note: I have seen many examples of custom bindings. This is NOT what I want. I need an example user model that is not bound to a specific database / table.

UPDATE

I found a good example from MS located here:

http://msdn.microsoft.com/en-us/library/dd405231.aspx

While this is the "answer" to my question, I don’t like it very much, because it connects me with MS's view of the world. @Aaronaught, @jeroenh, and @tvanfosson give much better answers from the meta point of view about moving my understanding (and yours?) Forward regarding the use of MVC.

I give the @Aaronaught check because it has sample code (which I requested). Thanks to everyone and feel free to add even better answers if you have any.

+3
source share
3 answers

, ; . , - - , .

, MVC, - - , , . :

[DataContract(Namespace = "http://services.acme.com")]
public class Customer
{
    [DataMember(Name = "CustomerID")]
    public Guid ID { get; set; }

    [DataMember(Name = "CustomerName")]
    public string Name { get; set; }
}

, :

public interface ICustomerRepository
{
    Customer GetCustomerByID(Guid id);
    IList<Customer> List();
}

"API" :

public class AcmeWSCustomerRepository : ICustomerRepository, IDisposable
{
    private Acme.Services.CrmServiceSoapClient client;

    public AcmeWSCustomerRepository()
        : this(new Acme.Services.CrmServiceSoapClient())

    public AcmeWSCustomerRepository(Acme.Services.CrmServiceSoapClient client)
    {
        if (client == null)
            throw new ArgumentNullException("client");
        this.client = client;
    }

    public void Dispose()
    {
        client.SafeClose();    // Extension method to close WCF proxies
    }

    public Customer GetCustomerByID(Guid id)
    {
        return client.GetCustomerByID(id);
    }

    public IList<Customer> List()
    {
        return client.GetAllCustomers();
    }
}

, , , - XML :

public class LocalCustomerRepository : ICustomerRepository, IDisposable
{
    private XDocument doc;

    public LocalCustomerRepository(string fileName)
    {
        doc = XDocument.Load(fileName);
    }

    public void Dispose()
    {
    }

    public Customer GetCustomerByID(Guid id)
    {
        return
            (from c in doc.Descendants("Customer")
             select new Customer(c.Element("ID").Value, c.Element("Name").Value))
            .FirstOrDefault();
    }

    // etc.
}

, , - - . WCF; - . "". , WCF , DataContract, Linq-to-XML - API; , .

, . , , Linq to SQL Entity Framework /. "" , ViewModel .

, , . , . , - ! POCO , , / API.

+5

, , , , DB. , , , -. , , , .

, , ( ).

+4

, "" MVC, , , , , .

In a well-designed MVC application, access to data is somehow abstracted, usually using some form of repository template: you define the level of abstraction (say, the IRepository interface) that defines the contract needed to receive and save data, the actual implementation is usually calls the database, but in your case you should name your "API".

Here is an example MVC application that calls the WCF service.

+1
source

Source: https://habr.com/ru/post/1732432/


All Articles