Home > Net >  EF LIINQ what is good design method, the former tone feel more code also repeated use method.
EF LIINQ what is good design method, the former tone feel more code also repeated use method.

Time:09-18

 
//delete this method, there are a lot of entity object, will write a lot of, what method can be extended or reuse?
Public ActionResult Delete (int id)
{
//by ID data query, delete,
Var entity=DataContext. Category. The Where (a=& gt; Amy polumbo K_ID==id). FirstOrDefault ();
DataContext. Category. Remove (entity);
Var res=DataContext. SaveChanges ()
If (res & gt; 0)
{
Return a Json (true, JsonRequestBehavior. AllowGet);
}
Return a Json (false, JsonRequestBehavior. AllowGet);
}

//want to write, front-end AJAX, an entity name, because it is the front-end calls,
Public ActionResult Delete (int id, string modelType)
{
//but this modelType is string, behind the where condition can not write,
Var entity=DataContext. Category. The Where (a=& gt; Amy polumbo K_ID==id). FirstOrDefault ();
//which what method or ingenious ways to give directions?
}

CodePudding user response:

Generic know
Protected override T delete The entity (T)
{
Return _context. Set (a). Remove (entity);
}

CodePudding user response:

DbContext _context=new DbContext (the connectionString);

CodePudding user response:

Use generic feeling line passing, generic or to specify, front desk call can only be strings, AJAX can't type,

CodePudding user response:

My approach is put forward when sealing up into object inside, ActionResult method according to the id with the factory to obtain object, then calls the object delete,
That you are on the stability, you write operations are to die in ActionResult what extension

CodePudding user response:

reference 4 floor nan peony reply:
my approach is put forward when sealing up into object inside, ActionResult method according to the id with the factory to obtain object, then calls the object delete,
That you are on the stability, you write operations are to die in ActionResult what extension to speak


My is packaging, can you so that each class object, should write a delete method? Then put the id to the factory for object, so that will have a lot of the same things,

I'm now, each class of share a delete method, rather than each class to create an ActionResult delete methods, such a project will be much the same code,

CodePudding user response:

That it may not seem like a difficult thing,
The base class for you to create a controller
Then by generic basic crud operations,

CodePudding user response:

references to disable the F3 5 floor response:
Quote: refer to 4th floor nan peony reply:

My approach is put forward when sealing up into object inside, ActionResult method according to the id with the factory to obtain object, then calls the object delete,
That you are on the stability, you write operations are to die in ActionResult what extension to speak


My is packaging, can you so that each class object, should write a delete method? Then put the id to the factory for object, so that will have a lot of the same things,

I'm now, each class of share a delete method, instead of each class to create an ActionResult deletion method, a project will be much the same code,


Can a generic abstraction inherited, how can repeat? Operation is written in the generic superclass statement about virtual then derived is not good, is not the object oriented is that so

CodePudding user response:

reference 6 floor is anger god reply:
that it may not seem like a very hard thing to do,
The base class for you to create a controller
Then by generic basic crud operations,

For, all controller inherits a base class, the base class a deletion method, the front desk is call a base of this method can be invoked, but the premise is that of the generic type when you invoke the generic, you're going to turn into the actual class to give him, the type of the entity who turn? I want to solve the subclasses don't create method calls the superclass delete method, so that will have the same code, the first a subclass will call a base class to delete method, there are still a lot of the same code,

CodePudding user response:

Generics,

The
reference 1/f, hands on the keyboard to knock very light response:

under the generic understandProtected override T delete The entity (T)
{
Return _context. Set (a). Remove (entity);
}


Parameters can add a keyName and keyValue just call introduced into different places each entity, delete operation itself is completely can be reused,

CodePudding user response:

references 9 f chengangcsdn response:
generics,

Quote: reference 1/f, hands on the keyboard to knock very light response:

Generic know
Protected override T delete The entity (T)
{
Return _context. Set (a). Remove (entity);
}


Parameters can add a keyName and keyValue just call introduced into different places each entity, delete operation itself is completely can be reused,


Yes, I want to solve the caller, there are too many local call a generic method to delete, so think a way to solve, don't write too much of the caller

CodePudding user response:

references to the tenth floor to disable the F3 response:
Quote: references 9 f chengangcsdn response:

Generics,

Quote: reference 1/f, hands on the keyboard to knock very light response:

Generic know
Protected override T delete The entity (T)
{
Return _context. Set (a). Remove (entity);
}


Parameters can add a keyName and keyValue just call introduced into different places each entity, delete operation itself is completely can be reused,


Yes, I want to solve the caller, there are too many local call a generic method to delete, so think a way to solve, don't write too much the caller



Generic who all know! Type T can only static compilation, no dynamic, that is to say you can't choose according to ajax to the T, the T must be defined in advance, so you can pack a layer, or use reflection! Ajax pass the name of the class, and then reflect the entity with ef to delete, to parameters of the abstract attributes, but such extension is limited, take temporary solution not effect a permanent cure

CodePudding user response:

references to disable the F3 eighth floor response:
Quote: refer to the sixth floor is nu month god reply:

That it may not seem like a difficult thing,
The base class for you to create a controller
Then by generic basic crud operations,

For, all controller inherits a base class, the base class a deletion method, the front desk is call a base of this method can be invoked, but the premise is that of the generic type when you invoke the generic, you're going to turn into the actual class to give him, the type of the entity who turn? I want to solve the subclasses don't create method calls the superclass delete method, so that will have the same code, the first subclass have to call a base class to delete method, there are still a lot of the same code,


You can see I write blog before,
Mainly, this sentence can add generics to dbcontext context management
 dbEF. Entry (entity) 

As for deletion method, actually very convenient also, you can specify the incoming & lt; T, S> Two generic,
Refer to the type, T S only with the type of the primary key id,
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  •  Tags:  
  • C #
  • Related