Domanda

Sto cercando di creare un semplice progetto di tre livelli e sono sulla buona strada per l'attuazione dello strato di Business Logic, in questo momento, questo è come il mio aspetto BL come

//The Entity/BO
public Customer
{
    public int CustomerID { get; set; }
    public string CustomerName { get; set; }
    public UserAccount UserAccount { get; set; }
    public List<Subscription> Subscriptions { get; set; }
 }

//The BO Manager
public class CustomerManager
{

     private CustomerDAL _dal = new CustomerDAL();

    private Customer _customer;

    public void Load(int customerID)
    {
        _customer = GetCustomerByID(customerID);
    }


    public Customer GetCustomer()
    {
        return _customer;
    }


    public Customer GetCustomerByID(int customerID)
    {

        return _dal.GetCustomerByID(customerID);
    }


    public Customer GetCustomer()
    {
        return _customer;
    }

    public UserAccount GetUsersAccount()
    {

        return _dal.GetUsersAccount(_customer.customerID);
    }

    public List<Subscription> GetSubscriptions()
    {
         // I load the subscriptions in the contained customer object, is this ok??
        _customer.Subscriptions = _customer.Subscriptions ?? _dal.GetCustomerSubscriptions(_customer.CustomerID);

        return _customer.Subscriptions;
    }

Come si può notare, il mio manager oggetto è in realtà solo un contenitore di mio vero oggetto (Cliente), e questo è dove ho messo la mia logica di business, un po 'modo di disaccoppiare l'entità di business per la logica di business, questo è come mi tipicamente usarlo

        int customerID1 = 1;
        int customerID2 = 2;

        customerManager.Load(1);

        //get customer1 object
        var customer = customerManager.GetCustomer();

        //get customer1 subscriptions
        var customerSubscriptions = customerManager.GetSubscriptions();

        //or this way
        //customerManager.GetSubscriptions();
        //var customerSubscriptions = customer.Subscriptions;


        customerManager.Load(2);

        //get customer2
        var newCustomer = customerManager.GetCustomer();

        //get customer2 subscriptions
        var customerSubscriptions = customerManager.GetSubscriptions();

Come si può vedere che detiene solo 1 oggetto per volta, e se ho bisogno di gestire Elenco dei clienti, probabilmente necessario creare un altro manager come CustomerListManager

La mia domanda è, è questo il modo giusto di attuazione della BL di un / disegno a tre livelli strati? O qualsiasi suggerimento su come si dovrebbe attuarla. Grazie.

È stato utile?

Soluzione

Come altri hanno già detto prima si dovrebbe avere uno sguardo alla Repository modello. Io consiglio anche di controllare le unità di lavoro modello così come Domain Driven Design per l'architettura dell'applicazione in generale.

Si potrebbe anche avere uno sguardo in oggetto relazionale-mapping ( ORM ) framework for .NET come Entity Framework o NHibernate se questa è un'opzione per te.

Per darvi un vantaggio temporale qui ci sono alcune grandi risorse per arrivare sulla strada giusta:

Libri

riferimenti online

La speranza che aiuta.

Altri suggerimenti

Effettua una ricerca per il modello repository, penso che sarà rispondere alle vostre esigenze.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top