Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
New exception?
Message
De
03/10/2012 15:59:24
 
 
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Titre:
Versions des environnements
Environment:
C# 4.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Divers
Thread ID:
01554234
Message ID:
01554255
Vues:
50
With a web app, you don't have state, so you should always commit. Passing a bool as a parameter is considered bad form. That class doesn't smell right to me. A class should have one function only. This one does multiple things. It's mixing context and repository. There should be multiple classes doing one thing. I'll have to work up a simple example and then post it.

>This is MVC application I am working on. I have the Repository class with this code:
>
>
>using System.Data.Entity;
>using System.Linq;
>using CardNumbers.Objects;
>
>namespace CardNumbers.Data
>{
>    public class Repository : DbContext, IRepository
>    {
>        public DbSet<Client> Clients { get; set; }
>        public DbSet<ClientOrder> ClientOrders { get; set; }
>        public DbSet<Reorder> Reorders { get; set; }
>        public DbSet<Operator> Operators { get; set; }
>
>        IQueryable<Client> IRepository.Clients
>        {
>            get { return Clients; }
>        }
>
>        IQueryable<ClientOrder> IRepository.ClientOrders
>        {
>            get { return ClientOrders; }
>        }
>
>        IQueryable<Operator> IRepository.Operators
>        {
>            get { return Operators; }
>        }
>
>        IQueryable<Reorder> IRepository.Reorders
>        {
>            get { return Reorders; }
>        }
>
>        public void Commit()
>        {
>            this.SaveChanges();
>        }
>
>        public void AddClient(Client client, bool autoCommit = true)
>        {
>            Clients.Add(client)   ;
>            if (autoCommit) Commit();
>        }
>
>        public void DeleteClient(Client client, bool autoCommit = true)
>        {
>            Clients.Remove(client);
>            if (autoCommit) Commit();
>        }
>
>        public void DeleteClient(int clientId, bool autoCommit = true)
>        {
>            var ReordersQuery = from reord in this.Reorders
>                                where reord.ClientId == clientId
>                                select reord;
>
>            if (ReordersQuery.Any())
>               // throw "Client " + clientId.ToString() + " can not be deleted because it has related rows in the ReOrders table!";
>
>        }
>
>
>        public void AddOperator(Operator oOperator, bool autoCommit = true)
>        {
>            Operators.Add(oOperator);
>            if (autoCommit) Commit();
>        }
>
>        public void DeleteOperator(Operator oOperator, bool autoCommit = true)
>        {
>            Operators.Remove(oOperator);
>            if (autoCommit) Commit();
>        }
>
>        public void AddOrder(ClientOrder clientorder, bool autoCommit = true)
>        {
>            ClientOrders.Add(clientorder);
>            if (autoCommit) Commit();
>        }
>    }
>}
>
>
>Since I am going to call DeleteClient method, I am thinking I may need less generic variation and manually test dependencies myself. So, I started to write a version that will delete based on id.
>
>Perhaps I don't need this method?
>
>I started to write it based on this logic I found in MSDN after I googled on 'LINQ delete' http://msdn.microsoft.com/en-us/library/bb386925.aspx
>
>
>
>
>>Let's backup a bit.
>>
>>Is this a web app or desktop? Where is the context defined?
>>
>>>Hi everybody,
>>>
>>>I am writing a method to delete a cient in my Repository class. Here is what I've started from:
>>>
>>>
>>>public void DeleteClient(int clientId, bool autoCommit = true)
>>>        {
>>>            var ReordersQuery = from reord in this.Reorders
>>>                                where reord.ClientId == clientId
>>>                                select reord;
>>>
>>>            if (ReordersQuery.Any())
>>>                //throw "Client " + clientId.ToString() + " can not be deleted because it has related rows in the ReOrders table!";
>>>
>>>        }
>>>
>>>I read help on throw as I need to throw a new exception. My question is - what kind of exception it should be?
>>>
>>>Thanks in advance.
Craig Berntson
MCSD, Microsoft .Net MVP, Grape City Community Influencer
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform